1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
- name: Assert playbook properties
hosts: localhost
gather_facts: yes
vars:
ara_playbook_name: ARA self tests
ara_playbook_labels:
- lookup-tests
tasks:
- name: Retrieve the current playbook so we can get the ID
ara_playbook:
register: playbook_query
- name: Recover data from ARA
vars:
playbook_id: "{{ playbook_query.playbook.id | string }}"
set_fact:
playbook: "{{ lookup('ara_api', '/api/v1/playbooks/' + playbook_id) }}"
tasks: "{{ lookup('ara_api', '/api/v1/tasks?playbook=' + playbook_id) }}"
results: "{{ lookup('ara_api', '/api/v1/results?playbook=' + playbook_id) }}"
hosts: "{{ lookup('ara_api', '/api/v1/hosts?playbook=' + playbook_id) }}"
- name: Assert playbook properties
assert:
that:
- playbook.name == 'ARA self tests'
- "playbook.labels | selectattr('name', 'search', 'lookup-tests') | list | length == 1"
- playbook.ansible_version == ansible_version.full
- playbook_dir in playbook.path
- "'tests/integration/lookups.yaml' in playbook.path"
# TODO: Validate when set from configuration file too
- name: Validate hostname when localhost_as_hostname is enabled
vars:
_localhost_as_hostname: "{{ lookup('env', 'ARA_LOCALHOST_AS_HOSTNAME') | default(false) }}"
assert:
that:
- hosts['results'][0]['name'] != inventory_hostname
- hosts['results'][0]['name'] != 'localhost'
- inventory_hostname == 'localhost'
success_msg: "inventory_hostname '{{ inventory_hostname }}' != {{ hosts['results'][0]['name'] }}"
fail_msg: |
localhost_as_hostname is enabled but the inventory hostname is still localhost...
It should be the hostname of the controller.
when: _localhost_as_hostname | bool
- name: Validate hostname when localhost_as_hostname is not enabled
vars:
_localhost_as_hostname: "{{ lookup('env', 'ARA_LOCALHOST_AS_HOSTNAME') | default(false) }}"
assert:
that:
- inventory_hostname == 'localhost'
- hosts['results'][0]['name'] == inventory_hostname
success_msg: "inventory_hostname {{ inventory_hostname }} == {{ hosts['results'][0]['name'] }}"
fail_msg: |
localhost_as_hostname isn't enabled
The inventory_hostname is localhost but the host is not localhost (???)
when: not _localhost_as_hostname | bool
#####
# Examples taken from docs on Ansible plugins and use cases
#####
- name: Get failed results
set_fact:
failed: "{{ lookup('ara_api', '/api/v1/results?status=failed') }}"
- name: Print task data from failed results
vars:
task_id: "{{ item.task | string }}"
task: "{{ lookup('ara_api', '/api/v1/tasks/' + task_id ) }}"
host_id: "{{ item.host | string }}"
host: "{{ lookup('ara_api', '/api/v1/hosts/' + host_id) }}"
debug:
msg: "{{ host.name }} failed | {{ task.name }} ({{ task.path }}:{{ task.lineno }})"
loop: "{{ failed.results }}"
- name: Get the currently running playbook
ara_playbook:
register: query
- name: Retrieve playbook id
set_fact:
playbook_id: "{{ query.playbook.id | string }}"
# With the playbook id we can create a link to the playbook report
- name: Recover base url from ara
set_fact:
api_base_url: "{{ lookup('ara_api', '/api/') }}"
- name: Print link to playbook report
vars:
ui_base_url: "{{ api_base_url.api[0] | regex_replace('/api/v1/', '') }}"
debug:
msg: "{{ ui_base_url }}/playbooks/{{ playbook_id }}.html"
|