This is a Dynamic Inventory for Ansible to be used together with MySQL.
It was written because we maintain a lot of servers and static inventory files did not meet our demand, and we like MySQL.
See JOSEs_NOTES for a quicker how-to.
Simply call the script like the following
ansible-playbook -i inventory.py
# or
ansible -i inventory.py
Limitations also work
ansible-playbook -i inventory.py --limit foo.bar.com
ansible-playbook -i inventory.py --limit groupFoo
Add a new host
- -H: set host dns
my.host.domain, can also set ip address - [-g]: set group name is
my, otherwise host will add to group calledungrouped - [-v key val]: 2 sets of variables { var0:val0, var1:val1 }
- See details:
python inventoryctl.py host -h
$ python inventoryctl.py host -H my.host.domain -g my -v var0 val0 -v var1 val1
Command: host
{'enabled': 1, 'variables': None, 'id': 1, 'name': 'my'}
- -H: which host will be update
- -U: enter update mode, otherwise only view the host
- See details:
python inventoryctl.py host -h
$ python inventoryctl.py host -H my.host.domain -U -v var2 val2
Command: host
Update mode
set variables to {"var1": "val1", "var0": "val0", "var2": "val2"}
Update my.host.domain affected rows: 1
I won't explain the process of installing a database or creating the tables, see tables.sql for the required MySQL structure.
Once setup rename mysql.ini.dist to mysql.ini to suit your needs, if you don't want to use caching just put it on 0.
In the table group you create the groups you need and their variables,
In the table host under host you place the IP/DNS for the system.
Under hostname you can fill in a value, this will be presented as a variable inventory_hostname during the play.
You can modify the name of this Fact variable by changing the facts_hostname_var variable in my mysql.ini.
The table hostgroups maps the relation between host and group using two FOREIGN KEYS.
Groups can have other groups as children, use the table childgroups.
This applies to host and group respectively.
If no variables are needed either NULL it (actual MySQL NULL not the string) or use {}.
As it was fork from productsupcom/ansible-dyninv-mysql, the same license, the GPL-3 applies.
The GPL-3 can be found under the link.