tech:ansible_inventory_script_-_inventaire_dynamique_3
Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentesRévision précédente | |||
| tech:ansible_inventory_script_-_inventaire_dynamique_3 [2025/06/02 15:26] – Jean-Baptiste | tech:ansible_inventory_script_-_inventaire_dynamique_3 [2026/06/07 19:12] (Version actuelle) – modification externe 127.0.0.1 | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| + | < | ||
| + | {{tag> | ||
| + | |||
| + | # Ansible inventaire script - inventaire dynamique vers statique - Conversion JSON YAML INI | ||
| + | |||
| + | Voir : | ||
| + | * [[notes_ansible-inventory]] | ||
| + | * https:// | ||
| + | |||
| + | |||
| + | ## INI vers JSON | ||
| + | |||
| + | Pour convertir un inventaire Ansible INI vers JSON | ||
| + | ~~~bash | ||
| + | ansible-inventory -i inventory.ini --list > inventory.json | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | ## INI vers YAML | ||
| + | |||
| + | Pour convertir un inventaire Ansible de INI vers YAML | ||
| + | ~~~bash | ||
| + | ansible-inventory -i inventory.ini -y --list > inventory.yml | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | ## JSON vers YAML | ||
| + | |||
| + | `cat.py` | ||
| + | ~~~python | ||
| + | #! / | ||
| + | |||
| + | import os | ||
| + | |||
| + | filename = os.environ[" | ||
| + | |||
| + | with open(filename, | ||
| + | print(f.read()) | ||
| + | ~~~ | ||
| + | |||
| + | ~~~bash | ||
| + | env FILENAME=inventaire.json ansible-inventory -i cat.py -y --list> inventaire.yml | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | ## JSON vers INI | ||
| + | |||
| + | Reformater l' | ||
| + | ~~~bash | ||
| + | env FILENAME=inventaire.json ansible-inventory -i cat.py --list> inventaire2.json | ||
| + | ~~~ | ||
| + | |||
| + | Script de conversion json_to_ini.py | ||
| + | |||
| + | `json_to_ini.py` | ||
| + | ~~~python | ||
| + | #! / | ||
| + | |||
| + | import sys | ||
| + | import json | ||
| + | import configparser | ||
| + | |||
| + | import argparse | ||
| + | |||
| + | parser = argparse.ArgumentParser( | ||
| + | epilog=""" | ||
| + | JSON inventory must be in ansible-inventory format. | ||
| + | """ | ||
| + | ) | ||
| + | parser.add_argument(" | ||
| + | parser.add_argument(" | ||
| + | args = parser.parse_args() | ||
| + | |||
| + | |||
| + | def convert_json2ini(): | ||
| + | config_object = configparser.ConfigParser(delimiters=" | ||
| + | |||
| + | with open(args.i, | ||
| + | python_dictionary = json.load(json_file) | ||
| + | |||
| + | python_dictionary[" | ||
| + | |||
| + | for section in python_dictionary[" | ||
| + | config_object.add_section(section) | ||
| + | for options in python_dictionary[section][" | ||
| + | suboptions = [] | ||
| + | for subsection in python_dictionary[" | ||
| + | subkey = str(subsection) | ||
| + | subvalue = ( | ||
| + | '"' | ||
| + | + str(python_dictionary[" | ||
| + | + '"' | ||
| + | ) | ||
| + | suboptions.append(" | ||
| + | key = " " | ||
| + | config_object.set(section, | ||
| + | |||
| + | with open(args.o, | ||
| + | config_object.write(ini_file) | ||
| + | |||
| + | |||
| + | def main(): | ||
| + | if args.i and args.o: | ||
| + | convert_json2ini() | ||
| + | sys.exit(0) | ||
| + | |||
| + | |||
| + | if __name__ == " | ||
| + | main() | ||
| + | ~~~ | ||
| + | |||
| + | ~~~bash | ||
| + | ./ | ||
| + | ~~~ | ||
| + | |||
| + | |||
