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

**URL:** <https://forum.ansible.com/t/any-way-to-make-a-task-serial-or-set-mutex-so-only-1-thread-run-it-at-a-time/14413>\
**Category:** Ansible Project\
**Created:** [January 23, 2014, 11:53pm UTC](https://forum.ansible.com/t/any-way-to-make-a-task-serial-or-set-mutex-so-only-1-thread-run-it-at-a-time/14413 "2014-01-23T23:53:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mike\_Sop](https://avatars.discourse-cdn.com/v4/letter/m/4af34b/32.png) [@Mike\_Sop](https://forum.ansible.com/u/Mike_Sop)\
**Post date:** [January 23, 2014, 11:53pm UTC](https://forum.ansible.com/t/any-way-to-make-a-task-serial-or-set-mutex-so-only-1-thread-run-it-at-a-time/14413/1 "2014-01-23T23:53:50Z")

</div>

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](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)

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [January 24, 2014, 1:06am UTC](https://forum.ansible.com/t/any-way-to-make-a-task-serial-or-set-mutex-so-only-1-thread-run-it-at-a-time/14413/2 "2014-01-24T01:06:38Z")

</div>

That’s a pretty old post.

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

[http://docs.ansible.com/playbooks\_delegation.html#id6](http://docs.ansible.com/playbooks_delegation.html#id6)

serial: 1

---

<div class="post-metadata">

**Author:** ![Khaled\_Janania](https://avatars.discourse-cdn.com/v4/letter/k/ecccb3/32.png) [@Khaled\_Janania](https://forum.ansible.com/u/Khaled_Janania)\
**Post date:** [April 4, 2014, 5:39pm UTC](https://forum.ansible.com/t/any-way-to-make-a-task-serial-or-set-mutex-so-only-1-thread-run-it-at-a-time/14413/3 "2014-04-04T17:39:25Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Paul\_Durivage](https://avatars.discourse-cdn.com/v4/letter/p/94ad74/32.png) [@Paul\_Durivage](https://forum.ansible.com/u/Paul_Durivage)\
**Post date:** [April 6, 2014, 3:10pm UTC](https://forum.ansible.com/t/any-way-to-make-a-task-serial-or-set-mutex-so-only-1-thread-run-it-at-a-time/14413/4 "2014-04-06T15:10:58Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [April 9, 2014, 1:33pm UTC](https://forum.ansible.com/t/any-way-to-make-a-task-serial-or-set-mutex-so-only-1-thread-run-it-at-a-time/14413/5 "2014-04-09T13:33:11Z")

</div>

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:

---

<div class="post-metadata">

**Author:** ![Roman\_Belyakovsky](https://avatars.discourse-cdn.com/v4/letter/r/ecae2f/32.png) [@Roman\_Belyakovsky](https://forum.ansible.com/u/Roman_Belyakovsky)\
**Post date:** [May 20, 2015, 9:45am UTC](https://forum.ansible.com/t/any-way-to-make-a-task-serial-or-set-mutex-so-only-1-thread-run-it-at-a-time/14413/6 "2015-05-20T09:45:33Z")

</div>

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.
