在多个清单主机组上运行 ansible 任务

Jea*_*ean 3 ansible ansible-inventory

我希望在多个库存组上运行相同的任务,例如:

[big_group]
greenhat
localhost
redhat
linux

[small_group]
localhost

[redhat_group]
redhat
Run Code Online (Sandbox Code Playgroud)

在我的剧本中,我需要在[small_group][redhat_group]上运行一个任务

我的任务如下

[big_group]
greenhat
localhost
redhat
linux

[small_group]
localhost

[redhat_group]
redhat
Run Code Online (Sandbox Code Playgroud)

收到警告作为

 [WARNING]: While constructing a mapping from firewall.yml, line 6, column 5, found a duplicate dict key (when). Using last defined value only.
Run Code Online (Sandbox Code Playgroud)

剧本结果:

TASK [Disable HTTPS service from firewalld] **************************************************************************************************************************************************
skipping: [localhost]
skipping: [redhat]
Run Code Online (Sandbox Code Playgroud)

如何指定多个组 when: inventory_hostname in groups[]

谢谢

Zei*_*tor 5

when一个任务中不能有多个子句。在您的情况下,您只需加入两个组 ( +) 并删除多个条目(如果有)(使用unique过滤器,以便将来更改库存)。

when: inventory_hostname in ((groups['small_group'] + groups['redhat_group']) | unique )
Run Code Online (Sandbox Code Playgroud)

同时,有一种更方便的方式 IMO 使用inventory_hostnames查找和利用模式,就像您在正常游戏中所做的那样

when: inventory_hostname in lookup('inventory_hostnames', 'small_group:redhat_group')
Run Code Online (Sandbox Code Playgroud)