Ansible:如果服务器属于一个组,则 运行 任务

Ansible: Run task if server belongs to a group

仅当主机属于一个或多个组时才执行任务的明智方法是什么?

目前,我在相关组中使用布尔值,例如:

库存文件

[db_servers:vars]
copy_connection_string=true

任务

-
  name: Copy db connection string file
  synchronize: src= ... (truncated for clarity)
  when: copy_connection_string is defined

when 子句中检查当前主机是否属于 db_servers 组的正确条件是什么?

运行 主机服务器是特定组成员时的任务

A​​nsible 包含 special or magic variables - one of the most common is group_names which is a list (array) of all the groups the current host is in.

- name: Copy db connection string file
  synchronize:.........
  when: "'db_servers' in group_names"

如果主机是 db_servers 组的成员,上述 Ansible 任务只会 运行。