Merge pull request #30 from xlab-steampunk/dont-use-vars

Remove the use of `vars` and `hostvars` wherever possible.
This commit is contained in:
Sean Cavanaugh
2021-06-03 14:07:58 -04:00
committed by GitHub
5 changed files with 63 additions and 59 deletions

View File

@@ -30,7 +30,7 @@ ansible-playbook playbooks/install_demo.yml -e @choose_demo.yml
# Demo Repository # Demo Repository
This repository currently holds {{hostvars[inventory_hostname].items()|rejectattr('0', 'match', 'ansible_')|selectattr('1.name', 'defined')|sort(attribute='1.name')|count}} demos. This repository currently holds {{job_template_definitions.items()|sort(attribute='1.name')|count}} demos.
## Infrastructure Demos ## Infrastructure Demos
<table> <table>
@@ -42,7 +42,7 @@ This repository currently holds {{hostvars[inventory_hostname].items()|rejectatt
<th>Video Walkthrough</th> <th>Video Walkthrough</th>
<th>Workshop Types</th> <th>Workshop Types</th>
</tr> </tr>
{% for key, value in hostvars[inventory_hostname].items()|rejectattr('0', 'match', 'ansible_')|selectattr('1.name', 'defined')|selectattr('1.category', 'match', 'infrastructure$')|sort(attribute='1.name') %} {% for key, value in job_template_definitions.items()|selectattr('1.category', 'match', 'infrastructure$')|sort(attribute='1.name') %}
<tr> <tr>
<td>{% if value.readme is defined%}<a href="{{ value.readme}}">{{value.name}}</a>{% else %}{{value.name}}{% endif %}</td> <td>{% if value.readme is defined%}<a href="{{ value.readme}}">{{value.name}}</a>{% else %}{{value.name}}{% endif %}</td>
<td>{{value.author}}</td> <td>{{value.author}}</td>
@@ -65,7 +65,7 @@ This repository currently holds {{hostvars[inventory_hostname].items()|rejectatt
<th>Video Walkthrough</th> <th>Video Walkthrough</th>
<th>Workshop Types</th> <th>Workshop Types</th>
</tr> </tr>
{% for key, value in hostvars[inventory_hostname].items()|rejectattr('0', 'match', 'ansible_')|selectattr('1.name', 'defined')|selectattr('1.category', 'match', 'network$')|sort(attribute='1.name') %} <tr> {% for key, value in job_template_definitions.items()|selectattr('1.category', 'match', 'network$')|sort(attribute='1.name') %} <tr>
<td>{{ value.name }}</td> <td>{{ value.name }}</td>
<td>{{ value.author }}</td> <td>{{ value.author }}</td>
<td><pre>demo: {{key }}</pre></td> <td><pre>demo: {{key }}</pre></td>
@@ -87,7 +87,7 @@ This repository currently holds {{hostvars[inventory_hostname].items()|rejectatt
<th>Video Walkthrough</th> <th>Video Walkthrough</th>
<th>Workshop Types</th> <th>Workshop Types</th>
</tr> </tr>
{% for key, value in hostvars[inventory_hostname].items()|rejectattr('0', 'match', 'ansible_')|selectattr('1.name', 'defined')|selectattr('1.category', 'match', 'security$')|sort(attribute='1.name') %} <tr> {% for key, value in job_template_definitions.items()|selectattr('1.category', 'match', 'security$')|sort(attribute='1.name') %} <tr>
<td>{{ value.name }}</td> <td>{{ value.name }}</td>
<td>{{ value.author }}</td> <td>{{ value.author }}</td>
<td><pre>demo: {{ key }}</pre></td> <td><pre>demo: {{ key }}</pre></td>
@@ -109,7 +109,7 @@ This repository currently holds {{hostvars[inventory_hostname].items()|rejectatt
<th>Video Walkthrough</th> <th>Video Walkthrough</th>
<th>Workshop Types</th> <th>Workshop Types</th>
</tr> </tr>
{% for key, value in hostvars[inventory_hostname].items()|rejectattr('0', 'match', 'ansible_')|selectattr('1.name', 'defined')|selectattr('1.category', 'match', 'developer$')|sort(attribute='1.name') %} <tr> {% for key, value in job_template_definitions.items()|selectattr('1.category', 'match', 'developer$')|sort(attribute='1.name') %} <tr>
<td>{{ value.name }}</td> <td>{{ value.name }}</td>
<td>{{ value.author }}</td> <td>{{ value.author }}</td>
<td><pre>demo: {{ key }}</pre></td> <td><pre>demo: {{ key }}</pre></td>

View File

@@ -1,11 +1,11 @@
--- ---
- name: add tower project - name: add tower project
awx.awx.tower_project: awx.awx.tower_project:
name: "{{ vars[demo].project.name }}" name: "{{ job_template_definitions[demo].project.name }}"
description: "{{ vars[demo].project.description }}" description: "{{ job_template_definitions[demo].project.description }}"
organization: "{{ vars[demo].project.organization }}" organization: "{{ job_template_definitions[demo].project.organization }}"
scm_type: "{{ vars[demo].project.scm_type }}" scm_type: "{{ job_template_definitions[demo].project.scm_type }}"
scm_url: "{{ vars[demo].project.scm_url }}" scm_url: "{{ job_template_definitions[demo].project.scm_url }}"
tower_username: "{{ my_tower_username }}" tower_username: "{{ my_tower_username }}"
tower_password: "{{ my_tower_password }}" tower_password: "{{ my_tower_password }}"
tower_host: "{{ my_tower_host }}" tower_host: "{{ my_tower_host }}"
@@ -14,58 +14,58 @@
- name: show values - name: show values
debug: debug:
msg: msg:
- "name: {{ vars[demo].name }}" - "name: {{ job_template_definitions[demo].name }}"
- "description: {{ vars[demo].description }}" - "description: {{ job_template_definitions[demo].description }}"
- "job_type: {{ vars[demo].job_type }}" - "job_type: {{ job_template_definitions[demo].job_type }}"
- "inventory: {{ vars[demo].inventory }}" - "inventory: {{ job_template_definitions[demo].inventory }}"
- "project: {{ vars[demo].project.name }}" - "project: {{ job_template_definitions[demo].project.name }}"
- "playbook: {{ vars[demo].playbook }}" - "playbook: {{ job_template_definitions[demo].playbook }}"
- "fact_caching_enabled: {{ vars[demo].fact_caching_enabled | default('false') }}" - "fact_caching_enabled: {{ job_template_definitions[demo].fact_caching_enabled | default('false') }}"
- "credential: {{ vars[demo].credential }}" - "credential: {{ job_template_definitions[demo].credential }}"
- "survey_enabled: {{ vars[demo].survey_enabled | default('false') }}" - "survey_enabled: {{ job_template_definitions[demo].survey_enabled | default('false') }}"
- name: add single job template without survey - name: add single job template without survey
awx.awx.tower_job_template: awx.awx.tower_job_template:
name: "{{ vars[demo].name }}" name: "{{ job_template_definitions[demo].name }}"
description: "{{ vars[demo].description }}" description: "{{ job_template_definitions[demo].description }}"
job_type: "{{ vars[demo].job_type }}" job_type: "{{ job_template_definitions[demo].job_type }}"
inventory: "{{ vars[demo].inventory }}" inventory: "{{ job_template_definitions[demo].inventory }}"
project: "{{ vars[demo].project.name }}" project: "{{ job_template_definitions[demo].project.name }}"
playbook: "{{ vars[demo].playbook }}" playbook: "{{ job_template_definitions[demo].playbook }}"
fact_caching_enabled: "{{ vars[demo].fact_caching_enabled | default('false') }}" fact_caching_enabled: "{{ job_template_definitions[demo].fact_caching_enabled | default('false') }}"
credential: "{{ vars[demo].credential }}" credential: "{{ job_template_definitions[demo].credential }}"
survey_enabled: "{{ vars[demo].survey_enabled }}" survey_enabled: "{{ job_template_definitions[demo].survey_enabled }}"
tower_username: "{{ my_tower_username }}" tower_username: "{{ my_tower_username }}"
tower_password: "{{ my_tower_password }}" tower_password: "{{ my_tower_password }}"
tower_host: "{{ my_tower_host }}" tower_host: "{{ my_tower_host }}"
validate_certs: false validate_certs: false
when: when:
- workshop_type in vars[demo].workshop_type - workshop_type in job_template_definitions[demo].workshop_type
- not vars[demo].survey_enabled - not job_template_definitions[demo].survey_enabled
register: add_job_template register: add_job_template
until: add_job_template is not failed until: add_job_template is not failed
retries: 5 retries: 5
- name: add single job template with survey - name: add single job template with survey
awx.awx.tower_job_template: awx.awx.tower_job_template:
name: "{{ vars[demo].name }}" name: "{{ job_template_definitions[demo].name }}"
description: "{{ vars[demo].description }}" description: "{{ job_template_definitions[demo].description }}"
job_type: "{{ vars[demo].job_type }}" job_type: "{{ job_template_definitions[demo].job_type }}"
inventory: "{{ vars[demo].inventory }}" inventory: "{{ job_template_definitions[demo].inventory }}"
project: "{{ vars[demo].project.name }}" project: "{{ job_template_definitions[demo].project.name }}"
playbook: "{{ vars[demo].playbook }}" playbook: "{{ job_template_definitions[demo].playbook }}"
fact_caching_enabled: "{{ vars[demo].fact_caching_enabled | default('false') }}" fact_caching_enabled: "{{ job_template_definitions[demo].fact_caching_enabled | default('false') }}"
credential: "{{ vars[demo].credential }}" credential: "{{ job_template_definitions[demo].credential }}"
survey_enabled: "{{ vars[demo].survey_enabled }}" survey_enabled: "{{ job_template_definitions[demo].survey_enabled }}"
survey_spec: "{{ vars[demo].survey_spec }}" survey_spec: "{{ job_template_definitions[demo].survey_spec }}"
tower_username: "{{ my_tower_username }}" tower_username: "{{ my_tower_username }}"
tower_password: "{{ my_tower_password }}" tower_password: "{{ my_tower_password }}"
tower_host: "{{ my_tower_host }}" tower_host: "{{ my_tower_host }}"
validate_certs: false validate_certs: false
when: when:
- workshop_type in vars[demo].workshop_type - workshop_type in job_template_definitions[demo].workshop_type
- vars[demo].survey_enabled - job_template_definitions[demo].survey_enabled
register: add_job_template register: add_job_template
until: add_job_template is not failed until: add_job_template is not failed
retries: 5 retries: 5

View File

@@ -2,7 +2,8 @@
- name: include all demo vars from install_demo roll - name: include all demo vars from install_demo roll
include_vars: include_vars:
dir: "vars" dir: "vars"
name: job_template_definitions
- name: will load vars from adjacent vars - name: will load vars
debug: debug:
msg: "variables loaded from install_demo role" msg: "variables loaded from install_demo role into job_template_definitions"

View File

@@ -1,21 +1,24 @@
--- ---
- name: explicitly load vars under a subkey for reliable access
include_tasks: load_vars.yml
- name: set facts from role vars - name: set facts from role vars
set_fact: set_fact:
demo_list: "{{ (dict(vars|dictsort|rejectattr('0', 'match', 'ansible_')|selectattr('1.name', 'defined')|selectattr('1.workshop_type', 'defined'))|dict2items)|map(attribute='key')|list }}" demo_list: "{{ ( job_template_definitions|map(attribute='key')|list }}"
full_demo_info: "{{ (dict(vars|dictsort|rejectattr('0', 'match', 'ansible_')|selectattr('1.name', 'defined')|selectattr('1.workshop_type', 'defined'))|dict2items) }}" full_demo_info: "{{ job_template_definitions|dict2items) }}"
- name: provide info to terminal window - name: provide info to terminal window
debug: debug:
msg: msg:
- "install {{ vars.demo }} on {{ my_tower_host }}" - "install {{ demo }} on {{ my_tower_host }}"
- "total demos: {{ demo_list | length }}" - "total demos: {{ demo_list | length }}"
- "available demos are: {{ demo_list }}" - "available demos are: {{ demo_list }}"
- name: make sure demo is a valid demo - name: make sure demo is a valid demo
assert: assert:
that: that:
- vars.demo is defined - demo is defined
- vars.demo in demo_list or vars.demo == "all" - demo in demo_list or demo == "all"
msg: msg:
- "demo must be defined and be one of: {{ demo_list }}" - "demo must be defined and be one of: {{ demo_list }}"
- "full list can be found on https://github.com/ansible/product-demos" - "full list can be found on https://github.com/ansible/product-demos"
@@ -35,11 +38,11 @@
include_tasks: job_template.yml include_tasks: job_template.yml
when: when:
- demo != "all" - demo != "all"
- vars[demo].workflow is not defined or not vars[demo].workflow - job_template_definitions[demo].workflow is not defined or not job_template_definitions[demo].workflow
- name: install single workflow - name: install single workflow
include_tasks: workflow.yml include_tasks: workflow.yml
when: when:
- demo != "all" - demo != "all"
- vars[demo].workflow is defined - job_template_definitions[demo].workflow is defined
- vars[demo].workflow - job_template_definitions[demo].workflow

View File

@@ -1,16 +1,16 @@
--- ---
# these tasks will install a workflow # these tasks will install a workflow
- name: "install all job templates in relation to workflow {{ hostvars[inventory_hostname][demo].name }}" - name: "install all job templates in relation to workflow {{ job_template_definitions[demo].name }}"
include_tasks: add_job_template.yml include_tasks: add_job_template.yml
loop: "{{ hostvars[inventory_hostname][demo].job_templates|dict2items }}" loop: "{{ job_template_definitions[demo].job_templates|dict2items }}"
- name: "install workflow template {{ hostvars[inventory_hostname][demo].name }}" - name: "install workflow template {{ job_template_definitions[demo].name }}"
awx.awx.tower_workflow_template: awx.awx.tower_workflow_template:
name: "{{ hostvars[inventory_hostname][demo].name }}" name: "{{ job_template_definitions[demo].name }}"
description: "{{ hostvars[inventory_hostname][demo].description }}" description: "{{ job_template_definitions[demo].description }}"
organization: "{{ hostvars[inventory_hostname][demo].organization }}" organization: "{{ job_template_definitions[demo].organization }}"
schema: "{{ hostvars[inventory_hostname][demo].schema }}" schema: "{{ job_template_definitions[demo].schema }}"
tower_username: "{{ my_tower_username }}" tower_username: "{{ my_tower_username }}"
tower_password: "{{ my_tower_password }}" tower_password: "{{ my_tower_password }}"
tower_host: "{{ my_tower_host }}" tower_host: "{{ my_tower_host }}"