On the play-level, we have serial: 1
to allow us to run the whole play one host at a time. But I haven't found a simple way to do this on a single task. This is especially relevant, if the task in question doesn't perform proper locking (for whatever reason).
One obvious answer is to put the task in its own play. But that doesn't help with roles. (Having to put serial: 1
on the play using the role isn't really intuitive.)
If you don't want any parallelism in performing the steps in your playbook, set the fork level to 1:
You can also put this in your ansible cfg file:
but if you want it on an individual basis, use the command line option above.
EDIT:
serial: 1
does something completely different: that is like running the playbook for each host in turn, waiting for completion of the complete playbook before moving on to the next host.forks=1
means run the first task in a play on one host before running the same task on the next host, so the first task will be run for each host before the next task is touched.So you want
forks=1
for just one play; unfortunately that is not currently possible.You can run a single task in serial (i.e. host-by-host) by adding
throttle: 1
to it.Example:
References:
NB:
throttle
was introduced in Ansible 2.9There's a workaround to this problem - one can pass list of hosts (or a group) to
with_items
, and then usedelegate_to
with this list. This way task will be executed host by host.For example:
If you are executing it on a single machine , then exclusive locks issue arises for more than one host .So you should execute one by one for all the hosts .For this you need to have
--forks=1
being set when calling ansible playbook command. FOr example:ansible-playbook webserver.yml --forks=1
where webserver.yml has app01 and app02 inside your[webserver]
For commands that can be run locally, use a loop to iterate over all the hosts in the play. This ONLY works if the command can be run locally. You could also run a command with ssh in it to the remote machines one by one in this manner, if keys are setup, but it becomes difficult when talking about escalation.
EG:
Think what you want is
run_once: true