Skip to content

Instantly share code, notes, and snippets.

@cidrblock
Last active July 10, 2019 20:28
Show Gist options
  • Save cidrblock/52439c11209d8a7f0aa2e513edfee3b1 to your computer and use it in GitHub Desktop.
Save cidrblock/52439c11209d8a7f0aa2e513edfee3b1 to your computer and use it in GitHub Desktop.
Ansible runner demo
""" ansible runner demo
"""
from functools import reduce
import operator
import yaml
import ansible_runner
def dict_get(data_dict, keypath):
map_list = keypath.split('.')
try:
return reduce(operator.getitem, map_list, data_dict)
except KeyError:
return None
INVENTORY = {
"all": {
"hosts": {
"eos101": {
"ansible_network_os": "eos",
"ansible_user": "admin",
"ansible_password": "password",
"ansible_become_pass": "password",
"ansible_become": True,
"ansible_become_method": "enable",
"ansible_facts_modules": "eos_facts",
"ansible_connection": "network_cli"
}
}
}
}
PLAYBOOK = {
"hosts": "all",
"gather_facts": False,
"tasks": [
{"eos_ospf": {'state': 'deleted'}},
{"eos_ospf": {
"config": {
"processes": [{
"id": 200,
"vrf": "blue",
"auto_cost": {
"reference_bandwidth": 1000
}
}]
}
}},
{"eos_ospf": {'state': 'deleted'}},
]
}
ROBJ = {"playbook": [PLAYBOOK],
"project_dir": './',
"inventory": INVENTORY,
"quiet": False}
def main():
""" runnerit
"""
robj = ansible_runner.run(**ROBJ)
for event in robj.events:
if event['event'] in ['runner_on_ok']:
for resval in ['changed', 'before', 'after']:
result = dict_get(event, f"event_data.res.{resval}")
if result is not None:
print(f"****** {resval}")
print(yaml.dump(result, default_flow_style=False))
if __name__ == '__main__':
main()
@cidrblock
Copy link
Author

cidrblock commented Jul 10, 2019

OUTPUT


****** changed
false
...

****** before
{}

****** changed
true
...

****** before
{}

****** after
processes:
- auto_cost:
    reference_bandwidth: 1000
  id: 200
  vrf: blue

****** changed
true
...

****** before
processes:
- auto_cost:
    reference_bandwidth: 1000
  id: 200
  vrf: blue

****** after
{}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment