2014-02-21 16:00:01 +01:00
|
|
|
# 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"
|
2018-07-24 23:21:28 +02:00
|
|
|
- async_result.rc == 0
|
|
|
|
- async_result.finished == 1
|
|
|
|
- async_result is finished
|
2014-02-21 16:00:01 +01:00
|
|
|
|
2021-03-04 20:06:27 +01:00
|
|
|
- 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
|
|
|
|
|
2014-03-26 21:48:42 +01:00
|
|
|
- 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"
|
2018-07-24 23:21:28 +02:00
|
|
|
- async_result.finished == 0
|
|
|
|
- async_result is not finished
|
2014-07-12 19:30:05 +02:00
|
|
|
|
|
|
|
- name: test skipped task handling
|
2016-04-18 16:47:38 +02:00
|
|
|
command: /bin/true
|
2014-07-12 19:30:05 +02:00
|
|
|
async: 15
|
|
|
|
poll: 0
|
|
|
|
when: False
|
2014-09-25 22:42:45 +02:00
|
|
|
|
|
|
|
# test async "fire and forget, but check later"
|
|
|
|
|
|
|
|
- name: 'start a task with "fire-and-forget"'
|
2016-05-31 23:47:36 +02:00
|
|
|
command: sleep 3
|
2014-09-25 22:42:45 +02:00
|
|
|
async: 30
|
|
|
|
poll: 0
|
|
|
|
register: fnf_task
|
|
|
|
|
|
|
|
- name: assert task was successfully started
|
|
|
|
assert:
|
|
|
|
that:
|
2018-07-30 10:41:49 +02:00
|
|
|
- fnf_task.started == 1
|
2018-08-24 01:29:34 +02:00
|
|
|
- fnf_task is started
|
2014-09-25 22:42:45 +02:00
|
|
|
- "'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
|
2018-07-24 23:21:28 +02:00
|
|
|
until: fnf_result is finished
|
2016-05-31 23:47:36 +02:00
|
|
|
retries: 10
|
|
|
|
delay: 1
|
2014-09-25 22:42:45 +02:00
|
|
|
|
|
|
|
- name: assert task was successfully checked
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- fnf_result.finished
|
2018-07-24 23:21:28 +02:00
|
|
|
- fnf_result is finished
|
2016-10-02 17:03:42 +02:00
|
|
|
|
|
|
|
- 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('\d+\.\d+')
|
|
|
|
- async_result.finished == 1
|
2018-07-24 23:21:28 +02:00
|
|
|
- async_result is finished
|
|
|
|
- async_result is not changed
|
2017-11-27 23:58:08 +01:00
|
|
|
- async_result is failed
|
2016-10-02 17:03:42 +02:00
|
|
|
- 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('\d+\.\d+')
|
|
|
|
- async_result.finished == 1
|
2018-07-24 23:21:28 +02:00
|
|
|
- async_result is finished
|
2016-10-02 17:03:42 +02:00
|
|
|
- async_result.changed == false
|
2018-07-24 23:21:28 +02:00
|
|
|
- async_result is not changed
|
|
|
|
- async_result.failed == true
|
|
|
|
- async_result is failed
|
2016-10-02 17:03:42 +02:00
|
|
|
- async_result.stderr is search('failing via exception', multiline=True)
|
|
|
|
|
|
|
|
- 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('\d+\.\d+')
|
|
|
|
- async_result.finished == 1
|
2018-07-24 23:21:28 +02:00
|
|
|
- async_result is finished
|
2016-10-02 17:03:42 +02:00
|
|
|
- async_result.changed == true
|
2018-07-24 23:21:28 +02:00
|
|
|
- async_result is changed
|
2017-11-27 23:58:08 +01:00
|
|
|
- async_result is successful
|
2016-10-02 17:03:42 +02:00
|
|
|
|
|
|
|
- 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('\d+\.\d+')
|
|
|
|
- async_result.finished == 1
|
2018-07-24 23:21:28 +02:00
|
|
|
- async_result is finished
|
2016-10-02 17:03:42 +02:00
|
|
|
- async_result.changed == true
|
2018-07-24 23:21:28 +02:00
|
|
|
- async_result is changed
|
2017-11-27 23:58:08 +01:00
|
|
|
- async_result is successful
|
2016-10-02 17:03:42 +02:00
|
|
|
- async_result.warnings[0] is search('trailing junk after module output')
|
2018-07-24 23:21:28 +02:00
|
|
|
|
2020-07-13 18:58:14 +02:00
|
|
|
- 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"
|
|
|
|
|
2018-07-24 23:21:28 +02:00
|
|
|
# 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"
|
2018-09-20 11:37:54 +02:00
|
|
|
|
|
|
|
- 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: run async task with custom dir - deprecated format
|
|
|
|
command: sleep 1
|
|
|
|
register: async_custom_dir_dep
|
|
|
|
async: 5
|
|
|
|
poll: 1
|
|
|
|
environment:
|
|
|
|
ANSIBLE_ASYNC_DIR: '{{ custom_async_tmp }}'
|
|
|
|
|
|
|
|
- name: check if the async temp dir is created - deprecated format
|
|
|
|
stat:
|
|
|
|
path: '{{ custom_async_tmp }}'
|
|
|
|
register: async_custom_dir_dep_result
|
|
|
|
|
|
|
|
- name: assert run async task with custom dir - deprecated format
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- async_custom_dir_dep is successful
|
|
|
|
- async_custom_dir_dep is finished
|
|
|
|
- async_custom_dir_dep_result.stat.exists
|
|
|
|
|
|
|
|
- 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
|
2018-09-25 02:22:06 +02:00
|
|
|
command: echo moo
|
2018-09-20 11:37:54 +02:00
|
|
|
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: get async status with custom dir - deprecated format
|
|
|
|
async_status:
|
|
|
|
jid: '{{ async_fandf_custom_dir.ansible_job_id }}'
|
|
|
|
register: async_fandf_custom_dir_dep_result
|
|
|
|
environment:
|
|
|
|
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
|
2021-06-14 22:39:59 +02:00
|
|
|
- async_fandf_custom_dir_fail.msg == "could not find job"
|
2018-09-20 11:37:54 +02:00
|
|
|
- async_fandf_custom_dir_result is successful
|
|
|
|
- async_fandf_custom_dir_dep_result is successful
|
|
|
|
|
|
|
|
always:
|
|
|
|
- name: remove custom tmp dir after test
|
|
|
|
file:
|
|
|
|
path: '{{ custom_async_tmp }}'
|
|
|
|
state: absent
|
2019-03-07 16:28:44 +01:00
|
|
|
|
|
|
|
- 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
|
2020-08-17 17:51:01 +02:00
|
|
|
|
|
|
|
- name: run async poll callback test playbook
|
|
|
|
command: ansible-playbook {{ role_path }}/callback_test.yml
|
2021-05-01 02:16:30 +02:00
|
|
|
delegate_to: localhost
|
2020-08-17 17:51:01 +02:00
|
|
|
register: callback_output
|
|
|
|
|
|
|
|
- assert:
|
|
|
|
that:
|
|
|
|
- '"ASYNC POLL on localhost" in callback_output.stdout'
|