Background Task Execution
We can do background task execution with -B
and -P
Basically,
-B
provides the time to kill this process. We can increase the timeout if we want to-P
provides what's the time to do polling (ansible will poll the status to check the status of the task).- Specify
0
if you want ansible to fire and forget
- Specify
So for example, we can execute like this:
ansible -i inventory.yml server -b -m command -a "apt upgrade -y" -B 3600 -P 0
This will be executed in the background and then return the ansible_job_id
.
For example:
[WARNING]: Platform linux on host app3 is using the discovered Python interpreter at /usr/bin/python3.8, but future installation of another Python interpreter could change the meaning of
that path. See https://docs.ansible.com/ansible-core/2.17/reference_appendices/interpreter_discovery.html for more information.
app3 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3.8"
},
"ansible_job_id": "j159068683693.1879",
"changed": true,
"finished": 0,
"results_file": "/root/.ansible_async/j159068683693.1879",
"started": 1
}
[WARNING]: Platform linux on host app1 is using the discovered Python interpreter at /usr/bin/python3.8, but future installation of another Python interpreter could change the meaning of
that path. See https://docs.ansible.com/ansible-core/2.17/reference_appendices/interpreter_discovery.html for more information.
app1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3.8"
},
"ansible_job_id": "j509933346296.1494",
"changed": true,
"finished": 0,
"results_file": "/root/.ansible_async/j509933346296.1494",
"started": 1
}
[WARNING]: Platform linux on host app2 is using the discovered Python interpreter at /usr/bin/python3.8, but future installation of another Python interpreter could change the meaning of
that path. See https://docs.ansible.com/ansible-core/2.17/reference_appendices/interpreter_discovery.html for more information.
app2 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3.8"
},
"ansible_job_id": "j424542566402.1459",
"changed": true,
"finished": 0,
"results_file": "/root/.ansible_async/j424542566402.1459",
"started": 1
}
We can then check the status for each task by using
ansible -i inventory.yml app3 -b -m async_status -a "jid=j159068683693.1879"