mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Forgot to use commit -a to submit all changes...
This commit is contained in:
parent
38ff797d57
commit
aa1e00e8e7
1 changed files with 63 additions and 28 deletions
|
@ -16,14 +16,15 @@ This script is dependent upon the spacealk-reports package being installed
|
||||||
on the same machine. It is basically a CSV-to-JSON converter from the
|
on the same machine. It is basically a CSV-to-JSON converter from the
|
||||||
output of "spacewalk-report system-groups-systems|inventory".
|
output of "spacewalk-report system-groups-systems|inventory".
|
||||||
|
|
||||||
Tested with Ansible 1.1
|
Tested with Ansible 1.9.2 and spacewalk 2.3
|
||||||
"""
|
"""
|
||||||
#
|
#
|
||||||
# Author:: Jon Miller <jonEbird@gmail.com>
|
# Author:: Jon Miller <jonEbird@gmail.com>
|
||||||
# Copyright:: Copyright (c) 2013, Jon Miller
|
# Copyright:: Copyright (c) 2013, Jon Miller
|
||||||
#
|
#
|
||||||
# Extended for support of multiple organizations by
|
# Extended for support of multiple organizations and
|
||||||
# Bernhard Lichtinger <bernhard.lichtinger@lrz.de> 2014
|
# adding the "_meta" dictionary to --list output by
|
||||||
|
# Bernhard Lichtinger <bernhard.lichtinger@lrz.de> 2015
|
||||||
#
|
#
|
||||||
# This program is free software: you can redistribute it and/or modify
|
# This program is free software: you can redistribute it and/or modify
|
||||||
# it under the terms of the GNU General Public License as published by
|
# it under the terms of the GNU General Public License as published by
|
||||||
|
@ -44,6 +45,7 @@ import os
|
||||||
import time
|
import time
|
||||||
from optparse import OptionParser
|
from optparse import OptionParser
|
||||||
import subprocess
|
import subprocess
|
||||||
|
import ConfigParser
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import json
|
import json
|
||||||
|
@ -54,6 +56,7 @@ base_dir = os.path.dirname(os.path.realpath(__file__))
|
||||||
SW_REPORT = '/usr/bin/spacewalk-report'
|
SW_REPORT = '/usr/bin/spacewalk-report'
|
||||||
CACHE_DIR = os.path.join(base_dir, ".spacewalk_reports")
|
CACHE_DIR = os.path.join(base_dir, ".spacewalk_reports")
|
||||||
CACHE_AGE = 300 # 5min
|
CACHE_AGE = 300 # 5min
|
||||||
|
INI_FILE = os.path.join(base_dir, "spacewalk.ini")
|
||||||
|
|
||||||
# Sanity check
|
# Sanity check
|
||||||
if not os.path.exists(SW_REPORT):
|
if not os.path.exists(SW_REPORT):
|
||||||
|
@ -83,6 +86,8 @@ def spacewalk_report(name):
|
||||||
|
|
||||||
lines = open(cache_filename, 'r').readlines()
|
lines = open(cache_filename, 'r').readlines()
|
||||||
keys = lines[0].strip().split(',')
|
keys = lines[0].strip().split(',')
|
||||||
|
# add 'spacewalk_' prefix to the keys
|
||||||
|
keys = [ 'spacewalk_' + key for key in keys ]
|
||||||
for line in lines[1:]:
|
for line in lines[1:]:
|
||||||
values = line.strip().split(',')
|
values = line.strip().split(',')
|
||||||
if len(keys) == len(values):
|
if len(keys) == len(values):
|
||||||
|
@ -107,12 +112,25 @@ parser.add_option('-p', default=False, dest="prefix_org_name", action="store_tru
|
||||||
(options, args) = parser.parse_args()
|
(options, args) = parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
|
# read spacewalk.ini if present
|
||||||
|
#------------------------------
|
||||||
|
if os.path.exists(INI_FILE):
|
||||||
|
config = ConfigParser.SafeConfigParser()
|
||||||
|
config.read(INI_FILE)
|
||||||
|
if config.has_option('spacewalk' , 'cache_age'):
|
||||||
|
CACHE_AGE = config.get('spacewalk' , 'cache_age')
|
||||||
|
if not options.org_number and config.has_option('spacewalk' , 'org_number'):
|
||||||
|
options.org_number = config.get('spacewalk' , 'org_number')
|
||||||
|
if not options.prefix_org_name and config.has_option('spacewalk' , 'prefix_org_name'):
|
||||||
|
options.prefix_org_name = config.getboolean('spacewalk' , 'prefix_org_name')
|
||||||
|
|
||||||
|
|
||||||
# Generate dictionary for mapping group_id to org_id
|
# Generate dictionary for mapping group_id to org_id
|
||||||
#------------------------------
|
#------------------------------
|
||||||
org_groups = {}
|
org_groups = {}
|
||||||
try:
|
try:
|
||||||
for group in spacewalk_report('system-groups'):
|
for group in spacewalk_report('system-groups'):
|
||||||
org_groups[group['group_id']] = group['org_id']
|
org_groups[group['spacewalk_group_id']] = group['spacewalk_org_id']
|
||||||
|
|
||||||
except (OSError), e:
|
except (OSError), e:
|
||||||
print >> sys.stderr, 'Problem executing the command "%s system-groups": %s' % \
|
print >> sys.stderr, 'Problem executing the command "%s system-groups": %s' % \
|
||||||
|
@ -124,18 +142,30 @@ except (OSError), e:
|
||||||
#------------------------------
|
#------------------------------
|
||||||
if options.list:
|
if options.list:
|
||||||
|
|
||||||
|
# to build the "_meta"-Group with hostvars first create dictionary for later use
|
||||||
|
host_vars = {}
|
||||||
|
try:
|
||||||
|
for item in spacewalk_report('inventory'):
|
||||||
|
host_vars[ item['spacewalk_profile_name'] ] = dict( ( key, ( value.split(';') if ';' in value else value) ) for key, value in item.items() )
|
||||||
|
|
||||||
|
except (OSError), e:
|
||||||
|
print >> sys.stderr, 'Problem executing the command "%s inventory": %s' % \
|
||||||
|
(SW_REPORT, str(e))
|
||||||
|
sys.exit(2)
|
||||||
|
|
||||||
groups = {}
|
groups = {}
|
||||||
|
meta = { "hostvars" : {} }
|
||||||
try:
|
try:
|
||||||
for system in spacewalk_report('system-groups-systems'):
|
for system in spacewalk_report('system-groups-systems'):
|
||||||
# first get org_id of system
|
# first get org_id of system
|
||||||
org_id = org_groups[ system['group_id'] ]
|
org_id = org_groups[ system['spacewalk_group_id'] ]
|
||||||
|
|
||||||
# shall we add the org_id as prefix to the group name:
|
# shall we add the org_id as prefix to the group name:
|
||||||
if options.prefix_org_name:
|
if options.prefix_org_name:
|
||||||
prefix = org_id + "-"
|
prefix = org_id + "-"
|
||||||
group_name = prefix + system['group_name']
|
group_name = prefix + system['spacewalk_group_name']
|
||||||
else:
|
else:
|
||||||
group_name = system['group_name']
|
group_name = system['spacewalk_group_name']
|
||||||
|
|
||||||
# if we are limited to one organization:
|
# if we are limited to one organization:
|
||||||
if options.org_number:
|
if options.org_number:
|
||||||
|
@ -143,13 +173,17 @@ if options.list:
|
||||||
if group_name not in groups:
|
if group_name not in groups:
|
||||||
groups[group_name] = set()
|
groups[group_name] = set()
|
||||||
|
|
||||||
groups[group_name].add(system['server_name'])
|
groups[group_name].add(system['spacewalk_server_name'])
|
||||||
|
if system['spacewalk_server_name'] in host_vars and not system['spacewalk_server_name'] in meta[ "hostvars" ]:
|
||||||
|
meta[ "hostvars" ][ system['spacewalk_server_name'] ] = host_vars[ system['spacewalk_server_name'] ]
|
||||||
# or we list all groups and systems:
|
# or we list all groups and systems:
|
||||||
else:
|
else:
|
||||||
if group_name not in groups:
|
if group_name not in groups:
|
||||||
groups[group_name] = set()
|
groups[group_name] = set()
|
||||||
|
|
||||||
groups[group_name].add(system['server_name'])
|
groups[group_name].add(system['spacewalk_server_name'])
|
||||||
|
if system['spacewalk_server_name'] in host_vars and not system['spacewalk_server_name'] in meta[ "hostvars" ]:
|
||||||
|
meta[ "hostvars" ][ system['spacewalk_server_name'] ] = host_vars[ system['spacewalk_server_name'] ]
|
||||||
|
|
||||||
except (OSError), e:
|
except (OSError), e:
|
||||||
print >> sys.stderr, 'Problem executing the command "%s system-groups-systems": %s' % \
|
print >> sys.stderr, 'Problem executing the command "%s system-groups-systems": %s' % \
|
||||||
|
@ -160,8 +194,10 @@ if options.list:
|
||||||
for group, systems in groups.iteritems():
|
for group, systems in groups.iteritems():
|
||||||
print '[%s]\n%s\n' % (group, '\n'.join(systems))
|
print '[%s]\n%s\n' % (group, '\n'.join(systems))
|
||||||
else:
|
else:
|
||||||
print json.dumps(dict([ (k, list(s)) for k, s in groups.iteritems() ]))
|
final = dict( [ (k, list(s)) for k, s in groups.iteritems() ] )
|
||||||
|
final["_meta"] = meta
|
||||||
|
print json.dumps( final )
|
||||||
|
#print json.dumps(groups)
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
|
|
||||||
|
@ -172,7 +208,7 @@ elif options.host:
|
||||||
host_details = {}
|
host_details = {}
|
||||||
try:
|
try:
|
||||||
for system in spacewalk_report('inventory'):
|
for system in spacewalk_report('inventory'):
|
||||||
if system['hostname'] == options.host:
|
if system['spacewalk_hostname'] == options.host:
|
||||||
host_details = system
|
host_details = system
|
||||||
break
|
break
|
||||||
|
|
||||||
|
@ -186,8 +222,7 @@ elif options.host:
|
||||||
for k, v in host_details.iteritems():
|
for k, v in host_details.iteritems():
|
||||||
print ' %s: %s' % (k, '\n '.join(v.split(';')))
|
print ' %s: %s' % (k, '\n '.join(v.split(';')))
|
||||||
else:
|
else:
|
||||||
print json.dumps(host_details)
|
print json.dumps( dict( ( key, ( value.split(';') if ';' in value else value) ) for key, value in host_details.items() ) )
|
||||||
|
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
|
Loading…
Reference in a new issue