Last active
July 23, 2016 14:53
-
-
Save minusdavid/2774a8d6df5ded46b1b63e05f214da01 to your computer and use it in GitHub Desktop.
Custom Ansible module to parse redhat.repo (or any ConfigParser compatible file) and return the file as facts (as per the advice in http://docs.ansible.com/ansible/developing_modules.html), so that they can be used to run subscription-manager in an idempotent manner
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- site_facts: src=/etc/yum.repos.d/redhat.repo | |
- name: Enable Red Hat repositories via subscription-manager | |
command: subscription-manager repos --enable {{ item }} | |
when: site_facts[item]['enabled'] is defined and site_facts[item]['enabled'] != "1" | |
with_items: | |
- rhel-6-server-optional-rpms | |
- rhel-6-server-extras-rpms |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
# (c) 2016, David Cook <[email protected]> | |
import ConfigParser | |
def get_site_facts(module): | |
setup_options = dict(module_setup=True) | |
src = module.params['src'] | |
setup_result = { 'ansible_facts': { 'site_facts' : {} } } | |
Config = ConfigParser.ConfigParser() | |
Config.read(src) | |
sections = Config.sections() | |
for section in sections: | |
setup_result['ansible_facts']['site_facts'][section] = {} | |
options = Config.options(section) | |
for option in options: | |
value = Config.get(section, option) | |
setup_result['ansible_facts']['site_facts'][section][option] = value | |
return setup_result | |
def main(): | |
module = AnsibleModule( | |
argument_spec = dict( | |
src=dict(required=True,), | |
), | |
supports_check_mode = True, | |
) | |
data = get_site_facts(module) | |
module.exit_json(**data) | |
# import module snippets | |
from ansible.module_utils.basic import * | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment