0

I am trying to run a command in Ansible so as to find the neighbors in my network:

- name: Get neighbors
  junos_rpc:
    rpc: "get-lldp-interface-neighbors"
    output: 'xml'
    args:
      interface_device: A
  register: net_topology

So my problem comes when in this task I need to loop over a list and give another arg for the interface_device and register the result also in another variable 'net_topology' every time.

Xenia Ioannidou
  • 67
  • 1
  • 1
  • 8

2 Answers2

2
- name: Get neighbors
  junos_rpc:
    rpc: "get-lldp-interface-neighbors"
    output: 'xml'
    args:
      interface_device: "{{ item }}"
  loop:
    - A
    - B
    - C
  register: net_topology

Once you modify your task like this, it will play three times: once for each element in my example loop. The variable item will get the value of the current element in the list.

You do not need to change your register variable: it will automatically be modified as explained in the ansible documentation:

When you use register with a loop, the data structure placed in the variable will contain a results attribute that is a list of all responses from the module. This differs from the data structure returned when using register without a loop

So you can inspect all your results in a subsequent task by looping over net_topology.results which contains the list of individual results.

Zeitounator
  • 38,476
  • 7
  • 53
  • 66
  • I also have a 2nd task and in that i am doing a loop like this: `- { path: "/rpc-reply/name", string: "{{ net_topology.results[].xml }}" } `.. So in the net_topology.results i want to get the field 'xml' from every object..But what i am doing is wrong.. i tried also this: "{{ net_topology.results[*].xml }}" but i am again wrong...how do i get from all results the item xml ? @Zeitounator – Xenia Ioannidou Apr 27 '20 at 22:02
  • Loop on `net_topology.results`, what you want to get will be in `item.xml`. – Zeitounator Apr 28 '20 at 10:35
0

Actually i did something similar with the above, but i just passed my list with a different way:

- name: building network topology
  junos_rpc:
    rpc: "get-lldp-interface-neighbors"
    output: 'xml'
    args:
      interface_device: "{{item}}"
  loop:
    "{{my_list}}"
  register: net_topology

And this is actually the same as doing this also:

- name: building network topology
  junos_rpc:
    rpc: "get-lldp-interface-neighbors"
    output: 'xml'
    args:
      interface_device: "{{item}}"
  with_items:
    "{{my_list}}"
  register: net_topology

I must say that my initial mistake was the identation of the loop, because it was placed inside the junos_rpc and by doing this I could not get any result !!!

Xenia Ioannidou
  • 67
  • 1
  • 1
  • 8