# Setting -f from within a playbook

**URL:** <https://forum.ansible.com/t/setting-f-from-within-a-playbook/12492>\
**Category:** Ansible Project\
**Created:** [March 13, 2013, 5:07pm UTC](https://forum.ansible.com/t/setting-f-from-within-a-playbook/12492 "2013-03-13T17:07:15Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yves\_Dorfsman](https://avatars.discourse-cdn.com/v4/letter/y/9de0a6/32.png) [@Yves\_Dorfsman](https://forum.ansible.com/u/Yves_Dorfsman)\
**Post date:** [March 13, 2013, 5:07pm UTC](https://forum.ansible.com/t/setting-f-from-within-a-playbook/12492/1 "2013-03-13T17:07:15Z")

</div>

Is there a way to set the number of forks from within a playbook?

The default is fine for most of our playbooks, but one of them benefits from a higher number. I don't want to rely on people remembering to set -f20 for this particular playbook (that's why we use ansible, humans are unreliable!).

---

<div class="post-metadata">

**Author:** ![Yves\_Dorfsman](https://avatars.discourse-cdn.com/v4/letter/y/9de0a6/32.png) [@Yves\_Dorfsman](https://forum.ansible.com/u/Yves_Dorfsman)\
**Post date:** [March 20, 2013, 5:02pm UTC](https://forum.ansible.com/t/setting-f-from-within-a-playbook/12492/2 "2013-03-20T17:02:57Z")

</div>

Anyway to do this?

Now I have another case where we install a piece of software that registers against a manager, but, that manager server can only accept one registration at a time... Being able to force "-f 1" (forks = 1 ???) would solve this issue (and the one above!).

Because we don't want to rely on human memory (remembering to use -f1 when running the playbook, or running the playbook once against each host with -l), we ended up creating one group per host - clunky!

---

<div class="post-metadata">

**Author:** ![benno\_joy](https://avatars.discourse-cdn.com/v4/letter/b/73ab20/32.png) [@benno\_joy](https://forum.ansible.com/u/benno_joy)\
**Post date:** [March 20, 2013, 5:16pm UTC](https://forum.ansible.com/t/setting-f-from-within-a-playbook/12492/3 "2013-03-20T17:16:10Z")

</div>

Yves,

I think you can use the ‘serial:’ option here, maybe there’s a better way.

serial: 1 means only one host at a time  
serial: 30 , 30 forks

Thanks,  
Benno

---

<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:** [March 20, 2013, 5:46pm UTC](https://forum.ansible.com/t/setting-f-from-within-a-playbook/12492/4 "2013-03-20T17:46:40Z")

</div>

Serial does not set the number of forks, it addresses the number of  
hosts to manage at one time versus doing the first task across the  
top.

I agree it should be made a playbook setting, but it's also  
configurable in the configuration file, so this should be sufficient  
for most.

--Michael

---

<div class="post-metadata">

**Author:** ![Yves\_Dorfsman](https://avatars.discourse-cdn.com/v4/letter/y/9de0a6/32.png) [@Yves\_Dorfsman](https://forum.ansible.com/u/Yves_Dorfsman)\
**Post date:** [March 20, 2013, 5:47pm UTC](https://forum.ansible.com/t/setting-f-from-within-a-playbook/12492/5 "2013-03-20T17:47:01Z")

</div>

Ah! I've just done a bunch of tests....

-it works well to limit the number of forks on tasks (serial \< 5)

-it never works on gather\_facts. gather\_facts is always run in parallel, five hosts at a time regardless of the serial option (unless -f is used)

-it does not work if you set serial \> 5, it still runs 5 hosts at once (both for gather\_facts and a task)

---

<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:** [March 20, 2013, 5:48pm UTC](https://forum.ansible.com/t/setting-f-from-within-a-playbook/12492/6 "2013-03-20T17:48:43Z")

</div>

serial is the construct used to define rolling updates behavior.

and only that.

---

<div class="post-metadata">

**Author:** ![Yves\_Dorfsman](https://avatars.discourse-cdn.com/v4/letter/y/9de0a6/32.png) [@Yves\_Dorfsman](https://forum.ansible.com/u/Yves_Dorfsman)\
**Post date:** [March 20, 2013, 5:48pm UTC](https://forum.ansible.com/t/setting-f-from-within-a-playbook/12492/7 "2013-03-20T17:48:52Z")

</div>

But aren't the settings in the configuration file applied to all the playbooks?

I'm looking for a set'table fork on a per playbook basis (I only have a few playbooks where I need a high fork number).

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [March 20, 2013, 5:56pm UTC](https://forum.ansible.com/t/setting-f-from-within-a-playbook/12492/8 "2013-03-20T17:56:47Z")

</div>

I understand.

You may wish to try Brian Coca's trick of putting a shebang line in  
your playbook.

I thought that was pretty clever!

#!/usr/bin/ansible-playbook --forks 20

etc

I agree it should be a feature, please open a github ticket so we can track it.

--Michael

---

<div class="post-metadata">

**Author:** ![Yves\_Dorfsman](https://avatars.discourse-cdn.com/v4/letter/y/9de0a6/32.png) [@Yves\_Dorfsman](https://forum.ansible.com/u/Yves_Dorfsman)\
**Post date:** [March 20, 2013, 6:31pm UTC](https://forum.ansible.com/t/setting-f-from-within-a-playbook/12492/9 "2013-03-20T18:31:01Z")

</div>

Is -f an upper limit or an absolute number of processes started?

If I have a group of host with 3 hosts in it, will ansible-playbook spin up 5 processes anyway (assuming forks=5), or will it only fork what he needs?

I'm trying to figure out the disadvantages of setting a high number of fork for all playbooks, even the ones that don't necessarily need it.

Thanks.

---

<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:** [March 20, 2013, 8:33pm UTC](https://forum.ansible.com/t/setting-f-from-within-a-playbook/12492/10 "2013-03-20T20:33:36Z")

</div>

forks just uses multiprocessing.py straight, so it will create 5 if --forks=5.

Given sufficient memory, --forks 50 is not unheard of, but can get a  
little RAM hungry at those levels.

Generally folks are almost always setting the serial value at those  
levels (rolling update mode) and it does not make sense to have forks  
exceed the serial count.

There really isn't a reason we can't auto-minimize the fork count when  
it is not needed.

--Michael

---

<div class="post-metadata">

**Author:** ![Yuri\_Lukyanov](https://avatars.discourse-cdn.com/v4/letter/y/ed655f/32.png) [@Yuri\_Lukyanov](https://forum.ansible.com/u/Yuri_Lukyanov)\
**Post date:** [January 30, 2014, 11:38am UTC](https://forum.ansible.com/t/setting-f-from-within-a-playbook/12492/11 "2014-01-30T11:38:23Z")

</div>

Are there any new considerations on this?

Being able to specify a number of forks in a play would be really helpful.

My use case is that in a playbook I have a play to ‘git pull’ and some other plays (compile the code, for example).  
The thing is that the git server I use has some restrictions on the number of simultaneous connections.  
So I’d like to run the first play with a certain number of forks and the rest as much parallel as possible.

The workaround for me would be to use ‘serial’, but it runs slowly as it waits all the hosts in a series and only then runs the next one.

---

<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 30, 2014, 2:30pm UTC](https://forum.ansible.com/t/setting-f-from-within-a-playbook/12492/12 "2014-01-30T14:30:00Z")

</div>

Nothing at this time.
