【问题标题】:Ansible: Include task only when server is not in groupAnsible:仅当服务器不在组中时才包含任务
【发布时间】:2018-04-26 07:20:07
【问题描述】:

我有一些名称不同的组,例如“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-')"' 但它不起作用。 有什么想法吗?

【问题讨论】:

  • 请不要破坏您的帖子。通过在 Stack Exchange 网络上发布,您已授予 SE 分发该内容的不可撤销的权利(在 CC BY-SA 3.0 license 下)。根据 SE 政策,任何破坏行为都将被撤销。

标签: include ansible groupname


【解决方案1】:

您可以使用几种方法来做到这一点。这是一个示例剧本,展示了两种这样的方法。一个使用选择,另一个使用搜索。

---
- 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

【讨论】:

  • 谢谢! "group_names | join(" ") is search('raw-')" 和 "group_names | join(" ") is not search('raw-')" 效果很好!
猜你喜欢
  • 1970-01-01
  • 2016-07-29
  • 2017-04-22
  • 1970-01-01
  • 2017-03-03
  • 2019-12-23
  • 1970-01-01
  • 1970-01-01
  • 2015-11-18
相关资源
最近更新 更多