0

I have retrieved data from Azure using the azure_rm_*_facts modules, and have ended up with some data (after tweaking) like this:

  {
    'nics': [
      {
        'fw01port01': {
          'ip': '10.10.10.4', 
          'tags': {
            'firewall': 'true',
            'sshserver': 'true'
          }
        }
      },
      {
        'fw01port02': {
          'ip': '10.10.20.4', 
          'tags': {
            'firewall': 'true'
          }
        }
      },
      {
        'host01port02': {
          'ip': '10.10.20.5', 
          'tags': {
            'firewall': 'false',
            'webserver': 'true',
            'sshserver': 'true'
          }
        }
      }
    ],
    'vms': [
      {
        'fw01': {
          'tags': {
            'production': 'true'
          },
          'nics': [
            'fw01port01',
            'fw01port02'
          ]
        }
      },
      {
        'host01': {
          'tags': {
            'production': 'true'          
          },
          'nics': [
            'host01port02'
          ]
        }
      }      
    ]
  }

I want to be able to iterate over tags, like this:

  'tags': {
    'production': {'true': [{'fw01port01': '10.10.10.4'}, {'fw01port02': '10.10.20.4'}, {'host01port02': '10.10.20.5'}]},
    'firewall': {'true': [{'fw01port01': '10.10.10.4'}, {'fw01port02': '10.10.20.4'}], 'false': [{'host01port02': '10.10.20.5'}]},
    'sshserver': {'true': [{'fw01port01': '10.10.10.4'}, {'host01port02': '10.10.20.5'}]},
    'webserver': {'true': [{'host01port02': '10.10.20.5'}]}
  }

Ideally, I'd also like to be able to inject the name of the resource group I got those hosts from.

This output was (loosely) calculated from this script:

---
- hosts: localhost
  gather_facts: false
  vars:
    ext_rg: "{{ lookup('env', 'rg') }}"
    allnics: []
    allvms: []

  tasks:
  - name: Learn Available Resource Groups
    azure_rm_resourcegroup_facts:
    register: resourcegroups

  - name: Set RG from Env
    set_fact:
      rgs: "['{{ ext_rg }}']"
    when: ext_rg is defined and ext_rg != ''

  - name: Parse Available Resource Groups
    set_fact:
      rgs: "{{ rgs | default([]) + [ item.name ] }}"
    loop: "{{ resourcegroups.ansible_facts.azure_resourcegroups }}"
    loop_control:
      label: "{{ item.name | default('None') }}"
    when: (ext_rg is not defined or ext_rg == '') and item.name is defined

  - name: Learn Available NICs
    azure_rm_networkinterface_facts:
      resource_group: "{{ item }}"
    register: networkinterface
    loop: "{{ rgs }}"

  - name: Learn Available VMs
    azure_rm_virtualmachine_facts:
      resource_group: "{{ item }}"
    register: virtualmachines
    loop: "{{ rgs }}"

  - name: Parse NICs
    set_fact:
      allnics: "{{ allnics | default([]) + item.ansible_facts.azure_networkinterfaces }}"
    loop: "{{ networkinterface.results }}"
    loop_control:
      label: "{{ item.item | default('None') }}"
    when: item.ansible_facts is defined and item.ansible_facts.azure_networkinterfaces is defined and item.ansible_facts.azure_networkinterfaces != []

  - name: Extract NIC data into individual NIC records
    set_fact:
      nics: "{{ nics | default([]) + [{item.name: {'ip': item.properties.ipConfigurations[0].properties.privateIPAddress | default([]), 'tags': item.tags | default([])} }] }}"
    loop: "{{ allnics }}"
    loop_control:
      label: "{{ item.name | default('None') }}"

  - name: Parse VMs
    set_fact:
      allvms: "{{ allvms | default([]) + item.vms }}"
    loop: "{{ virtualmachines.results }}"
    loop_control:
      label: "{{ item.item | default('None') }}"
    when: item.vms is defined and item.vms != []

  - name: Extract VM data into individual VM records
    set_fact:
      vms: "{{ vms | default([]) + [{item.name: {'tags': item.tags | default([]), 'networkInterfaceNames': item.network_interface_names}}] }}"
    loop: "{{ allvms }}"
    loop_control: 
      label: "{{ item.name | default('None') }}"

  - debug: 
msg: "{'nics': {{ nics | default([]) }}, 'vms': {{ vms | default([]) }} }"

An I going to be able to make this work the way I want with native Ansible modules (and if so, how), or will I need to write a custom plugin or filter to make it work?

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.