Skip to content

Instantly share code, notes, and snippets.

@JonTheNiceGuy
Last active January 16, 2019 09:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JonTheNiceGuy/802efcc7462c617f50dd62b50bf027b1 to your computer and use it in GitHub Desktop.
Save JonTheNiceGuy/802efcc7462c617f50dd62b50bf027b1 to your computer and use it in GitHub Desktop.
Learning Azure Resources
---
- 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([]) }} }"
{
"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"}]}
}
}
{
"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"
]
}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment