Skip to content
This repository has been archived by the owner on Jun 29, 2022. It is now read-only.

Commit

Permalink
Convert systemid.* fact gathering to raw tasks. Parse xml results. Cl…
Browse files Browse the repository at this point in the history
…oses #115.
  • Loading branch information
chambridge committed Jul 31, 2017
1 parent 85f9ca3 commit 98e2805
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 6 deletions.
3 changes: 1 addition & 2 deletions library/run_cmds.py
Expand Up @@ -51,8 +51,7 @@
# passed in through the playbook (attached
# to the field requested using an '_').

DEFAULT_CMD_DICT = {"SysId": file_commands.SystemIdRhoCmd,
"RedhatPackages":
DEFAULT_CMD_DICT = {"RedhatPackages":
redhat_packages_command.RedhatPackagesRhoCmd}


Expand Down
38 changes: 37 additions & 1 deletion library/spit_results.py
Expand Up @@ -16,9 +16,17 @@
import csv
import os
import json
import sys
import xml
# pylint: disable=import-error
from ansible.module_utils.basic import AnsibleModule

# for parsing systemid
if sys.version_info > (3,):
import xmlrpc.client as xmlrpclib # pylint: disable=import-error
else:
import xmlrpclib # pylint: disable=import-error


class Results(object):
"""The class Results contains the functionality to parse
Expand All @@ -31,6 +39,28 @@ def __init__(self, module):
self.name = module.params['name']
self.file_path = module.params['file_path']
self.vals = module.params['vals']
self.fact_names = module.params['fact_names']

def handle_systemid(self, data):
"""Process the output of systemid.contents
and supply the appropriate output information
"""
if 'systemid.contents' in data:
blob = data['systemid.contents']
try:
systemid = xmlrpclib.loads(blob)[0][0]
except xml.parsers.expat.ExpatError:
pass

if 'SysId_systemid.system_id' in self.fact_names and ('system_id'
in systemid):
data['systemid.system_id'] = systemid['system_id']
if 'SysId_systemid.username' in self.fact_names and ('usnername'
in systemid):
data['systemid.username'] = systemid['usnername']

del data['systemid.contents']
return data

def write_to_csv(self):
"""Output report data to file in csv format"""
Expand All @@ -39,10 +69,15 @@ def write_to_csv(self):
file_size = os.path.getsize(f_path)
vals = self.vals
fields = sorted(vals[0].keys())
try:
fields.remove('systemid.contents')
except ValueError:
pass
writer = csv.writer(write_file, delimiter=',')
if file_size == 0:
writer.writerow(fields)
for data in vals:
data = self.handle_systemid(data)
sorted_keys = sorted(data.keys())
sorted_values = []
for k in sorted_keys:
Expand All @@ -64,7 +99,8 @@ def main():
fields = {
"name": {"required": True, "type": "str"},
"file_path": {"required": True, "type": "str"},
"vals": {"required": True, "type": "list"}
"vals": {"required": True, "type": "list"},
"fact_names": {"required": True, "type": "list"}
}

module = AnsibleModule(argument_spec=fields)
Expand Down
26 changes: 26 additions & 0 deletions roles/file_contents/tasks/main.yml
Expand Up @@ -23,3 +23,29 @@
file_contents: "{{ file_contents|default({}) | combine({ item: instnum_instnum['stdout']}) }}"
with_items:
- 'instnum.instnum'

- name: gather systemid.contents fact
raw: if [ -f /etc/sysconfig/rhn/systemid ] ; then cat /etc/sysconfig/rhn/systemid ; fi
register: systemid_contents
ignore_errors: yes
when: '"SysId_systemid.system_id" in facts_to_collect or "SysId_systemid.username" in facts_to_collect'

- name: add systemid_contents to dictionary
set_fact:
file_contents: "{{ file_contents|default({}) | combine({ item: systemid_contents['stdout']}) }}"
with_items:
- 'systemid.contents'

- name: initialize systemid.system_id to dictionary
set_fact:
file_contents: "{{ file_contents|default({}) | combine({ item: '' }) }}"
with_items:
- 'systemid.system_id'
when: '"SysId_systemid.system_id" in facts_to_collect'

- name: initialize systemid.username to dictionary
set_fact:
file_contents: "{{ file_contents|default({}) | combine({ item: '' }) }}"
with_items:
- 'systemid.username'
when: '"SysId_systemid.username" in facts_to_collect'
6 changes: 5 additions & 1 deletion roles/write/tasks/main.yml
Expand Up @@ -9,4 +9,8 @@
set_fact: host_facts="{{ host_facts.results | map(attribute="ansible_facts.host_fact") | list }}"

- name: write the list to a csv
spit_results: name=spit file_path={{report_path}} vals={{host_facts}}
spit_results:
name: reporter
file_path: "{{report_path}}"
vals: "{{host_facts}}"
fact_names: "{{facts_to_collect}}"
7 changes: 5 additions & 2 deletions test/test_spit_results.py
Expand Up @@ -31,14 +31,17 @@ def test__main__success(self, ansible_mod_cls):
args = {
"name": "foo",
"file_path": TMP_TEST_REPORT,
"vals": [{'fact1': 'value1'}]
"vals": [{'fact1': 'value1'}],
"fact_names": ["Cpu_cpu.model_ver", "SysId_systemid.system_id",
"SysId_systemid.username"]
}
mod_obj.params = args
spit_results.main()
expected_arguments_spec = {
"name": {"required": True, "type": "str"},
"file_path": {"required": True, "type": "str"},
"vals": {"required": True, "type": "list"}
"vals": {"required": True, "type": "list"},
"fact_names": {"required": True, "type": "list"}
}

assert(mock.call(argument_spec=expected_arguments_spec) ==
Expand Down

0 comments on commit 98e2805

Please sign in to comment.