# Problem with var within a playbook

**URL:** <https://forum.ansible.com/t/problem-with-var-within-a-playbook/18877>\
**Category:** Ansible Project\
**Created:** [July 28, 2015, 4:13pm UTC](https://forum.ansible.com/t/problem-with-var-within-a-playbook/18877 "2015-07-28T16:13:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![KSS](https://avatars.discourse-cdn.com/v4/letter/k/a587f6/32.png) [@KSS](https://forum.ansible.com/u/KSS)\
**Post date:** [July 28, 2015, 4:13pm UTC](https://forum.ansible.com/t/problem-with-var-within-a-playbook/18877/1 "2015-07-28T16:13:40Z")

</div>

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);

---

<div class="post-metadata">

**Author:** ![KSS](https://avatars.discourse-cdn.com/v4/letter/k/a587f6/32.png) [@KSS](https://forum.ansible.com/u/KSS)\
**Post date:** [July 28, 2015, 4:28pm UTC](https://forum.ansible.com/t/problem-with-var-within-a-playbook/18877/2 "2015-07-28T16:28:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![KSS](https://avatars.discourse-cdn.com/v4/letter/k/a587f6/32.png) [@KSS](https://forum.ansible.com/u/KSS)\
**Post date:** [July 29, 2015, 1:36pm UTC](https://forum.ansible.com/t/problem-with-var-within-a-playbook/18877/3 "2015-07-29T13:36:58Z")

</div>

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
