Skip to content

Instantly share code, notes, and snippets.

@jctanner
Created December 19, 2018 00:40
Show Gist options
  • Save jctanner/f38f916dd5d027ce4e0d320c119e5632 to your computer and use it in GitHub Desktop.
Save jctanner/f38f916dd5d027ce4e0d320c119e5632 to your computer and use it in GitHub Desktop.
rhel filters
[22/1139]
TASK [set_fact] ************************************************************************************************
task path: /home/jtanner/workspace/issues/AP-RHEL_FACT_FILTER/site.yml:6
ok: [el7host] => {
"ansible_facts": {
"device_by_pciid1": {
"virtio2": "eth0",
"virtio3": "eth1"
},
"device_by_pciid2": {
"virtio2": "eth0",
"virtio3": "eth1"
},
"macaddress_by_pciid1": {
"virtio2": "52:54:00:4e:f1:54",
"virtio3": "52:54:00:6e:be:b2"
},
"macaddress_by_pciid2": {
"virtio2": "52:54:00:4e:f1:54",
"virtio3": "52:54:00:6e:be:b2"
}
},
"changed": false
}
#!/usr/bin/env python
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
def keyvalmap(*args, **kwargs):
key = kwargs['key']
value = kwargs['value']
res = {}
for item in args[0]:
v = item.get('value')
if v is None:
continue
if not isinstance(v, dict):
continue
if key not in v:
continue
if value in v:
res[v[key]] = v[value]
return res
class FilterModule(object):
def filters(self):
return {
'keyvalmap': keyvalmap
}
- hosts: el7host
connection: ssh
gather_facts: True
user: root
tasks:
- set_fact:
device_by_pciid1: "{{ ansible_facts | dict2items | json_query('[?value.pciid].value.{key: pciid, value: device}') | items2dict }}"
device_by_pciid2: "{{ ansible_facts | dict2items | keyvalmap(key='pciid', value='device') }}"
macaddress_by_pciid1: "{{ ansible_facts | dict2items | json_query('[?value.pciid].value.{key: pciid, value: macaddress}') | items2dict }}"
macaddress_by_pciid2: "{{ ansible_facts | dict2items | keyvalmap(key='pciid', value='macaddress') }}"
- debug: var=device_by_pciid1
- debug: var=device_by_pciid2
- debug: var=macaddress_by_pciid1
- debug: var=macaddress_by_pciid2
- assert:
that:
- "device_by_pciid1 == device_by_pciid2"
- "macaddress_by_pciid1 == macaddress_by_pciid2"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment