# Use variables in standard loops

**URL:** <https://forum.ansible.com/t/use-variables-in-standard-loops/19332>\
**Category:** Ansible Project\
**Created:** [September 22, 2015, 9:00pm UTC](https://forum.ansible.com/t/use-variables-in-standard-loops/19332 "2015-09-22T21:00:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vikas\_Kumar](https://avatars.discourse-cdn.com/v4/letter/v/ecb155/32.png) [@Vikas\_Kumar](https://forum.ansible.com/u/Vikas_Kumar)\
**Post date:** [September 22, 2015, 9:00pm UTC](https://forum.ansible.com/t/use-variables-in-standard-loops/19332/1 "2015-09-22T21:00:24Z")

</div>

I am trying to user variables in the below example described [here](http://docs.ansible.com/ansible/playbooks_loops.html#standard-loops).

```auto

- name: add several users 
  user: name={{ item.name }}
  state=present groups={{ item.groups }}
  with_items:
    - { name: 'testuser1', groups: 'wheel' }
    - { name: 'testuser2', groups: 'root' }

```

This works fine, but when I try to infuse variables, it does not works as intended. I am sure, I am doing something wrong.

I’ve defined two lists in my playbook

`  
vars:

- old:
- ens155
- ens160
- new:
- eth0
- eth1  
`

I am trying to make below code work like _ens155 is eth0_ and _ens160 is eth1_, but this code shows this message - _[ens155, ens160] is [eth0, eth1]_

`

- shell: echo {{ item.old }} is {{ item.new }}  
with\_items:
- { old: ‘{{ old }}’, new: ‘{{ new }}’ }  
`

How can I make this work ? If required, I am happy to change the way my variables are defined.

Regards.

---

<div class="post-metadata">

**Author:** ![Alwin\_Schoemaker](https://avatars.discourse-cdn.com/v4/letter/a/ecd19e/32.png) [@Alwin\_Schoemaker](https://forum.ansible.com/u/Alwin_Schoemaker)\
**Post date:** [September 23, 2015, 6:27am UTC](https://forum.ansible.com/t/use-variables-in-standard-loops/19332/2 "2015-09-23T06:27:40Z")

</div>

The injected _old_ and _new_ are injected as an array. Try it like this:

- shell: echo {{ item.old }} is {{ item.new }}  
with\_items:
- { old: ‘{{ old[0] }}’, new: ‘{{ new[0] }}’ }
- { old: ‘{{ old[1] }}’, new: ‘{{ new[1] }}’ }

---

<div class="post-metadata">

**Author:** ![Vikas\_Kumar](https://avatars.discourse-cdn.com/v4/letter/v/ecb155/32.png) [@Vikas\_Kumar](https://forum.ansible.com/u/Vikas_Kumar)\
**Post date:** [September 23, 2015, 6:42am UTC](https://forum.ansible.com/t/use-variables-in-standard-loops/19332/3 "2015-09-23T06:42:44Z")

</div>

Thanks Alwin, the number of elements in the list could vary.

This is what I was looking for.

`

- shell: echo {{ item.0 }} is {{ item.1 }}  
with\_together:
- “{{ old }}”
- “{{ new }}”  
`

Regards.
