Problem with var within a playbook

34 views
Skip to first unread message

KSS

unread,
Jul 28, 2015, 12:13:40 PM7/28/15
to Ansible Project
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?

KSS

unread,
Jul 28, 2015, 12:28:48 PM7/28/15
to Ansible Project, kss...@gmail.com
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

KSS

unread,
Jul 29, 2015, 9:36:59 AM7/29/15
to Ansible Project, kss...@gmail.com
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
Reply all
Reply to author
Forward
0 new messages