Search code examples
jsonansiblejinja2json-query

ansible, jinja2, json - Using json_query in Jinja?


Hello Developer Community!

I'm currently working on developing some Ansible playbooks to manage Citrix NetScaler configuration and would like to get some help about the following. I have the following data structure defined in a variable file:

nsadc_config_file_textfsm_nsapp_lb_service_parsed

    {
    "bind_lbvserver_NotParsed": "",
    "bind_lbvserver_analyticsprofile": "",
    "bind_lbvserver_gotopriorityexpression": "",
    "bind_lbvserver_name": "VSRV-CS-Client1",
    "bind_lbvserver_policyname": "",
    "bind_lbvserver_priority": "",
    "bind_lbvserver_service_group_name": "SVC_Client1_App1_Server1_Port1",
    "bind_lbvserver_type": "",
    "bind_lbvserver_weight": ""
},
{
    "bind_lbvserver_NotParsed": "",
    "bind_lbvserver_analyticsprofile": "",
    "bind_lbvserver_gotopriorityexpression": "NEXT",
    "bind_lbvserver_name": "VSRV_Client2_App2",
    "bind_lbvserver_policyname": "policy_Client2_Rewrite",
    "bind_lbvserver_priority": "80",
    "bind_lbvserver_service_group_name": "",
    "bind_lbvserver_type": "REQUEST",
    "bind_lbvserver_weight": ""
},

I would like to query this JSON structure in a Jinja file, tried the followings but unfortunately it does not work. I'm not 100% sure if it is possible to use "json_query" in a Jinja file.

                                servicebindings:
{% for item_1 in nsadc_config_file_textfsm_nsapp_lb_vserver_binding_parsed %}
{% if (item_0.add_lbvserver_name == item_1.bind_lbvserver_name) and (item_1.bind_lbvserver_service_group_name in nsadc_config_file_textfsm_nsapp_lb_service_parsed) %}

                                    - servicename:         "{{ item_1.bind_lbvserver_service_group_name }}"
{% if item_1.bind_lbvserver_weight is defined and item_1.bind_lbvserver_weight %}
                                      weight:              "{{ item_1.bind_lbvserver_weight }}"
{% endif %}
{% endif %}
{% endfor %}

Instead of using (item_1.bind_lbvserver_service_group_name in nsadc_config_file_textfsm_nsapp_lb_service_parsed) above, I have also tried to include a json_query statement directly in Jinja, but with no luck. I'm afraid, there might be an issue with using quotes and escape chars.

{% if (item_0.add_lbvserver_name == item_1.bind_lbvserver_name) and (nsadc_config_file_textfsm_nsapp_lb_service_parsed | json_query(\"[?add_svc_name == '\" + item_1.bind_lbvserver_service_group_name + \"']\")) %}

Could anybody please advise if it is possible to query JSON structure directly in Jinja?

Many thanks in advance!


Solution

  • json_query is a Jinja filter, so of course you can use it in a template...or anywhere else that Jinja expressions are valid.

    For example, if I have data in a playbook like this (and a template task):

    - hosts: localhost
      gather_facts: false
      vars:
        var1:
          - name: thing1
            color: red
          - name: thing2
            color: blue
    
        var2:
          - name: bob
            likes: blue
          - name: alice
            likes: green
    
      tasks:
        - template:
            src: template.in
            dest: output.txt
    

    I can write a template that looks like this:

    {% for person in var2 %}
    {% for thing in var1|json_query('[?color == `{}`]'.format(person.likes)) %}
    - {{ person.name }} likes {{ thing.name }}
    {% endfor %}
    {% endfor %}
    

    Which will produce the following output in output.txt:

    - bob likes thing2
    

    Note two things about the json_query expression in the template:

    • Because we're looking for a literal string, we need to use backticks to quote the value.
    • We're using Python string formatting to include our target value in the query string.

    In lieu of the string formatting syntax we could instead have written:

    json_query('[?color == `' + person.likes + '`]')