Set forks for one task in ansible playbook

和自甴很熟 提交于 2020-01-03 18:52:03

问题


I have one task in my playbook that I want to run on only one host at a time. The rest of the tasks I want to honor the --forks option. Is this possible?


回答1:


Yes checkout rolling updates

specifically you can add a "serial: x" parameter for a play where x is the number of forks




回答2:


In the glossary of terms documentation for Ansible there's this paragraph

Rolling Update

The act of addressing a number of nodes in a group N at a time to avoid updating them all at once and bringing the system offline. For instance, in a web topology of 500 nodes handling very large volume, it may be reasonable to update 10 or 20 machines at a time, moving on to the next 10 or 20 when done. The serial: keyword in an Ansible playbooks control the size of the rolling update pool. The default is to address the batch size all at once, so this is something that you must opt-in to. OS configuration (such as making sure config files are correct) does not typically have to use the rolling update model, but can do so if desired.

Using the --forks=X along with serial: playbook constructs it's not possible to limit a single task to run sequentially. That's because the serial: keyword is only applicable to playbooks. Your only recourse here is to split your tasks up into 2 playbooks and limit the entire play which includes the task you want to run sequentially to serial: 1.

Example

$ cat helloansible.yml
---
- hosts: all
  serial: 1

  tasks:
    - debug:
        msg: "hello ansible - serial"

- hosts: all

  tasks:
    - debug:
        msg: "hello ansible - batched"

When I run this against an inventory you'll see the 1st playbook iterate over each host in the inventory:

$ ansible-playbook -i inventory/lab helloansible.yml -l server*

PLAY [all] **************************************************************************************************************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************************************************************************************
ok: [server-01a.lab1.mydom.local]

TASK [debug] ************************************************************************************************************************************************************************************************
ok: [server-01a.lab1.mydom.local] => {
    "msg": "hello ansible - serial"
}

PLAY [all] **************************************************************************************************************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************************************************************************************
ok: [server-01b.lab1.mydom.local]

TASK [debug] ************************************************************************************************************************************************************************************************
ok: [server-01b.lab1.mydom.local] => {
    "msg": "hello ansible - serial"
}

PLAY [all] **************************************************************************************************************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************************************************************************************
ok: [server-redis-01a.lab1.mydom.local]

TASK [debug] ************************************************************************************************************************************************************************************************
ok: [server-redis-01a.lab1.mydom.local] => {
    "msg": "hello ansible - serial"
}

PLAY [all] **************************************************************************************************************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************************************************************************************
ok: [server-redis-01b.lab1.mydom.local]

TASK [debug] ************************************************************************************************************************************************************************************************
ok: [server-redis-01b.lab1.mydom.local] => {
    "msg": "hello ansible - serial"
}

PLAY [all] **************************************************************************************************************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************************************************************************************
ok: [server-redis-01c.lab1.mydom.local]

TASK [debug] ************************************************************************************************************************************************************************************************
ok: [server-redis-01c.lab1.mydom.local] => {
    "msg": "hello ansible - serial"
}

Whereas in the 2nd playbook that runs:

PLAY [all] **************************************************************************************************************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************************************************************************************
ok: [server-01b.lab1.mydom.local]
ok: [server-redis-01a.lab1.mydom.local]
ok: [server-redis-01c.lab1.mydom.local]
ok: [server-redis-01b.lab1.mydom.local]
ok: [server-01a.lab1.mydom.local]

TASK [debug] ************************************************************************************************************************************************************************************************
ok: [server-01a.lab1.mydom.local] => {
    "msg": "hello ansible - batched"
}
ok: [server-01b.lab1.mydom.local] => {
    "msg": "hello ansible - batched"
}
ok: [server-redis-01a.lab1.mydom.local] => {
    "msg": "hello ansible - batched"
}
ok: [server-redis-01b.lab1.mydom.local] => {
    "msg": "hello ansible - batched"
}
ok: [server-redis-01c.lab1.mydom.local] => {
    "msg": "hello ansible - batched"
}

PLAY RECAP **************************************************************************************************************************************************************************************************
server-01a.lab1.mydom.local : ok=4    changed=0    unreachable=0    failed=0
server-01b.lab1.mydom.local : ok=4    changed=0    unreachable=0    failed=0
server-redis-01a.lab1.mydom.local : ok=4    changed=0    unreachable=0    failed=0
server-redis-01b.lab1.mydom.local : ok=4    changed=0    unreachable=0    failed=0
server-redis-01c.lab1.mydom.local : ok=4    changed=0    unreachable=0    failed=0


来源:https://stackoverflow.com/questions/33675888/set-forks-for-one-task-in-ansible-playbook

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!