Ansible: access variables and dump to CSV file

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 }}"

OUTPUT

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

Note: it's my first time to post here, don't know how to fix line by line properly... I just want to access the out.host which is 'centos' and save it on the csv file, of course I need to do more, but this is the first thing I need to do, please help! Thanks.

---

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

  args:
    executable: /bin/bash

  with_items: "{{ out.results }}"

This is the only one I can refer, item.changed which is "False" but all others I can't.

Why?

1
задан 22 October 2018 в 14:55
1 ответ

Как вы можете видеть в опубликованной вами отладке, в отличие от измененного ключа , в элементах результатов отсутствует ключ host 1139281].

Есть один внутри словаря вызова , поэтому:

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

Хотя это значение, которое вы определили сами, а не возвращаемое значение.

0
ответ дан 4 December 2019 в 04:13

Теги

Похожие вопросы