# Ansible loop query

**URL:** <https://forum.ansible.com/t/ansible-loop-query/27496>\
**Category:** Ansible Project\
**Created:** [May 10, 2018, 4:57am UTC](https://forum.ansible.com/t/ansible-loop-query/27496 "2018-05-10T04:57:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aman\_Kaushik](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@Aman\_Kaushik](https://forum.ansible.com/u/Aman_Kaushik)\
**Post date:** [May 10, 2018, 4:57am UTC](https://forum.ansible.com/t/ansible-loop-query/27496/1 "2018-05-10T04:57:38Z")

</div>

Hello Team,

Below is my playbook & variable for Ansible. Could you please check and help with my query.

**Playbook**

- name: GIN\_MNG Routing

lineinfile:

line: set routing-instances GIN\_MNG routing-options static route {{ item.dst }} next-hop {{ item.nhp }} {{item.action | default()}}

line: set routing-instances GIN\_MNG routing-options static route {{ item.dst }} {{item.option | default()}}

path: config/{{ inventory\_hostname }}\_common-config.conf

with\_items: “{{ routing.GIN\_MNG.srouting }}”

**Variable**

routing:

srouting:

- { dst : 0.0.0.0/0, nhp: 172.19.66.65 }

- { dst : 10.227.222.0/28, nhp: 192.168.1.10, action: retain }

- { dst : 10.227.222.16/28, nhp: 192.168.1.10, option: discard }

**Issue** : This loop runs only on line2 \<\<\<set routing-instances GIN\_MNG routing-options static route {{ item.dst }} {{item.option | default()}} \>\>\> and yields below result.

**Result**

set routing-instances GIN\_MNG routing-options static route 0.0.0.0/0

set routing-instances GIN\_MNG routing-options static route 10.227.222.0/28

set routing-instances GIN\_MNG routing-options static route 10.227.222.16/28 discard

**Expected** : I want to run loop on both lines and expects below result. I don’t want to create new task for each line since my variable is common.

**Expected Result**

set routing-instances GIN\_MNG routing-options static route 0.0.0.0/0 next-hop 172.19.66.65

set routing-instances GIN\_MNG routing-options static route 10.227.222.0/28 next-hop 192.168.1.10 retain

set routing-instances GIN\_MNG routing-options static route 10.227.222.16/28 next-hop 192.168.1.10

set routing-instances GIN\_MNG routing-options static route 0.0.0.0/0

set routing-instances GIN\_MNG routing-options static route 10.227.222.0/28

set routing-instances GIN\_MNG routing-options static route 10.227.222.16/28 discard

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [May 10, 2018, 5:59am UTC](https://forum.ansible.com/t/ansible-loop-query/27496/2 "2018-05-10T05:59:05Z")

</div>

The second line is overwriting the first line, you can only have one line per lineintask task.  
So you need to split it to one task per line.

If you only want one loop, you can loop include\_tasks/import\_task with the two lineinfile task in that file.
