首页 文章

Ansible - 按前缀过滤主机组

提问于
浏览
0

我正在尝试获取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

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

    ---
    - hosts: conf_*
      tasks:
        - name: Create configsvr config file   
          template:
            src: mongod.conf.j2
            dest: "{{ mongod.conf.path }}"
            owner: mongod
            group: mongod
            mode: 0600
    
  • 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
    

相关问题