Ansible:仅当服务器不在组中时才包含任务
Ansible: Include task only when server is not in group
我有一些名称不同的组,例如 "raw-webservers"、"raw-db"、...
现在,如果服务器在以 'raw-*' 开头的组中(有效),我想包含一个剧本;如果服务器不在以 'raw-' 开头的组中,我想包含另一个剧本。我无法通过仅指定组的一个子集来弄清楚如何做最后一件事。
- include_tasks: change_password.yml
when: "'raw-' not in group_names" # works only with complete group names
- include_tasks: change_password_raw.yml
when: "group_names | search('raw-')" # works
我已经尝试 'when: "group_names | not search('raw-')"' 但它不起作用。
有什么想法吗?
您可以使用几种方法来完成此操作。这是一个示例剧本,显示了两种这样的方法。一个使用 select,另一个使用搜索。
---
- hosts: all
gather_facts: no
tasks:
- debug:
msg: "hello world using select"
delegate_to: 127.0.0.1
when: group_names | select('match','raw-*') | list | length > 0
- debug:
msg: "hello world using search"
delegate_to: 127.0.0.1
when: group_names | join(" ") is search('raw-')
您会在此处看到搜索 works on strings 而不是列表,因此加入的原因。
除此之外,您还可以使用另一个群组。例如,您的库存可以添加以下内容。
[raw:children]
db
这可以被测试为 'raw' in group_names
。
我有一些名称不同的组,例如 "raw-webservers"、"raw-db"、... 现在,如果服务器在以 'raw-*' 开头的组中(有效),我想包含一个剧本;如果服务器不在以 'raw-' 开头的组中,我想包含另一个剧本。我无法通过仅指定组的一个子集来弄清楚如何做最后一件事。
- include_tasks: change_password.yml
when: "'raw-' not in group_names" # works only with complete group names
- include_tasks: change_password_raw.yml
when: "group_names | search('raw-')" # works
我已经尝试 'when: "group_names | not search('raw-')"' 但它不起作用。 有什么想法吗?
您可以使用几种方法来完成此操作。这是一个示例剧本,显示了两种这样的方法。一个使用 select,另一个使用搜索。
---
- hosts: all
gather_facts: no
tasks:
- debug:
msg: "hello world using select"
delegate_to: 127.0.0.1
when: group_names | select('match','raw-*') | list | length > 0
- debug:
msg: "hello world using search"
delegate_to: 127.0.0.1
when: group_names | join(" ") is search('raw-')
您会在此处看到搜索 works on strings 而不是列表,因此加入的原因。
除此之外,您还可以使用另一个群组。例如,您的库存可以添加以下内容。
[raw:children]
db
这可以被测试为 'raw' in group_names
。