输出

输出
vars:

servers:
  - name: centos
    port: 22

tasks:

- name: Check if remote port
  wait_for: host={{ item.name }} port={{ item.port }} timeout=1
  ignore_errors: True
  register: out
  with_items: "{{ servers }}"

- debug: var=out

- name: Save remote port   
  shell: echo "{{ item.host }}" > /tmp/x_output.csv
  args:
    executable: /bin/bash
  with_items: "{{ out.results }}"

输出

PLAY [all] **************************************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************
ok: [centos]

TASK [telnet : Check if remote port] ************************************************************************************************
ok: [centos] => (item={u'name': u'centos', u'port': u'22'})

TASK [telnet : debug] ***************************************************************************************************************
ok: [centos] => {
    "out": {
        "changed": false,
        "msg": "All items completed",
        "results": [
            {
                "_ansible_ignore_errors": true,
                "_ansible_item_result": true,
                "_ansible_no_log": false,
                "_ansible_parsed": true,
                "changed": false,
                "elapsed": 0,
                "failed": false,
                "invocation": {
                    "module_args": {
                        "active_connection_states": [
                            "ESTABLISHED",
                            "FIN_WAIT1",
                            "FIN_WAIT2",
                            "SYN_RECV",
                            "SYN_SENT",
                            "TIME_WAIT"
                        ],
                        "connect_timeout": 5,
                        "delay": 0,
                        "exclude_hosts": null,
                        "host": "centos",
                        "msg": null,
                        "path": null,
                        "port": 22,
                        "search_regex": null,
                        "sleep": 1,
                        "state": "started",
                        "timeout": 1
                    }
                },
                "item": {
                    "name": "centos",
                    "port": "22"
                },
                "path": null,
                "port": 22,
                "search_regex": null,
                "state": "started"
            }
        ]
    }
}

TASK [telnet : Save remote port] ****************************************************************************************************
fatal: [centos]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'host'\n\nThe error appears to have been in '/home/xxxxxx/ansible/tso-playbook/roles/telnet/tasks/main.yml': line 17, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Save remote port\n  ^ here\n\nexception type: <class 'ansible.errors.AnsibleUndefinedVariable'>\nexception: 'dict object' has no attribute 'host'"}
        to retry, use: --limit @/home/xxxxxxx/ansible/tso-playbook/telnet.retry

PLAY RECAP **************************************************************************************************************************
centos                     : ok=3    changed=0    unreachable=0    failed=1

注意:这是我第一次在这里发帖,不知道如何正确地逐行修复...我只想访问 out.host 'centos' 并将其保存在 csv 文件中,当然我需要做更多,但这是我需要做的第一件事,请帮忙!谢谢。

---

- name: Save remote port   
  shell: echo {{ item.changed }} > /tmp/x_output.csv

  args:
    executable: /bin/bash

  with_items: "{{ out.results }}"

这是我唯一可以参考的,item.changed 为“False”,但其他的我都不能。

为什么?

答案1

正如您在发布的调试中所看到的,与键不同,列表元素中changed没有键。hostresults

字典里有一个invocation,因此:

shell: echo "{{ item.invocation.module_args.host }}" > /tmp/x_output.csv

虽然它是你自己定义的值,而不是返回值。

相关内容