#!/usr/bin/python # Copyright: (c) 2020-, Sven Velt # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) from __future__ import (absolute_import, division, print_function) __metaclass__ = type DOCUMENTATION = r''' --- module: read_ini short_description: Read INI version_added: "0.1.0" description: Read INI file (and store) options: requirements: author: - Sven Velt ''' EXAMPLES = r''' ''' RETURN = r''' ''' from ansible.module_utils.basic import AnsibleModule import os from configparser import ConfigParser def run_module(): module_args = dict( filename = dict(type='str', required=True), ) result = dict( changed = False, content = dict(), ) module = AnsibleModule( argument_spec = module_args, supports_check_mode = True, ) if not os.access(module.params['filename'], os.R_OK): module.fail_json( msg = 'Could not read `%s`' % module.params['filename'] ) inifile = ConfigParser(interpolation=None) try: inifile.read(module.params['filename']) except IOError as e: module.fail_json( msg = 'Could not read ini file `%s`, because: %s' % (module.params['filename'], e) ) for section in inifile.sections(): new_section = dict() for option in inifile.options(section): new_section[option] = inifile.get(section, option) result['content'][section] = new_section module.exit_json(**result) def main(): run_module() if __name__ == '__main__': main()