Ansible loop query

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

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.