How do you convert ansible ini inventory into json or yaml
Asked Answered
S

2

14

Ansible AWX requires inventories to be entered in yaml or json format.

When you start learning ansible, you may take the choice to start off with your inventory in ini format.

https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html

Is it possible to convert between formats?

Seljuk answered 30/8, 2019 at 13:5 Comment(1)
I would be very interested to know in which version of AWX this limitation has been made... since we currently use AWX with about a dozen inventories in ini format. I'd better make sure we modify them before we upgrade.Cheroot
S
26

Example given here

https://evrard.me/convert-ansible-inventories-with-ansible-inventory-cli

ansible-inventory -i inventory.ini -y --list > inventory.yaml

The -y flag gives yaml output. Omitting the flag gives json.

Seljuk answered 30/8, 2019 at 13:5 Comment(2)
That works for a simple inventory, but if you are using group_vars directories it will 'flatten' those group vars into the output YAML file, so you would need to edit them out.Enrichetta
@Enrichetta just copy your inventory to another folder first to ignore the group varsImprest
S
1

There is a converter written in python here.

https://github.com/appuio/ansible-ini2yaml

Seljuk answered 30/8, 2019 at 13:19 Comment(1)
Tried this and it does work quite well - handles the slightly unusual Ansible format within the INI file sections, and generates equivalent YAML. Gives a warning from the pyyaml package, and Python 2.7 only but probably not hard to port to Python 3.Enrichetta

© 2022 - 2024 — McMap. All rights reserved.