Hi,
I'm having a problem with a particular variable.
I'm hoping figuring out the problem below will help me understand the issue with a larger playbook.
# ansible --version
ansible 1.9.2
A simple playbook (I need the list of ip addresses as it is passed to an api in one request);
---
- hosts: all
gather_facts: true
vars:
ip: "{% for host in groups['all'] %}{{ hostvars[host]['ansible_default_ipv4']['address'] }}{% if not loop.last %},{% endif %}{% endfor %}"
tasks:
- name: Debug
debug: msg="IPaddresses={{ ip }}"
delegate_to: localhost
run_once: true
tags: first-play
- hosts: all
gather_facts: true
vars:
ip: "{% for host in groups['all'] %}{{ hostvars[host]['ansible_default_ipv4']['address'] }}{% if not loop.last %},{% endif %}{% endfor %}"
tasks:
- name: Debug 2
debug: msg="2nd list of IPaddresses={{ ip }}"
delegate_to: localhost
run_once: true
tags: second-play
------------- End of playbook -------------------
When the above is run one task at a time (i.e using the tags) it works fine;
# ansible-playbook -i 'host1,host2' ./test-ipvar.yml --tags first-play
PLAY [all] ********************************************************************
GATHERING FACTS ***************************************************************
ok: [host1]
ok: [host2]
TASK: [Debug] *****************************************************************
ok: [host1 -> localhost] => {
"msg": "IPaddresses=10.0.0.1,10.0.0.2"
}
PLAY RECAP ********************************************************************
host2 : ok=2 changed=0 unreachable=0 failed=0
host1 : ok=2 changed=0 unreachable=0 failed=0
# ansible-playbook -i 'host1,host2' ./test-ipvar.yml --tags second-play
PLAY [all] ********************************************************************
GATHERING FACTS ***************************************************************
ok: [host1]
ok: [host2]
TASK: [Debug 2] ***************************************************************
ok: [host1 -> localhost] => {
"msg": "2nd list of IPaddresses=10.0.0.1,10.0.0.2"
}
PLAY RECAP ********************************************************************
host2 : ok=2 changed=0 unreachable=0 failed=0
host1 : ok=2 changed=0 unreachable=0 failed=0
However, running the playbook, we get an error for the second play although that variable is clearly available before;
# ansible-playbook -i 'host1,host2' ./test-ipvar.ymlEnter code here...
PLAY [all] ********************************************************************
GATHERING FACTS ***************************************************************
ok: [host1]
ok: [host2]
TASK: [Debug] *****************************************************************
ok: [host1 -> localhost] => {
"msg": "IPaddresses=10.0.0.1,10.0.0.2"
}
PLAY [all] ********************************************************************
GATHERING FACTS ***************************************************************
ok: [host1]
ok: [host2]
TASK: [Debug 2] ***************************************************************
fatal: [host1 -> localhost] => One or more undefined variables: 'dict' object has no attribute 'ansible_default_ipv4'
FATAL: all hosts have already failed -- aborting
PLAY RECAP ********************************************************************
to retry, use: --limit @/root/test-ipvar.retry
host2 : ok=3 changed=0 unreachable=0 failed=1
host1 : ok=3 changed=0 unreachable=0 failed=1
Even putting both tasks in the same play results in only the first play being successful.
Does anyone have any ideas why it behaves this way?