Strangely, if I use ;

 ip: "{% for host in *play_hosts* %}{{ 
hostvars[host]['ansible_default_ipv4']['address'] }}{% if not loop.last 
%},{% endif %}{% endfor %}"

This all works across both tasks (or both plays, if split it into two 
plays) whereas using "groups['all']" fails on the second task

On Tuesday, 28 July 2015 17:28:48 UTC+1, KSS wrote:
>
> Looks like it's better to use set_fact here (well, that method works). It 
> would still be good to understand why the above fails though
>
> On Tuesday, 28 July 2015 17:13:40 UTC+1, KSS wrote:
>>
>> 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]
>>
>> Enter code here...
>>
>>
>> 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?
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/8ceafebf-b595-4cb2-b098-d103463fd3b7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to