Ansible loop related issues

。_饼干妹妹 提交于 2019-12-31 05:36:11

问题


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.


回答1:


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
      ...


来源:https://stackoverflow.com/questions/42108649/ansible-loop-related-issues

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!