我想升级MongoDB,并创建了这样的剧本:
- hosts: all
tasks:
- name: Shutdown mongod service
service:
name: mongod
state: stopped
- name: Replace MongoDB binaries
yum:
name: mongod-org
state: present
- name: Start mongod service
service:
name: mongod
state: started
问题是,Ansible首先在所有主机上停止服务这使我的应用程序不可用,然后在所有主机上进行升级,最后在所有主机上再次启动服务。
如何为每个主机依次运行这些任务,即
我尝试使用block
和include_tasks
,但结果总是这样:
TASK [Shutdown mongod service] ***********************************************
changed: [d-mipmdb-cfg-01]
changed: [d-mipmdb-cfg-03]
changed: [d-mipmdb-cfg-02]
TASK [Replace MongoDB binaries] ******************************************************************************
changed: [d-mipmdb-cfg-01]
changed: [d-mipmdb-cfg-03]
changed: [d-mipmdb-cfg-02]
TASK [Start mongod service] ******************************************************************************
changed: [d-mipmdb-cfg-03]
changed: [d-mipmdb-cfg-01]
changed: [d-mipmdb-cfg-02]
答案 0 :(得分:2)
https://docs.ansible.com/ansible/latest/user_guide/playbooks_strategies.html
默认情况下,Ansible与您在每个播放的hosts:
字段中设置的模式中的所有主机并行运行。如果您一次只想管理几台机器,例如在滚动更新期间,则可以使用serial
关键字来定义Ansible一次可以管理多少台主机:
- name: test play
hosts: webservers
serial: 2
gather_facts: False
tasks:
- name: first task
command: hostname
- name: second task
command: hostname