Parse a special hosts file to create an Ansible inventory
I love Ansible, but I don't like the INI format of the inventory file because it's very WET. There are lots of ways around that, but for one reason or another I don't like them either, so I wrote this to work the way I think the Ansible inventory should work.
It's very simple: it takes a list of nodes with an indented list of either groups to which that node belongs, or inventory variables for that node, and it builds a WET Ansible INI inventory.
Just clone the repo someplace and use whichever files you want.
hosts2inventory.pldoes all the workhosts2inventory.shis a trivial wrapper to "do the right thing" ("write" thing?)
./hosts2inventory.shcat /tmp/hosts.txt | ./hosts2inventory.pl (-o <hosts>)gc | ./hosts2inventory.pl (-o <hosts>)./hosts2inventory.pl (-h -D)# for help or debugging
Use -o - for STDOUT, default if omitted is ./hosts.
Note gc is my alias gc='xsel -b' and it means "Get from the Clipboard" so I can create an inventory on-the-fly from random stuff I just typed into an editor.
If I run gc | ./hosts2inventory.pl -o - then:
desktop1
desktop
gui
workstation
variable1=foo
laptop1
gui
laptop
workstation
# DO NOT MANUALLY EDIT THIS FILE!
# Ansible inventory generated by 'hosts2inventory.pl' on 2020-12-30 13:54:10
# [all] 2 nodes
desktop1 variable1=foo
laptop1
[desktop] # 1 nodes
desktop1
[gui] # 2 nodes
desktop1
laptop1
[laptop] # 1 nodes
laptop1
[workstation] # 2 nodes
desktop1
laptop1