Backgrounding an Ansible Process
From bib. source
Backgrounding processes rely on asynchrony that takes advantage of Ansible’s parallelism.
This may be especially useful when “you run dnf update or apt-get update && apt-get dist-upgrade,” as “it could be a few minutes before all the packages on your servers are updated” (Geerling 2023, 37). It is also useful if you are managing many servers through Ansible (Ibid).
Running a command in the background involves use of the following options (Geerling 2023, 38):
-B <seconds>: maximum amount of time in seconds to let the job run-P <seconds>: amount of time in seconds to wait between polling the servers for updated job status
This is exemplified as such (Ibid):
ansible $INVENTORY_FILE_GROUP -b -B $MAX_SECONDS_DURATION -P $SECONDS_PER_POLL -a "dnf -y update"Wherein dnf -y update is now run in the background, and a poll returns information about the status of this job.
So long as this background job is running, it is possible to “check on the status elsewhere using Ansible’s async_status module” (Ibid). However, one needs to pass in the Ansible job identification value, also called the JID, to do so (Ibid).
The JID is a field, jid, in the argument supplied for the async_status module using the ansible command’s a option (see 20250322150219-Ansible_Modules_via_CLI), exemplified in the following (Geerling 2023, 39):
ansible $INVENTORY_FILE_GROUP -b -m async_status -a "jid=${ANSIBLE_JOB_ID}"The value for the particular job one wants is found in the report on the job status provided when backgrounding that job with the aforementioned P option, and it exists in that report as an ansible_job_id field value.
More on modules
For background on some of these options, see 20250322150219-Ansible_Modules_via_CLI.
configuration_management bash bash_script bash_scripting Bourne_Again_Shell Bourne_Again_Shell_script Bourne_Again_Shell_scripting asynchrony background_process arguments fields package server configuration_management ad-hoc_command ad-hoc_commands
bibliography
- “Ad-Hoc Commands.” In Ansible for DevOps: Server and Configuration Management for Humans, 2nd ed., 18–46. Leanpub, 2023.