looping through azure_rm_networkinterface_facts

16 views
Skip to first unread message

Gregory Edigarov

unread,
Oct 11, 2019, 11:46:18 AM10/11/19
to Ansible Project
Hello,

Supposing I retrive information about interfaces like  this:

 - name: Get network interface details newly created hosts
    azure_rm_networkinterface_facts:
      resource_group: "{{ rg_name }}"
      name: "{{ item }}01"
    register: azure_networkinterfaces
    loop:
      -  "{{ env }}-sw1-vm"
      -  "{{ env }}-sw2-vm"
      -  "{{ env }}-sw3-vm"


how can I get their private IP's in a loop?

i.e. I want then add them to dns zone looping through something like: 

    loop:
      - sw1
      - sw2
      - sw3

or something like that, and in the zone there should be 
     sw1 in a 10.0.0.4 
     sw2 in a 10.0.0.5
     sw3 in a 10.0.0.6



so i only need the idea of how to loop through such an object

thank you. 

Gregory Edigarov

unread,
Oct 14, 2019, 3:45:37 PM10/14/19
to Ansible Project
ok, so I am answering to myself:

the key is in these lines

  - name: setting facts for hostnames and ips
    set_fact: 
      names_ips: "{{ names_ips|default([]) + [{'name': item.ansible_facts.azure_networkinterfaces[0].name|regex_replace('.*-(.+)-.*','\\1'),
                                               'ip': item.ansible_facts.azure_networkinterfaces[0].properties.ipConfigurations[0].properties.privateIPAddress }] }}"
    loop: "{{ azure_networkinterfaces.results }}"

took me four days figuring that names_ips should be created as |default([]) first

Gregory Edigarov

unread,
Oct 14, 2019, 3:57:41 PM10/14/19
to Ansible Project
the way of doing this more efficienlty is still appreciated
Reply all
Reply to author
Forward
0 new messages