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

Commit

Permalink
Use Ansible variables, not dynamic role files
Browse files Browse the repository at this point in the history
Move towards using JBoss Scanner's facts inside of Rho by using static
Ansible role files with variables. (Previous Rho was dynamically
generating the role files at runtime.) This will make it much easier
to use a larger number of tasks in the roles, which is important
because JBoss Scanner uses many Ansible tasks.
  • Loading branch information
Noah Lavine committed Jul 7, 2017
1 parent 9121bcf commit 38bc433
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 73 deletions.
1 change: 0 additions & 1 deletion .gitignore
Expand Up @@ -17,7 +17,6 @@ build
.build
test/coverage
data
roles

.vagrant
*.retry
95 changes: 23 additions & 72 deletions rho/clicommands.py
Expand Up @@ -21,6 +21,7 @@
import re
import glob
import time
import json
import subprocess as sp
from collections import defaultdict
from collections import OrderedDict
Expand Down Expand Up @@ -140,73 +141,6 @@ def _check_range_validity(range_list):
sys.exit(1)


# Function to write to the playbook. Takes in the facts
# requested by the user and the file path for the report.
def _edit_playbook(facts, report_path):
string_to_write = "---\n\n- name: Collect these facts\n" \
" run_cmds: name=whatever fact_names=default\n" \
" register: facts_all\n\n" \
"- name: record host returned dictionary\n" \
" set_fact:\n res={{facts_all.meta}}\n"
# pylint: disable=unidiomatic-typecheck
if os.path.isfile(facts[0]) and not facts == ['default']:
my_facts = _read_in_file(facts[0])
string_to_write = "---\n\n- name: Collect these facts\n" \
" set_fact:\n fact_list:\n"
string_to_write = _stringify_facts(string_to_write, my_facts)
elif type(facts) == list and len(facts) >= 1 and\
not facts == ['default']:
string_to_write = "---\n\n- name: Collect these facts\n" \
" set_fact:\n fact_list:\n"
string_to_write = _stringify_facts(string_to_write, facts)
elif not facts == ['default']:
print(_("facts can be a file, list or 'default' only"))
sys.exit(1)

report_path = os.path.abspath(os.path.normpath(report_path))

if not os.path.exists('roles/collect/tasks'):
os.makedirs('roles/collect/tasks')

with open('roles/collect/tasks/main.yml', 'w') as collect_task_file:
collect_task_file.write(string_to_write)

string_to_write = '---\n\n- name: store facts from all' \
' hosts in a variable\n set_fact: ' \
'host_fact={{hostvars[item]["res"]}}\n ' \
' with_items: "{{groups.alpha}}"\n ' \
' register: host_facts\n\n- name:' \
' parse variable into a list of dictionaries' \
'\n set_fact: host_facts="{{ host_facts.results' \
' | map(attribute="ansible_facts.host_fact") | list }}' \
'"\n\n- name: write the list to a csv\n spit_results:' \
' name=spit file_path=' + report_path + ' vals' \
'={{host_' \
'facts}}\n'

if not os.path.exists('roles/write/tasks'):
os.makedirs('roles/write/tasks')
with open('roles/write/tasks/main.yml', 'w') as write_task_file:
write_task_file.write(string_to_write)


# Helper function to fill in the collect role
# of the playbook.
def _stringify_facts(string_to_write, facts):
for fact in facts:
string_to_write += " - " + fact + "\n"

string_to_write += "\n- name: grab info from list\n" \
" run_cmds: name=list_facts fact_names" \
"={{fact_list}}\n" \
" register: facts_selected\n\n" \
"- name: record host returned dictionary\n" \
" set_fact:\n" \
" res={{facts_selected.meta}}\n"

return string_to_write


# Creates the inventory for pinging all hosts and records
# successful auths and the hosts they worked on
# pylint: disable=too-many-statements
Expand Down Expand Up @@ -490,14 +424,16 @@ def _validate_options(self):

def _do_command(self):
# pylint: disable=too-many-locals
# pylint: disable=too-many-branches
profile = self.options.profile

facts = self.options.facts

forks = self.options.ansible_forks \
if self.options.ansible_forks else '50'

report_path = self.options.report_path
report_path = os.path.abspath(os.path.normpath(
self.options.report_path))

profile_exists = False

Expand Down Expand Up @@ -536,8 +472,6 @@ def _do_command(self):
print(_("Invalid profile. Create profile first"))
sys.exit(1)

_edit_playbook(facts, report_path)

# reset is used when the profile has just been created
# or freshly updated.

Expand All @@ -563,12 +497,29 @@ def _do_command(self):
"Please use --reset with profile first.")
sys.exit(1)

cmd_string = 'ansible-playbook rho_playbook.yml -i data/'\
+ profile + '_hosts ' + '-v -f ' + forks
if facts == ['default']:
facts_to_collect = 'default'
elif os.path.isfile(facts[0]):
facts_to_collect = _read_in_file(facts[0])
else:
assert isinstance(facts, list)
facts_to_collect = facts

ansible_vars = {'facts_to_collect': facts_to_collect,
'report_path': report_path}

cmd_string = ('ansible-playbook rho_playbook.yml '
'-i data/{profile}_hosts -v -f {forks} '
'--extra-vars \'{vars}\'').format(
profile=profile,
forks=forks,
vars=json.dumps(ansible_vars))

# process finally runs ansible on the
# playbook and inventories thus created.

print ('Running:', cmd_string)

process = sp.Popen(cmd_string,
shell=True)

Expand Down
9 changes: 9 additions & 0 deletions roles/collect/tasks/main.yml
@@ -0,0 +1,9 @@
---

- name: Collect these facts
run_cmds: name=whatever fact_names={{facts_to_collect}}
register: facts_all

- name: record host returned dictionary
set_fact:
res={{facts_all.meta}}
12 changes: 12 additions & 0 deletions roles/write/tasks/main.yml
@@ -0,0 +1,12 @@
---

- name: store facts from all hosts in a variable
set_fact: host_fact={{hostvars[item]["res"]}}
with_items: "{{groups.alpha}}"
register: host_facts

- name: parse variable into a list of dictionaries
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}}

0 comments on commit 38bc433

Please sign in to comment.