Home:ALL Converter>Ansible - Filter host groups by prefix

Ansible - Filter host groups by prefix

Ask Time:2017-07-27T23:45:48         Author:AmineGherib

Json Formatter

I'm trying to fetch the names of host groups in Ansible that have a specific prefix. Right now, I'm trying to delegate a template task to servers under host groups with the prefix "config_".

I'm using json_query which uses JMESPath expressions. The query however is incorrect. Can anyone guess what I'm missing?

- 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_`)]) }}" 

Error msg:

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

Author:AmineGherib,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/45355455/ansible-filter-host-groups-by-prefix
Eric Citaire :

You should simply use built-in patterns to select your target hosts.\n\n---\n- hosts: conf_*\n tasks:\n - name: Create configsvr config file \n template:\n src: mongod.conf.j2\n dest: \"{{ mongod.conf.path }}\"\n owner: mongod\n group: mongod\n mode: 0600\n",
2017-07-28T08:09:30
yy