Skip to content

Instantly share code, notes, and snippets.

@jlaska
Created June 11, 2013 18:03
Show Gist options
  • Save jlaska/5759226 to your computer and use it in GitHub Desktop.
Save jlaska/5759226 to your computer and use it in GitHub Desktop.
ansible/site_facts Make yum repository configuration available to ansible playbooks using the following site_facts.
#!/usr/bin/env python
import json
import os
import yum
def main():
module = AnsibleModule(
argument_spec = dict(
yum_conf=dict(required=False,
default='/etc/yum.conf'),
)
)
yum_conf = module.params['yum_conf']
yb = yum.YumBase()
if not yb.setCacheDir(force=True, reuse=False):
module.fail_json(msg="Can't create a tmp. cachedir.")
repo_dict = dict()
for id, repo in yb.repos.repos.items():
repo_dict[id] = dict()
for attr in ['name', 'id', 'baseurl', 'gpgcheck', 'gpgfile', 'enabled', 'repofile']:
if hasattr(repo, attr):
repo_dict[id][attr] = getattr(repo, attr)
results = dict(ansible_facts=dict(yum_repos=repo_dict))
module.exit_json(**results)
# include magic from lib/ansible/module_common.py
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment