Ansible:如何区分两个主机组之间的主机

Ansible: How to differentiate the hosts between two host-groups

我的主机文件就像

[app]
192.168.6.100
192.168.6.101

[DB]
192.168.7.200
192.168.7.201

我想 运行 两个不同的 .yml 文件,分别使用 - include app.yml- include db.yml 来自 main.yml 文件

为了区分我使用的主机 when: "{{ groups['app'] }}"when: "{{ groups['db'] }}",但它无法正常工作。我也不确定它是否是正确的方法

# cat main.yml 
---
 - include: app.yml
   when: "{{ groups['app'] }}"
 - include: db.yml 
   when: "{{ groups['db'] }}"

你很接近。正确的语法是:

---
 - include: app.yml
   when: "'app' in group_names"
 - include: db.yml 
   when: "'db' in group_names"