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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
|
# test code for the async keyword
# (c) 2014, James Tanner <tanner.jc@gmail.com>
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
- name: run a 2 second loop
shell: for i in $(seq 1 2); do echo $i ; sleep 1; done;
async: 10
poll: 1
register: async_result
- debug: var=async_result
- name: validate async returns
assert:
that:
- "'ansible_job_id' in async_result"
- "'changed' in async_result"
- "'cmd' in async_result"
- "'delta' in async_result"
- "'end' in async_result"
- "'rc' in async_result"
- "'start' in async_result"
- "'stderr' in async_result"
- "'stdout' in async_result"
- "'stdout_lines' in async_result"
- async_result.rc == 0
- async_result.finished == 1
- async_result is finished
- name: assert temp async directory exists
stat:
path: "~/.ansible_async"
register: dir_st
- assert:
that:
- dir_st.stat.isdir is defined and dir_st.stat.isdir
- name: stat temp async status file
stat:
path: "~/.ansible_async/{{ async_result.ansible_job_id }}"
register: tmp_async_file_st
- name: validate automatic cleanup of temp async status file on completed run
assert:
that:
- not tmp_async_file_st.stat.exists
- name: test async without polling
command: sleep 5
async: 30
poll: 0
register: async_result
- debug: var=async_result
- name: validate async without polling returns
assert:
that:
- "'ansible_job_id' in async_result"
- "'started' in async_result"
- async_result.finished == 0
- async_result is not finished
- name: test skipped task handling
command: /bin/true
async: 15
poll: 0
when: False
# test async "fire and forget, but check later"
- name: 'start a task with "fire-and-forget"'
command: sleep 3
async: 30
poll: 0
register: fnf_task
- name: assert task was successfully started
assert:
that:
- fnf_task.started
- "'ansible_job_id' in fnf_task"
- name: 'check on task started as a "fire-and-forget"'
async_status: jid={{ fnf_task.ansible_job_id }}
register: fnf_result
until: fnf_result is finished
retries: 10
delay: 1
- name: assert task was successfully checked
assert:
that:
- fnf_result.finished == 1
- fnf_result is finished
- name: test graceful module failure
async_test:
fail_mode: graceful
async: 30
poll: 1
register: async_result
ignore_errors: true
- name: assert task failed correctly
assert:
that:
- async_result.ansible_job_id is match('j\d+\.\d+')
- async_result.finished
- async_result.changed is false
- async_result.failed
- async_result.msg == 'failed gracefully'
- name: test exception module failure
async_test:
fail_mode: exception
async: 5
poll: 1
register: async_result
ignore_errors: true
- name: validate response
assert:
that:
- async_result.ansible_job_id is match('j\d+\.\d+')
- async_result.finished == 1
- async_result is finished
- async_result is not changed
- async_result.finished
- async_result.changed is false
- async_result.failed
- async_result is failed
- async_result.msg is contains 'failing via exception'
# DTFIX5: enable tracebacks, ensure exception is populated
#- async_result.exception is contains 'failing via exception'
- name: test leading junk before JSON
async_test:
fail_mode: leading_junk
async: 5
poll: 1
register: async_result
- name: validate response
assert:
that:
- async_result.ansible_job_id is match('j\d+\.\d+')
- async_result.finished
- async_result.changed
- async_result.failed is false
- name: test trailing junk after JSON
async_test:
fail_mode: trailing_junk
async: 5
poll: 1
register: async_result
- name: validate response
assert:
that:
- async_result.ansible_job_id is match('j\d+\.\d+')
- async_result.finished
- async_result.changed
- async_result.failed is false
- async_result.warnings[0] is search('trailing junk after module output')
- name: test stderr handling
async_test:
fail_mode: stderr
async: 30
poll: 1
register: async_result
ignore_errors: true
- assert:
that:
- async_result.stderr == "printed to stderr\n"
# NOTE: This should report a warning that cannot be tested
- name: test async properties on non-async task
command: sleep 1
register: non_async_result
- name: validate response
assert:
that:
- non_async_result is successful
- non_async_result is changed
- non_async_result is finished
- "'ansible_job_id' not in non_async_result"
- name: set fact of custom tmp dir
set_fact:
custom_async_tmp: ~/.ansible_async_test
- name: ensure custom async tmp dir is absent
file:
path: '{{ custom_async_tmp }}'
state: absent
- block:
- name: run async task with custom dir
command: sleep 1
register: async_custom_dir
async: 5
poll: 1
vars:
ansible_async_dir: '{{ custom_async_tmp }}'
- name: check if the async temp dir is created
stat:
path: '{{ custom_async_tmp }}'
register: async_custom_dir_result
- name: assert run async task with custom dir
assert:
that:
- async_custom_dir is successful
- async_custom_dir is finished
- async_custom_dir_result.stat.exists
- name: remove custom async dir again
file:
path: '{{ custom_async_tmp }}'
state: absent
- name: remove custom async dir after deprecation test
file:
path: '{{ custom_async_tmp }}'
state: absent
- name: run fire and forget async task with custom dir
command: echo moo
register: async_fandf_custom_dir
async: 5
poll: 0
vars:
ansible_async_dir: '{{ custom_async_tmp }}'
- name: fail to get async status with custom dir with defaults
async_status:
jid: '{{ async_fandf_custom_dir.ansible_job_id }}'
register: async_fandf_custom_dir_fail
ignore_errors: yes
- name: get async status with custom dir using newer format
async_status:
jid: '{{ async_fandf_custom_dir.ansible_job_id }}'
register: async_fandf_custom_dir_result
vars:
ansible_async_dir: '{{ custom_async_tmp }}'
- name: assert run fire and forget async task with custom dir
assert:
that:
- async_fandf_custom_dir is successful
- async_fandf_custom_dir_fail is failed
- async_fandf_custom_dir_fail.msg == "could not find job"
- async_fandf_custom_dir_result is successful
always:
- name: remove custom tmp dir after test
file:
path: '{{ custom_async_tmp }}'
state: absent
- name: Test that async has stdin
command: >
{{ ansible_python_interpreter|default('/usr/bin/python') }} -c 'import os; os.fdopen(os.dup(0), "r")'
async: 1
poll: 1
- name: run async poll callback test playbook
command: ansible-playbook {{ role_path }}/callback_test.yml
delegate_to: localhost
register: callback_output
- assert:
that:
- '"ASYNC POLL on localhost" in callback_output.stdout'
- name: run playbook in --check with task disabling check mode
command: ansible-playbook {{ role_path }}/check_task_test.yml --check
register: check_task_disabled_output
delegate_to: localhost
environment:
ANSIBLE_NOCOLOR: 'true'
ANSIBLE_FORCE_COLOR: 'false'
- assert:
that:
- '"ASYNC OK on localhost" in check_task_disabled_output.stdout'
- 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.
gather_facts:
parallel: true
timeout: 60
check_mode: true
when: "ansible_facts['os_family'] != 'Darwin'"
vars:
ansible_facts_modules:
- setup
- service_facts
- name: test async in check mode
check_mode: true
block:
- setup:
async: 60
poll: 0
register: gf_async
- async_status:
jid: "{{ gf_async['ansible_job_id'] }}"
register: gf_done
until: gf_done is finished
retries: 100
delay: 10
timeout: 30
|