Last active
September 17, 2019 20:01
-
-
Save cocampbe/43f5a4bfa9101b52fd981c06a7bb330f to your computer and use it in GitHub Desktop.
Use this via cron to create a dynamic inventory file. As servers are added/removed from spacewalk, or satellite, your inventory file will be updated.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
''' | |
create_ansible_hosts_file.py | |
create ansible inventory file from servers in spacewalk and satellite | |
by: @cocampbe | |
''' | |
from subprocess import run,PIPE | |
inventory_file = '/etc/ansible/hosts' | |
groups = {} | |
# Get server lists | |
sw_servers = run(["ssh", "sw_server", "spacecmd -q system_list | awk '{print $1}' | cut -d. -f1"], encoding="utf-8", stdout=PIPE).stdout.split() | |
sat_servers = run(["ssh", "sat_server", " hammer --output base host list | grep Name| awk -F: '{print $NF}' | cut -d. -f1"], encoding="utf-8", stdout=PIPE).stdoutsplit() | |
groups['prod'] = [x for x in sw_servers + sat_servers if x.startswith("pd")] | |
groups['dev'] = [x for x in sw_servers + sat_servers if x.startswith("dv")] | |
groups['test'] = [x for x in sw_servers + sat_servers if x.startswith("ts") or x.startswith("td")] | |
# mysql groups | |
groups['mysql_prod'] = [x for x in groups['prod'] if "mysql" in x] | |
groups['mysql_dev'] = [x for x in groups['dev'] if "mysql" in x] | |
groups['mysql_ua'] = [x for x in groups['ua'] if "mysql" in x] | |
groups['mysql:children'] = [x for x in groups.keys() if "mysql" in x] | |
with open(inventory_file, 'w') as f: | |
for key,values in groups.items(): | |
f.write("[" + key + "]" + '\n') | |
f.write('\n'.join(values) + '\n\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment