Ansible loop related issues

2019-03-05 18:38发布

I have a playbook which has multiple roles and serial setup so that fist it's running on one machine then on the rest of them. In one of the roles I have the following tasks:

- name: getting dbnodes IP addresses
  local_action: shell echo  "{% for host in groups['dbnodes'] %}{{ hostvars[host]['ansible_eth0']['ipv4']['address'] }},{% endfor %}"
  run_once: true
  register: IPS

Basically what I want to do is to gather the IP addresses of all the hosts and register it with IPS for further usage. But the task is failing because of the serial (I think) with the following error.

TASK [dbcluster : getting dbnodes IP addresses] ******************************** fatal: [162.220.52.190]: FAILED! => {"failed": true, "msg": "the field 'action' has an invalid value, which appears to include a variable that is undefined. The error was: 'dict object' has no attribute 'ansible_eth0'\n\nThe error appears to have been in '/root/tenon-delivery/ansible/roles/dbcluster/tasks/main.yml': line 52, 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: getting dbnodes IP addresses\n ^ here\n"}

While running ansible dbnode -s setup I can see that the ansible_eth0 has a proper value. I don't understand why it is saying that it's undefined.

Any idea how to gather the facts on all machines in the same time while still having the option several tasks/handlers still being done serialized.

标签: loops ansible
1条回答
Melony?
2楼-- · 2019-03-05 18:59

ansible_eth0 fact may be unknown at the time of your task run.

You may want to add fact gathering play at the very top of your playbook:

- hosts: dbnodes
  gather_facts: yes
  tasks:
    - debug: msg="facts gathering"

- hosts: othernodes
  tasks:
    - name: getting dbnodes IP addresses
      ...
查看更多
登录 后发表回答