Inital commit
This commit is contained in:
commit
e8761ab285
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
files/plugins_custom/
|
||||||
|
|
||||||
|
.*.sw?
|
||||||
|
*~
|
||||||
|
|
23
README.md
Normal file
23
README.md
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
Inventory scripts for Ansible: LXC
|
||||||
|
==================================
|
||||||
|
|
||||||
|
Dependencies
|
||||||
|
------------
|
||||||
|
- `lxc` Python module
|
||||||
|
|
||||||
|
Usage
|
||||||
|
-----
|
||||||
|
% ansible-inventory --graph -i inventory_lxc_ip.py
|
||||||
|
% ansible-inventory --graph -i inventory_lxc.py
|
||||||
|
|
||||||
|
Connection
|
||||||
|
----------
|
||||||
|
- `inventory_lxc.py` uses `ansible_connection=lxc`
|
||||||
|
- `inventory_lxc_ip.py` uses first IPv4 reported by LXC
|
||||||
|
|
||||||
|
Author Information
|
||||||
|
------------------
|
||||||
|
|
||||||
|
Sven Velt - <sven-ansiblerole@velt.biz>
|
||||||
|
https://git.velt.biz/velt.biz/
|
||||||
|
|
33
inventory_lxc.py
Executable file
33
inventory_lxc.py
Executable file
|
@ -0,0 +1,33 @@
|
||||||
|
#!/usr/bin/python3
|
||||||
|
|
||||||
|
import json
|
||||||
|
import sys
|
||||||
|
|
||||||
|
try:
|
||||||
|
import lxc
|
||||||
|
except ImportError:
|
||||||
|
print("Could not import lxc module!")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
########################################
|
||||||
|
inventory ={ '_meta':{'hostvars':{} } }
|
||||||
|
########################################
|
||||||
|
|
||||||
|
ctnames = lxc.list_containers()
|
||||||
|
|
||||||
|
for ctname in ctnames:
|
||||||
|
ct = lxc.Container(ctname)
|
||||||
|
|
||||||
|
if ct.running:
|
||||||
|
for group in ct.get_running_config_item('lxc.group').rstrip().split('\n'):
|
||||||
|
if not group in inventory:
|
||||||
|
inventory[group] = {
|
||||||
|
'hosts': [],
|
||||||
|
'vars': {
|
||||||
|
'ansible_connection': 'lxc',
|
||||||
|
}
|
||||||
|
}
|
||||||
|
inventory[group]['hosts'].append(ctname)
|
||||||
|
|
||||||
|
print(json.dumps(inventory))
|
||||||
|
|
37
inventory_lxc_ip.py
Executable file
37
inventory_lxc_ip.py
Executable file
|
@ -0,0 +1,37 @@
|
||||||
|
#!/usr/bin/python3
|
||||||
|
|
||||||
|
import json
|
||||||
|
import sys
|
||||||
|
|
||||||
|
try:
|
||||||
|
import lxc
|
||||||
|
except ImportError:
|
||||||
|
print("Could not import lxc module!")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
########################################
|
||||||
|
inventory ={ '_meta':{'hostvars':{} } }
|
||||||
|
########################################
|
||||||
|
|
||||||
|
ctnames = lxc.list_containers()
|
||||||
|
|
||||||
|
for ctname in ctnames:
|
||||||
|
ct = lxc.Container(ctname)
|
||||||
|
|
||||||
|
if ct.running:
|
||||||
|
for group in ct.get_running_config_item('lxc.group').rstrip().split('\n'):
|
||||||
|
if not group in inventory:
|
||||||
|
inventory[group] = {
|
||||||
|
'hosts': [],
|
||||||
|
'vars': {
|
||||||
|
'ansible_user': 'root',
|
||||||
|
},
|
||||||
|
}
|
||||||
|
inventory[group]['hosts'].append(ctname)
|
||||||
|
inventory['_meta']['hostvars'][ctname] = {
|
||||||
|
'ansible_host': ct.get_ips()[0],
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
print(json.dumps(inventory))
|
||||||
|
|
Loading…
Reference in a new issue