Discussion:
[ansible-project] Problem with var within a playbook
KSS
2015-07-28 16:13:40 UTC
Permalink
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 ansible-project+***@googlegroups.com.
To post to this group, send email to ansible-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/57692d0a-6745-4114-9a39-974d87540064%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
KSS
2015-07-28 16:28:47 UTC
Permalink
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
Post by KSS
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
ip: "{% for host in groups['all'] %}{{
hostvars[host]['ansible_default_ipv4']['address'] }}{% if not loop.last
%},{% endif %}{% endfor %}"
- name: Debug
debug: msg="IPaddresses={{ ip }}"
delegate_to: localhost
run_once: true
tags: first-play
- hosts: all
gather_facts: true
ip: "{% for host in groups['all'] %}{{
hostvars[host]['ansible_default_ipv4']['address'] }}{% if not loop.last
%},{% endif %}{% endfor %}"
- 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
********************************************************************
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 ansible-project+***@googlegroups.com.
To post to this group, send email to ansible-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/a556cc2b-8057-4466-a425-eb7db5c0f95b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
KSS
2015-07-29 13:36:58 UTC
Permalink
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
Post by KSS
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
Post by KSS
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
ip: "{% for host in groups['all'] %}{{
hostvars[host]['ansible_default_ipv4']['address'] }}{% if not loop.last
%},{% endif %}{% endfor %}"
- name: Debug
debug: msg="IPaddresses={{ ip }}"
delegate_to: localhost
run_once: true
tags: first-play
- hosts: all
gather_facts: true
ip: "{% for host in groups['all'] %}{{
hostvars[host]['ansible_default_ipv4']['address'] }}{% if not loop.last
%},{% endif %}{% endfor %}"
- 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
********************************************************************
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 ansible-project+***@googlegroups.com.
To post to this group, send email to ansible-***@googlegroups.com.
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.
Loading...