commit 6f6f1eae4bb11bbd0d325e343db13d804190bcb6 Author: Sven Velt Date: Wed Dec 22 10:48:29 2021 +0100 Inital commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7b9a7c6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,164 @@ +# ---> Ansible +*.retry + +# ---> Vim +# Swap +[._]*.s[a-v][a-z] +!*.svg # comment out if you don't need vector files +[._]*.sw[a-p] +[._]s[a-rt-v][a-z] +[._]ss[a-gi-z] +[._]sw[a-p] + +# Session +Session.vim +Sessionx.vim + +# Temporary +.netrwhist +*~ +# Auto-generated tag files +tags +# Persistent undo +[._]*.un~ + +# ---> Python +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + diff --git a/README.md b/README.md new file mode 100644 index 0000000..dc1c2a0 --- /dev/null +++ b/README.md @@ -0,0 +1,20 @@ +# module_read_ini + +Ansible module to read whole ini file + +## Testing Example + + % python3 ./read_ini.py +# 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()