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

问题描述 投票:5回答:1

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

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

库存文件

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

ansible
1个回答
14
投票

Run task when a host server is a member of a specific group

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任务。

© www.soinside.com 2019 - 2024. All rights reserved.