Ansible loop query

25 views
Skip to first unread message

Aman Kaushik

unread,
May 10, 2018, 12:57:38 AM5/10/18
to Ansible Project

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

Kai Stian Olstad

unread,
May 10, 2018, 1:59:22 AM5/10/18
to ansible...@googlegroups.com
On 10.05.2018 06:57, Aman Kaushik wrote:
> *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.

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.


--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages