# learning ansible's vars

**URL:** <https://forum.ansible.com/t/learning-ansibles-vars/20998>\
**Category:** Ansible Developer\
**Created:** [March 28, 2016, 3:25pm UTC](https://forum.ansible.com/t/learning-ansibles-vars/20998 "2016-03-28T15:25:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jyanga](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jyanga/32/8108_2.png) [@jyanga](https://forum.ansible.com/u/jyanga)\
**Post date:** [March 28, 2016, 3:25pm UTC](https://forum.ansible.com/t/learning-ansibles-vars/20998/1 "2016-03-28T15:25:29Z")

</div>

I run my playbook with the command below.  
ansible-playbook test.yml --limit test

In my test.yml file, I have the following:

---

<div class="post-metadata">

**Author:** ![Rene\_Pijlman](https://avatars.discourse-cdn.com/v4/letter/r/9fc29f/32.png) [@Rene\_Pijlman](https://forum.ansible.com/u/Rene_Pijlman)\
**Post date:** [March 28, 2016, 3:35pm UTC](https://forum.ansible.com/t/learning-ansibles-vars/20998/2 "2016-03-28T15:35:09Z")

</div>

I guess this must be:

line: “^IPADDR={{ item.ip\_add }}”

… since you’re in a with\_items loop. WIth spaces for readability.

---

<div class="post-metadata">

**Author:** ![jyanga](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jyanga/32/8108_2.png) [@jyanga](https://forum.ansible.com/u/jyanga)\
**Post date:** [March 28, 2016, 3:51pm UTC](https://forum.ansible.com/t/learning-ansibles-vars/20998/3 "2016-03-28T15:51:47Z")

</div>

Thank you for the quick response.

I changed it to item.ip\_add and got the error below.

fatal: [server1]: FAILED! =\> {“failed”: true, “msg”: “‘item’ is undefined”}

---

<div class="post-metadata">

**Author:** ![Rene\_Pijlman](https://avatars.discourse-cdn.com/v4/letter/r/9fc29f/32.png) [@Rene\_Pijlman](https://forum.ansible.com/u/Rene_Pijlman)\
**Post date:** [March 28, 2016, 4:23pm UTC](https://forum.ansible.com/t/learning-ansibles-vars/20998/4 "2016-03-28T16:23:23Z")

</div>

Ok, here it is, with a few bugs fixed. It ran without errors in my environment

srv\_lst:

- { host\_name: ‘server1’, ip\_add: ‘10.10.10.1’ }

- { host\_name: ‘server2’, ip\_add: ‘10.1.1.1’ }

- name: change ip address depending on host  
lineinfile:  
dest: /etc/sysconfig/network-scripts/ifcfg-eth0  
regexp: ‘^IPADDR=’  
line: “IPADDR={{item.ip\_add}}”  
when: “ansible\_hostname == item.host\_name”  
with\_items: “{{srv\_lst}}”

Mind the indentation level: when and with\_items allign with the module.

No quotes in srv\_list. It’s a list of dicts, not a list of strings.

Also note that your code matched a line with “IPADDRESS” or similar and replaced it with ^IPADDR with the caret. So I added the ‘=’ to ‘regexp:’ and removed the caret in ‘line:’.

---

<div class="post-metadata">

**Author:** ![jyanga](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jyanga/32/8108_2.png) [@jyanga](https://forum.ansible.com/u/jyanga)\
**Post date:** [March 28, 2016, 4:42pm UTC](https://forum.ansible.com/t/learning-ansibles-vars/20998/5 "2016-03-28T16:42:51Z")

</div>

Thanks, Rene.

Your corrections were spot on. 🙂

Regards,  
j

---

<div class="post-metadata">

**Author:** ![Rene\_Pijlman](https://avatars.discourse-cdn.com/v4/letter/r/9fc29f/32.png) [@Rene\_Pijlman](https://forum.ansible.com/u/Rene_Pijlman)\
**Post date:** [March 28, 2016, 5:09pm UTC](https://forum.ansible.com/t/learning-ansibles-vars/20998/6 "2016-03-28T17:09:50Z")

</div>

You’re welcome.

I just realized we should have been in group ‘Ansible Project’.‘Ansible Development’ is for people working _on_ Ansible, rather than _with_

---

<div class="post-metadata">

**Author:** ![jyanga](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jyanga/32/8108_2.png) [@jyanga](https://forum.ansible.com/u/jyanga)\
**Post date:** [March 28, 2016, 5:33pm UTC](https://forum.ansible.com/t/learning-ansibles-vars/20998/7 "2016-03-28T17:33:31Z")

</div>

I apologize. Moving to the “Ansible Project” group. 🙂

Regards,  
j
