Skip to content

Instantly share code, notes, and snippets.

@3hedgehogs
Created May 17, 2022 16:10
Show Gist options
  • Save 3hedgehogs/320b58e25d2570c68ddd0cdd2ce0530b to your computer and use it in GitHub Desktop.
Save 3hedgehogs/320b58e25d2570c68ddd0cdd2ce0530b to your computer and use it in GitHub Desktop.
Runn ansible module from python script and print cache
#!/usr/bin/env python
import io
import os
import json
import sys
import ansible_runner
def finished_callback(runner):
print(runner.status)
print(runner.config.ident)
print(runner.get_fact_cache('localhost'))
fact_cache = os.path.join(runner.config.fact_cache, 'ansible_facts' + 'localhost')
print(fact_cache)
if os.path.exists(fact_cache):
with open(fact_cache) as f:
print(json.loads(f.read()))
r = ansible_runner.run(
private_data_dir='/tmp',
host_pattern='localhost',
module='setup',
module_args='filter=ansible_memtotal_mb,ansible_processor_nproc',
# finished_callback=finished_callback,
fact_cache_type='jsonfile'
)
print(r.status)
print(r.get_fact_cache('ansible_facts' + 'localhost'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment