Ansible - 按前缀过滤主机组

时间:2017-07-27 15:45:49

标签: ansible jmespath

我试图在Ansible中获取具有特定前缀的主机组名称。现在,我尝试将模板任务委托给主机组下的服务器,前缀为" config _"。

我使用json_query使用JMESPath表达式。但是查询不正确。谁能猜到我失踪的东西?

- name: Create configsvr config file   
  template: src=mongod.conf.j2 dest={{ mongod.conf.path }} owner=mongod group=mongod mode=0600
  delegate_to: "{{ groups|json_query([?starts_with(@, `config_`)]) }}" 

错误消息:

 FAILED! => {"failed": true, "msg": "template error while templating string: unexpected char u'?' at 22. String: {{ groups|json_query([?starts_with(@, `config_m`)]) }}"}

2 个答案:

答案 0 :(得分:0)

您应该只使用内置patterns来选择目标主机。

---
- hosts: conf_*
  tasks:
    - name: Create configsvr config file   
      template:
        src: mongod.conf.j2
        dest: "{{ mongod.conf.path }}"
        owner: mongod
        group: mongod
        mode: 0600

答案 1 :(得分:0)

您可以使用群组来改善广告资源,例如:

[conf:children]
conf_a
conf_b
conf_c

[conf_a]
srv1

[conf_b]
srv2

[conf_c]
srv3

然后在你的剧本中定位conf小组:

---
- hosts: conf
  tasks:
    - name: Create configsvr config file   
      template:
        src: mongod.conf.j2
        dest: "{{ mongod.conf.path }}"
        owner: mongod
        group: mongod
        mode: 0600