Last active
February 20, 2018 15:05
-
-
Save skamithi/675ef7a0573e37b66b0691e871e1c6e5 to your computer and use it in GitHub Desktop.
analyse tower script
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
HOST VAR ANALYSIS | |
------- | |
mygrouptwovarstwo {'host_count': 2, 'group_count': 1} | |
myvars {'host_count': 2, 'group_count': 1} | |
anothergroup2 {'host_count': 2, 'group_count': 1} | |
anothergroup1 {'host_count': 2, 'group_count': 1} | |
mygrouptwovars {'host_count': 2, 'group_count': 1} | |
myvarstwo {'host_count': 2, 'group_count': 1} | |
HOST GROUP ANALYSIS | |
------- | |
Test Multiple Sources {'host_count': 6, 'group_name': u'web'} | |
test inv 2 {'host_count': 3, 'group_name': u'test'} | |
TestMePlease {'host_count': 2, 'group_name': u'GroupTwo'} | |
satellite {'host_count': 1, 'group_name': u'foreman_environment_production'} | |
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/python | |
# coding: utf-8 -*- | |
# (c) 2017, Stanley Karunditu <skarundi@redhat.com> | |
# GNU General Public License v3.0+ (see COPYING or | |
# https://www.gnu.org/licenses/gpl-3.0.txt) | |
# prereq log into tower as root, execute the awx virtualenv | |
# > source /var/lib/awx/venv/ansible/bin/activate | |
# Then pass the --user and --passwd and --host variables to the script | |
# > tower_analysis.py --user admin --passwd tower --host 192.168.0.10 | |
import requests | |
import argparse | |
from collections import OrderedDict | |
def get_pages(base_url, uri, headers): | |
_next = uri | |
output = [] | |
while _next: | |
url = base_url + _next + '?page_size=100' | |
r = requests.get(url, data=None, verify=False, headers=headers).json() | |
if r.get('results'): | |
output = output + r.get('results') | |
elif 'variable_data' in uri: | |
output += r | |
_next = r.get('next') | |
return output | |
def main(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--user') | |
parser.add_argument('--passwd') | |
parser.add_argument('--host') | |
args = parser.parse_args() | |
base_url = "https://" + args.host | |
payload = { | |
'username': args.user, | |
'password': args.passwd | |
} | |
auth_url = base_url + "/api/v1/authtoken/" | |
r = requests.post(auth_url, data=None, json=payload, verify=False) | |
auth_token = 'Token ' + r.json().get('token') | |
headers = {'Authorization': auth_token} | |
inventory_uri = '/api/v1/inventories/' | |
# Parse through all the inventory lists to get the IDs | |
inventory_list = get_pages(base_url, inventory_uri, headers) | |
inventory_list_ids = [i.get('id') for i in inventory_list] | |
# format | |
# {key: host_count, group_count} | |
host_vars = {} | |
inventory_details = {} | |
# parse through each inventory and get a list of groups then get a list of all the | |
# variables | |
for entry in inventory_list_ids: | |
name_url = base_url + '/api/v1/inventories/' + str(entry) + '/' | |
r = requests.get(name_url, data=None, verify=False, headers=headers).json() | |
inventoryname = r.get('name') | |
groups = (get_pages(base_url, r.get('related').get('groups'), headers)) | |
for group in groups: | |
group_host_count = len(get_pages(base_url, group.get('related').get('hosts'), headers)) | |
group_host_vars = get_pages(base_url, group.get('related').get('variable_data'), headers) | |
inventory_details[inventoryname] = { | |
'group_name': group.get('name'), | |
"host_count": group_host_count | |
} | |
for _hostvarkey in group_host_vars: | |
if not host_vars.get(_hostvarkey): | |
host_vars[_hostvarkey] = { | |
'host_count': group_host_count, | |
'group_count': 1 | |
} | |
else: | |
_current_host_count = host_vars.get(_hostvarkey).get('host_count') | |
host_vars.get(_hostvarkey)['host_count'] = _current_host_count + group_host_count | |
host_vars.get(_hostvarkey)['group_count'] =+ 1 | |
print("HOST VAR ANALYSIS\n-------") | |
od = OrderedDict(sorted(host_vars.items(), key=lambda kv: kv[1]['host_count'], reverse=True)) | |
for k, v in od.items(): | |
print k, v | |
print "\n" | |
print("HOST GROUP ANALYSIS\n-------") | |
od = OrderedDict(sorted(inventory_details.items(), key=lambda kv: kv[1]['host_count'], reverse=True)) | |
for k, v in od.items(): | |
print k, v | |
print "\n" | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment