0

我需要根据组名运行几个任务,其余任务应该以正常方式执行,例如:一个带有变量的任务 bootstrap 现在我只需要在主机组包含名为 bootstrap 的组时运行此任务

ex: task1: echo "bootstrap node" >> bootstrap

inventory file
[bootstrap]
ip1
ip2
ip3
4

1 回答 1

1

您可以直接为该组运行播放

---
- name: Play to group bootstrap
  hosts: bootstrap

  tasks
  - name: Example of task
    debug:
      msg: Example of a task

但是如果您需要为特定组使用一些任务,when: "'bootstrap' in group_name"如下所示

---
- name: Play to group bootstrap
  hosts: bootstrap

  tasks
  - name: Example of task for all
    debug:
      msg: Example of running a task for all

  - name: Example of task for bootstrap
    debug:
      msg: Example of running a task for bootstrap
    when: "'bootstrap' in group_name"

如果有很多任务,您可以使用如下块

---
- name: Play to group bootstrap
  hosts: bootstrap

  tasks
  - name: Example of task for all
    debug:
      msg: Example of running a task for all

  - name: Block for bootstrap
    block:
      - name: Example of task 1 for bootstrap
        debug:
          msg: Example of running a task 1 for bootstrap

      - name: Example of task 2 for bootstrap
        debug:
          msg: Example of running a task 2 for bootstrap

      - name: Example of task 3 for bootstrap
        debug:
          msg: Example of running a task 3 for bootstrap

    when: "'bootstrap' in group_name"
于 2020-11-19T21:08:39.500 回答