Inital commit

This commit is contained in:
Sven Velt 2024-09-20 09:48:55 +02:00
commit e8761ab285
4 changed files with 98 additions and 0 deletions

5
.gitignore vendored Normal file
View file

@ -0,0 +1,5 @@
files/plugins_custom/
.*.sw?
*~

23
README.md Normal file
View 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
View 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
View 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))