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

时间:2015-10-07 09:39:38

标签: ansible

只有当主机属于一个或多个组时,运行任务的理智方式是什么?

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

库存文件

[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组的正确条件是什么?

1 个答案:

答案 0 :(得分:14)

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

Ansible包含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任务。