Port setup module to use the common module base

This commit is contained in:
Michael DeHaan 2012-07-28 17:14:23 -04:00
commit d0a5dec686

View file

@ -598,73 +598,65 @@ def ansible_facts():
# =========================================== # ===========================================
# load config & template variables def run_setup(module):
if len(sys.argv) == 1: setup_options = {}
sys.exit(1) facts = ansible_facts()
for (k, v) in facts.items():
setup_options["ansible_%s" % k] = v
argfile = sys.argv[1] # if facter is installed, and we can use --json because
if not os.path.exists(argfile): # ruby-json is ALSO installed, include facter data in the JSON
sys.exit(1)
setup_options = open(argfile).read().strip() if os.path.exists("/usr/bin/facter"):
try: cmd = subprocess.Popen("/usr/bin/facter --json", shell=True,
setup_options = json.loads(setup_options) stdout=subprocess.PIPE, stderr=subprocess.PIPE)
except: out, err = cmd.communicate()
list_options = shlex.split(setup_options) facter = True
setup_options = {} try:
for opt in list_options: facter_ds = json.loads(out)
(k,v) = opt.split("=") except:
setup_options[k]=v facter = False
if facter:
for (k,v) in facter_ds.items():
setup_options["facter_%s" % k] = v
syslog.openlog('ansible-%s' % os.path.basename(__file__)) # ditto for ohai, but just top level string keys
syslog.syslog(syslog.LOG_NOTICE, 'Invoked with %s' % setup_options) # because it contains a lot of nested stuff we can't use for
# templating w/o making a nicer key for it (TODO)
# Get some basic facts in case facter or ohai are not installed if os.path.exists("/usr/bin/ohai"):
for (k, v) in ansible_facts().items(): cmd = subprocess.Popen("/usr/bin/ohai", shell=True,
setup_options["ansible_%s" % k] = v stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = cmd.communicate()
ohai = True
try:
ohai_ds = json.loads(out)
except:
ohai = False
if ohai:
for (k,v) in ohai_ds.items():
if type(v) == str or type(v) == unicode:
k2 = "ohai_%s" % k
setup_options[k2] = v
# if facter is installed, and we can use --json because setup_result = {}
# ruby-json is ALSO installed, include facter data in the JSON setup_result['ansible_facts'] = setup_options
if os.path.exists("/usr/bin/facter"): # hack to keep --verbose from showing all the setup module results
cmd = subprocess.Popen("/usr/bin/facter --json", shell=True, setup_result['verbose_override'] = True
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = cmd.communicate()
facter = True
try:
facter_ds = json.loads(out)
except:
facter = False
if facter:
for (k,v) in facter_ds.items():
setup_options["facter_%s" % k] = v
# ditto for ohai, but just top level string keys return setup_result
# because it contains a lot of nested stuff we can't use for
# templating w/o making a nicer key for it (TODO)
if os.path.exists("/usr/bin/ohai"): def main():
cmd = subprocess.Popen("/usr/bin/ohai", shell=True, module = AnsibleModule(
stdout=subprocess.PIPE, stderr=subprocess.PIPE) argument_spec = dict()
out, err = cmd.communicate() )
ohai = True data = run_setup(module)
try: module.exit_json(**data)
ohai_ds = json.loads(out)
except:
ohai = False
if ohai:
for (k,v) in ohai_ds.items():
if type(v) == str or type(v) == unicode:
k2 = "ohai_%s" % k
setup_options[k2] = v
setup_result = {} # this is magic, see lib/ansible/module_common.py
setup_result['ansible_facts'] = setup_options #<<INCLUDE_ANSIBLE_MODULE_COMMON>>
main()
# hack to keep --verbose from showing all the setup module results
setup_result['verbose_override'] = True
print json.dumps(setup_result)