| 1 |
# test code for the async keyword |
| 2 |
# (c) 2014, James Tanner <[email protected]> |
| 3 |
|
| 4 |
# This file is part of Ansible |
| 5 |
# |
| 6 |
# Ansible is free software: you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation, either version 3 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# Ansible is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
- name: run a 2 second loop |
| 20 |
shell: for i in $(seq 1 2); do echo $i ; sleep 1; done; |
| 21 |
async: 10 |
| 22 |
poll: 1 |
| 23 |
register: async_result |
| 24 |
|
| 25 |
|
| 26 |
- debug: var=async_result |
| 27 |
|
| 28 |
- name: validate async returns |
| 29 |
assert: |
| 30 |
that: |
| 31 |
- "'ansible_job_id' in async_result" |
| 32 |
- "'changed' in async_result" |
| 33 |
- "'cmd' in async_result" |
| 34 |
- "'delta' in async_result" |
| 35 |
- "'end' in async_result" |
| 36 |
- "'rc' in async_result" |
| 37 |
- "'start' in async_result" |
| 38 |
- "'stderr' in async_result" |
| 39 |
- "'stdout' in async_result" |
| 40 |
- "'stdout_lines' in async_result" |
| 41 |
- async_result.rc == 0 |
| 42 |
- async_result.finished == 1 |
| 43 |
- async_result is finished |
| 44 |
|
| 45 |
- name: assert temp async directory exists |
| 46 |
stat: |
| 47 |
path: "~/.ansible_async" |
| 48 |
register: dir_st |
| 49 |
|
| 50 |
- assert: |
| 51 |
that: |
| 52 |
- dir_st.stat.isdir is defined and dir_st.stat.isdir |
| 53 |
|
| 54 |
- name: stat temp async status file |
| 55 |
stat: |
| 56 |
path: "~/.ansible_async/{{ async_result.ansible_job_id }}" |
| 57 |
register: tmp_async_file_st |
| 58 |
|
| 59 |
- name: validate automatic cleanup of temp async status file on completed run |
| 60 |
assert: |
| 61 |
that: |
| 62 |
- not tmp_async_file_st.stat.exists |
| 63 |
|
| 64 |
- name: test async without polling |
| 65 |
command: sleep 5 |
| 66 |
async: 30 |
| 67 |
poll: 0 |
| 68 |
register: async_result |
| 69 |
|
| 70 |
- debug: var=async_result |
| 71 |
|
| 72 |
- name: validate async without polling returns |
| 73 |
assert: |
| 74 |
that: |
| 75 |
- "'ansible_job_id' in async_result" |
| 76 |
- "'started' in async_result" |
| 77 |
- async_result.finished == 0 |
| 78 |
- async_result is not finished |
| 79 |
|
| 80 |
- name: test skipped task handling |
| 81 |
command: /bin/true |
| 82 |
async: 15 |
| 83 |
poll: 0 |
| 84 |
when: False |
| 85 |
|
| 86 |
# test async "fire and forget, but check later" |
| 87 |
|
| 88 |
- name: 'start a task with "fire-and-forget"' |
| 89 |
command: sleep 3 |
| 90 |
async: 30 |
| 91 |
poll: 0 |
| 92 |
register: fnf_task |
| 93 |
|
| 94 |
- name: assert task was successfully started |
| 95 |
assert: |
| 96 |
that: |
| 97 |
- fnf_task.started |
| 98 |
- "'ansible_job_id' in fnf_task" |
| 99 |
|
| 100 |
- name: 'check on task started as a "fire-and-forget"' |
| 101 |
async_status: jid={{ fnf_task.ansible_job_id }} |
| 102 |
register: fnf_result |
| 103 |
until: fnf_result is finished |
| 104 |
retries: 10 |
| 105 |
delay: 1 |
| 106 |
|
| 107 |
- name: assert task was successfully checked |
| 108 |
assert: |
| 109 |
that: |
| 110 |
- fnf_result.finished == 1 |
| 111 |
- fnf_result is finished |
| 112 |
|
| 113 |
- name: test graceful module failure |
| 114 |
async_test: |
| 115 |
fail_mode: graceful |
| 116 |
async: 30 |
| 117 |
poll: 1 |
| 118 |
register: async_result |
| 119 |
ignore_errors: true |
| 120 |
|
| 121 |
- name: assert task failed correctly |
| 122 |
assert: |
| 123 |
that: |
| 124 |
- async_result.ansible_job_id is match('j\d+\.\d+') |
| 125 |
- async_result.finished |
| 126 |
- async_result.changed is false |
| 127 |
- async_result.failed |
| 128 |
- async_result.msg == 'failed gracefully' |
| 129 |
|
| 130 |
- name: test exception module failure |
| 131 |
async_test: |
| 132 |
fail_mode: exception |
| 133 |
async: 5 |
| 134 |
poll: 1 |
| 135 |
register: async_result |
| 136 |
ignore_errors: true |
| 137 |
|
| 138 |
- name: validate response |
| 139 |
assert: |
| 140 |
that: |
| 141 |
- async_result.ansible_job_id is match('j\d+\.\d+') |
| 142 |
- async_result.finished == 1 |
| 143 |
- async_result is finished |
| 144 |
- async_result is not changed |
| 145 |
- async_result.finished |
| 146 |
- async_result.changed is false |
| 147 |
- async_result.failed |
| 148 |
- async_result is failed |
| 149 |
- async_result.msg is contains 'failing via exception' |
| 150 |
# DTFIX5: enable tracebacks, ensure exception is populated |
| 151 |
#- async_result.exception is contains 'failing via exception' |
| 152 |
|
| 153 |
- name: test leading junk before JSON |
| 154 |
async_test: |
| 155 |
fail_mode: leading_junk |
| 156 |
async: 5 |
| 157 |
poll: 1 |
| 158 |
register: async_result |
| 159 |
|
| 160 |
- name: validate response |
| 161 |
assert: |
| 162 |
that: |
| 163 |
- async_result.ansible_job_id is match('j\d+\.\d+') |
| 164 |
- async_result.finished |
| 165 |
- async_result.changed |
| 166 |
- async_result.failed is false |
| 167 |
|
| 168 |
- name: test trailing junk after JSON |
| 169 |
async_test: |
| 170 |
fail_mode: trailing_junk |
| 171 |
async: 5 |
| 172 |
poll: 1 |
| 173 |
register: async_result |
| 174 |
|
| 175 |
- debug: |
| 176 |
var: async_result |
| 177 |
|
| 178 |
- name: validate response |
| 179 |
assert: |
| 180 |
that: |
| 181 |
- async_result.ansible_job_id is match('j\d+\.\d+') |
| 182 |
- async_result.finished |
| 183 |
- async_result.changed |
| 184 |
- async_result.failed is false |
| 185 |
- async_result.warnings[0] is search('trailing junk after module output') |
| 186 |
|
| 187 |
- name: test stderr handling |
| 188 |
async_test: |
| 189 |
fail_mode: stderr |
| 190 |
async: 30 |
| 191 |
poll: 1 |
| 192 |
register: async_result |
| 193 |
ignore_errors: true |
| 194 |
|
| 195 |
- assert: |
| 196 |
that: |
| 197 |
- async_result.stderr == "printed to stderr\n" |
| 198 |
|
| 199 |
# NOTE: This should report a warning that cannot be tested |
| 200 |
- name: test async properties on non-async task |
| 201 |
command: sleep 1 |
| 202 |
register: non_async_result |
| 203 |
|
| 204 |
- name: validate response |
| 205 |
assert: |
| 206 |
that: |
| 207 |
- non_async_result is successful |
| 208 |
- non_async_result is changed |
| 209 |
- non_async_result is finished |
| 210 |
- "'ansible_job_id' not in non_async_result" |
| 211 |
|
| 212 |
- name: set fact of custom tmp dir |
| 213 |
set_fact: |
| 214 |
custom_async_tmp: ~/.ansible_async_test |
| 215 |
|
| 216 |
- name: ensure custom async tmp dir is absent |
| 217 |
file: |
| 218 |
path: '{{ custom_async_tmp }}' |
| 219 |
state: absent |
| 220 |
|
| 221 |
- block: |
| 222 |
- name: run async task with custom dir |
| 223 |
command: sleep 1 |
| 224 |
register: async_custom_dir |
| 225 |
async: 5 |
| 226 |
poll: 1 |
| 227 |
vars: |
| 228 |
ansible_async_dir: '{{ custom_async_tmp }}' |
| 229 |
|
| 230 |
- name: check if the async temp dir is created |
| 231 |
stat: |
| 232 |
path: '{{ custom_async_tmp }}' |
| 233 |
register: async_custom_dir_result |
| 234 |
|
| 235 |
- name: assert run async task with custom dir |
| 236 |
assert: |
| 237 |
that: |
| 238 |
- async_custom_dir is successful |
| 239 |
- async_custom_dir is finished |
| 240 |
- async_custom_dir_result.stat.exists |
| 241 |
|
| 242 |
- name: remove custom async dir again |
| 243 |
file: |
| 244 |
path: '{{ custom_async_tmp }}' |
| 245 |
state: absent |
| 246 |
|
| 247 |
- name: remove custom async dir after deprecation test |
| 248 |
file: |
| 249 |
path: '{{ custom_async_tmp }}' |
| 250 |
state: absent |
| 251 |
|
| 252 |
- name: run fire and forget async task with custom dir |
| 253 |
command: echo moo |
| 254 |
register: async_fandf_custom_dir |
| 255 |
async: 5 |
| 256 |
poll: 0 |
| 257 |
vars: |
| 258 |
ansible_async_dir: '{{ custom_async_tmp }}' |
| 259 |
|
| 260 |
- name: fail to get async status with custom dir with defaults |
| 261 |
async_status: |
| 262 |
jid: '{{ async_fandf_custom_dir.ansible_job_id }}' |
| 263 |
register: async_fandf_custom_dir_fail |
| 264 |
ignore_errors: yes |
| 265 |
|
| 266 |
- name: get async status with custom dir using newer format |
| 267 |
async_status: |
| 268 |
jid: '{{ async_fandf_custom_dir.ansible_job_id }}' |
| 269 |
register: async_fandf_custom_dir_result |
| 270 |
vars: |
| 271 |
ansible_async_dir: '{{ custom_async_tmp }}' |
| 272 |
|
| 273 |
- name: assert run fire and forget async task with custom dir |
| 274 |
assert: |
| 275 |
that: |
| 276 |
- async_fandf_custom_dir is successful |
| 277 |
- async_fandf_custom_dir_fail is failed |
| 278 |
- async_fandf_custom_dir_fail.msg == "could not find job" |
| 279 |
- async_fandf_custom_dir_result is successful |
| 280 |
|
| 281 |
always: |
| 282 |
- name: remove custom tmp dir after test |
| 283 |
file: |
| 284 |
path: '{{ custom_async_tmp }}' |
| 285 |
state: absent |
| 286 |
|
| 287 |
- name: Test that async has stdin |
| 288 |
command: > |
| 289 |
{{ ansible_python_interpreter|default('/usr/bin/python') }} -c 'import os; os.fdopen(os.dup(0), "r")' |
| 290 |
async: 5 |
| 291 |
poll: 1 |
| 292 |
|
| 293 |
- name: run async poll callback test playbook |
| 294 |
command: ansible-playbook {{ role_path }}/callback_test.yml |
| 295 |
delegate_to: localhost |
| 296 |
register: callback_output |
| 297 |
|
| 298 |
- assert: |
| 299 |
that: |
| 300 |
- '"ASYNC POLL on localhost" in callback_output.stdout' |
| 301 |
|
| 302 |
- name: run playbook in --check with task disabling check mode |
| 303 |
command: ansible-playbook {{ role_path }}/check_task_test.yml --check |
| 304 |
register: check_task_disabled_output |
| 305 |
delegate_to: localhost |
| 306 |
environment: |
| 307 |
ANSIBLE_NOCOLOR: 'true' |
| 308 |
ANSIBLE_FORCE_COLOR: 'false' |
| 309 |
|
| 310 |
- assert: |
| 311 |
that: |
| 312 |
- '"ASYNC OK on localhost" in check_task_disabled_output.stdout' |
| 313 |
|
| 314 |
- name: test 'hidden' async, gather_facts does internal on parallel true. Ensure it won't freeze in check_mode due to async_status not supporting it. |
| 315 |
gather_facts: |
| 316 |
parallel: true |
| 317 |
gather_timeout: 10 |
| 318 |
timeout: 60 |
| 319 |
check_mode: true |
| 320 |
when: "ansible_facts['os_family'] != 'Darwin'" |
| 321 |
vars: |
| 322 |
ansible_facts_modules: |
| 323 |
- setup |
| 324 |
- service_facts |
| 325 |
|
| 326 |
- name: test async in check mode |
| 327 |
check_mode: true |
| 328 |
block: |
| 329 |
- setup: |
| 330 |
async: 60 |
| 331 |
poll: 0 |
| 332 |
register: gf_async |
| 333 |
- async_status: |
| 334 |
jid: "{{ gf_async['ansible_job_id'] }}" |
| 335 |
register: gf_done |
| 336 |
until: gf_done is finished |
| 337 |
retries: 100 |
| 338 |
delay: 10 |
| 339 |
timeout: 30 |