Any way to make a task serial (or set mutex so only 1 thread run it at a time)?

Came across another ticket describing a change to make a “playbook” serial (I’m guessing that means all tasks in the playbook): https://groups.google.com/forum/#!searchin/ansible-project/task$20serial/ansible-project/ZrSuwsqv1m8/-o-bJTQuJnUJ

would like to have more granular control and just specify particular task(s) as serial, is that possible? Perhaps the intended way of doing this is to create a new playbook for those particular tasks? Seems like overkill but am willing to give it a shot (beats having to run ALL my tasks with forks 1 or serial 1)

That’s a pretty old post.

You should read up on the “serial:” keyword in Ansible.

http://docs.ansible.com/playbooks_delegation.html#id6

serial: 1

The documentation doesn’t really answer the question though. Does that mean there’s no way to define one task as serial? In other words, serial only applies for all tasks?

Correct, a task does not allow the keyword serial to be defined on it. There’s no need to define an entirely new playbook, but you will have to declare a new play.

Yes, the play could just have one task in it.

A serial task in the middle of several non-serial ones would look like this in a single playbook file.

  • hosts: webservers
    tasks:

Looks like it’s going to be possible in v2.0.

Move tasks that should be done in ‘serial: 1’ mode to a separate file. Include it with items iterating over hosts in a group and set some variable to a host name. Add to files in a separate task condition to check if hostname is similar to that variable.