Skip to content

Instantly share code, notes, and snippets.

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/3bb8b012ef0c29c07a02d2a81d3cafd6 to your computer and use it in GitHub Desktop.
Save JonTheNiceGuy/3bb8b012ef0c29c07a02d2a81d3cafd6 to your computer and use it in GitHub Desktop.
Manipulating yaml tree data using Ansible

Manipulating yaml tree data using Ansible

I sometimes need to convert from a dictionary tree (like in data.yml) to a specific json formatted output file using Ansible.

In this case, I've used a combination of set_fact, a for loop and a combine filter to build my output. I'm also using a regex_search to make sure I'm only getting records where the data I'm interested in is there!

Hope it's useful to people!

---
resource_groups:
vnetrg:
vnet:
transitvnet:
supernet:
- "192.0.2.0/25"
subnet:
ingresssubnet:
subnet: "192.0.2.0/27"
egresssubnet:
subnet: "192.0.2.32/27"
appgwsubnet:
subnet: "192.0.2.64/27"
udr: "spokert"
gwsubnet:
gwsubnet:
subnet: "192.0.2.96/27"
udr: "spokert"
webvnet:
supernet:
- "192.0.2.128/27"
websubnet:
subnet: "192.0.2.128/27"
udr: "spokert"
dbvnet:
supernet:
- "192.0.2.192/27"
dbsubnet:
subnet: "192.0.2.192/27"
udr: "spokert"
udr:
spokert:
Internet:
prefix: "0.0.0.0/0"
gateway: "192.0.2.62"
fwrg:
storage:
- disks
lb:
webrg:
storage:
- disks
dbrg:
storage:
- disks
{
"transitvnet": {
"supernet": ["192.0.2.0/25"],
"subnet": {"ingresssubnet": {"subnet": "192.0.2.0/27"}, "egresssubnet": {"subnet": "192.0.2.32/27"}, "appgwsubnet": {"subnet": "192.0.2.64/27", "udr": "spokert"}},
"gwsubnet": {"gwsubnet": {"subnet": "192.0.2.96/27", "udr": "spokert"}},
"rg": "vnetrg"
},
"webvnet": {
"supernet": ["192.0.2.128/27"],
"subnet": {"websubnet": {"subnet": "192.0.2.128/27", "udr": "spokert"}},
"rg": "vnetrg"
},
"dbvnet": {
"supernet": ["192.0.2.192/27"],
"subnet": {"dbsubnet": {"subnet": "192.0.2.192/27", "udr": "spokert"}},
"rg": "vnetrg"
}
}
- set_fact:
vnets: "{ {% for vnet in item.value.vnet %}'{{ vnet }}': {{ item.value.vnet[vnet]|combine({'rg': item.key}) }}{% if not loop.last %}, {% endif %}{% endfor %}{% if vnets|default({})|length > 0 %}, {% for vnet in vnets|default({}) %}'{{ vnet }}': {{ vnets[vnet] }}{% if not loop.last %}, {% endif %}{% endfor %}{% endif %} }"
loop: "{{ resource_groups|dict2items }}"
loop_control:
label: "{{ item.key }}"
when: item.value|string|regex_search("'vnet':")|type_debug != 'NoneType'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment