CEamonn
CEamonn

Reputation: 925

Loop through a list in ansible two extract values

I have a playbook that gets values from network devices and creates a fact for them, the data looks like this

"response": [
        {
            "address": "5555.6666.7777.8888", 
            "age": "0", 
            "interface": "Vlan4050, Ethernet23", 
            "mac": "324c.23d7.7235"
        }, 
        {
            "address": "111.222.333.444", 
            "age": "0", 
            "interface": "Vlan4051, Ethernet24", 
            "mac": "63g.aed6.892d"
        }
    ]

I create a fact to get the addresses from each object

- name: set list of addresses
  set_fact:
    address: "{{ arp_arista.response | map(attribute='address') | list }}"

I then want to debug a message with each address

- name: debug test
  debug: 
    msg: "This is the IP: {{ item }}"
  loop:
    - "{{ address }}"

I expected this to output

This is the IP: 5555.6666.7777.8888
This is the IP: 111.222.333.444

But instead it outputs

This is the IP: [u'5555.6666.7777.8888', u'111.222.333.444']

Is there a way to loop and print each address separately to be used?

Upvotes: 1

Views: 835

Answers (1)

Vladimir Botka
Vladimir Botka

Reputation: 68104

Q: "Is there a way to loop and print each address separately to be used?"

A: Yes. Iterate the items in the list address

  loop: "{{ address }}"


Explanation of the problem

The loop in the question iterates explicit list of one item wich is the list address itself

  loop:
    - "{{ address }}"

Upvotes: 1

Related Questions