在主机上一对一运行ansible任务

时间:2020-09-24 14:05:12

标签: ansible

我想升级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首先在所有主机上停止服务这使我的应用程序不可用,然后在所有主机上进行升级,最后在所有主机上再次启动服务。

如何为每个主机依次运行这些任务,即

  1. 转到第一个主机
  2. 停止服务
  3. 替换二进制文件
  4. 启动服务
  5. 在下一个主机上重复,直到完成所有主机

我尝试使用blockinclude_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]

1 个答案:

答案 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