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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
- name: Create fake hosts to test with
hosts: localhost
gather_facts: no
vars:
host_count: 3
tasks:
- name: Add a host to the inventory
add_host:
hostname: "delegator-{{ item }}"
ansible_connection: local
ansible_host: 127.0.0.1
groups: delegated_hostgroup
with_sequence: start=1 end={{ host_count }}
- name: Run a play with delegated tasks
hosts: localhost
gather_facts: no
tasks:
- name: test1 - Run a normal task
command: echo "A normal task"
changed_when: false
- name: test2 - Run a task with delegate_to
command: echo "A delegated task"
changed_when: false
delegate_to: "delegator-1"
# Test for particular behavior when running a loop task with delegate_to
# https://github.com/ansible/ansible/issues/75339
- name: test3 - Run a delegated task with a variable
command: echo "A delegated task to a variable host"
changed_when: false
delegate_to: "{{ item }}"
loop: "{{ groups['delegated_hostgroup'] }}"
- name: test4 - Run a delegated task with a variable inside a conditional loop
command: echo "A delegated task with a loop and some skipped elements"
changed_when: false
delegate_to: "{{ item }}"
loop: "{{ groups['delegated_hostgroup'] }}"
when: item == "delegator-1"
- name: Validate recorded task results
hosts: localhost
gather_facts: no
tasks:
- name: Get the current playbook to retrieve it's id
ara_playbook:
register: playbook_query
- name: Set the playbook id
set_fact:
playbook_id: "{{ playbook_query.playbook.id | string }}"
- name: Validate the first test task
block:
- name: Find the first test task
set_fact:
tasks: "{{ lookup('ara_api', '/api/v1/tasks?name=test1&playbook=' + playbook_id) }}"
- name: Find the first test task result
vars:
task_id: "{{ tasks.results[0].id }}"
set_fact:
results: "{{ lookup('ara_api', '/api/v1/results?task=' + task_id) }}"
- name: Find the host for the first test task
vars:
result: "{{ results.results[0] }}"
set_fact:
# FIXME: Uncertain why "to_json | from json" is necessary here. The lookup returns a dict but trying to
# access it yields: 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'name'
host: "{{ lookup('ara_api', '/api/v1/hosts/' + result.host | string) | to_json | from_json }}"
- name: Validate the result for the first test task
vars:
result: "{{ results.results[0] }}"
assert:
that:
- result.host is integer
- result.delegated_to == []
- host.name == "localhost"
- name: Validate the second test task
block:
- name: Find the second test task
set_fact:
tasks: "{{ lookup('ara_api', '/api/v1/tasks?name=test2&playbook=' + playbook_id) }}"
- name: Find the second test task result
vars:
task_id: "{{ tasks.results[0].id }}"
set_fact:
results: "{{ lookup('ara_api', '/api/v1/results?task=' + task_id) }}"
- name: Find the host for the second test task
vars:
result: "{{ results.results[0] }}"
set_fact:
host: "{{ lookup('ara_api', '/api/v1/hosts/' + result.host | string) | to_json | from_json }}"
delegated_host: "{{ lookup('ara_api', '/api/v1/hosts/' + result.delegated_to[0] | string) | to_json | from_json }}"
- name: Validate the result for the second test task
vars:
result: "{{ results.results[0] }}"
assert:
that:
- result.host is integer
- result.delegated_to | length == 1
- result.host != result.delegated_to[0]
- host.name == "localhost"
- delegated_host.name == "delegator-1"
- name: Validate the third test task
block:
- name: Find the third test task
set_fact:
tasks: "{{ lookup('ara_api', '/api/v1/tasks?name=test3&playbook=' + playbook_id) }}"
- name: Find the third test task result
vars:
task_id: "{{ tasks.results[0].id }}"
set_fact:
results: "{{ lookup('ara_api', '/api/v1/results?task=' + task_id) }}"
- name: Find the hosts for the third test task
vars:
result: "{{ results.results[0] }}"
set_fact:
host: "{{ lookup('ara_api', '/api/v1/hosts/' + result.host | string) | to_json | from_json }}"
first_delegated: "{{ lookup('ara_api', '/api/v1/hosts/' + result.delegated_to[0] | string) | to_json | from_json }}"
second_delegated: "{{ lookup('ara_api', '/api/v1/hosts/' + result.delegated_to[1] | string) | to_json | from_json }}"
third_delegated: "{{ lookup('ara_api', '/api/v1/hosts/' + result.delegated_to[2] | string) | to_json | from_json }}"
- name: Validate the result for the third test task
vars:
result: "{{ results.results[0] }}"
assert:
that:
- result.host is integer
- result.delegated_to | length == groups['delegated_hostgroup'] | length
- result.host not in result.delegated_to
- host.name == 'localhost'
# Validate that we have the actual hostnames and not the variable as-is
- ("delegator" in first_delegated.name and "delegated_hostgroup" not in first_delegated.name)
- ("delegator" in second_delegated.name and "delegated_hostgroup" not in second_delegated.name)
- ("delegator" in third_delegated.name and "delegated_hostgroup" not in third_delegated.name)
- name: Validate the fourth test task
block:
- name: Find the fourth test task
set_fact:
tasks: "{{ lookup('ara_api', '/api/v1/tasks?name=test4&playbook=' + playbook_id) }}"
- name: Find the fourth test task result
vars:
task_id: "{{ tasks.results[0].id }}"
set_fact:
results: "{{ lookup('ara_api', '/api/v1/results?task=' + task_id) }}"
- name: Find the hosts for the fourth test task
vars:
result: "{{ results.results[0] }}"
set_fact:
host: "{{ lookup('ara_api', '/api/v1/hosts/' + result.host | string) | to_json | from_json }}"
delegated_host: "{{ lookup('ara_api', '/api/v1/hosts/' + result.delegated_to[0] | string) | to_json | from_json }}"
- name: Validate the result for the fourth test task
vars:
result: "{{ results.results[0] }}"
assert:
that:
- result.host is integer
# The hostgroup has three hosts but the condition is made so only one goes through, the other two are skipped
- result.delegated_to | length == 1
- result.host not in result.delegated_to
- host.name == 'localhost'
- delegated_host.name == 'delegator-1'
|