繁体   English   中英

ansible、jinja2、json - 在 Jinja 中使用 json_query?

[英]ansible, jinja2, json - Using json_query in Jinja?

你好开发者社区!

我目前正在开发一些 Ansible 剧本来管理 Citrix NetScaler 配置,并希望获得有关以下方面的一些帮助。 我在变量文件中定义了以下数据结构:

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": ""
},

我想在 Jinja 文件中查询这个 JSON 结构,尝试了以下方法但不幸的是它不起作用。 我不是 100% 确定是否可以在 Jinja 文件中使用“json_query”。

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

上面没有使用(item_1.bind_lbvserver_service_group_name in nsadc_config_file_textfsm_nsapp_lb_service_parsed) ,我还尝试在 Jinja 中直接包含一个 json_query 语句,但没有运气。 恐怕,使用引号和转义字符可能会出现问题。

{% 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 + \"']\")) %}

有人可以建议是否可以直接在 Jinja 中查询 JSON 结构?

提前谢谢了!

json_query是一个 Jinja 过滤器,所以当然你可以在模板中使用它......或其他任何 Jinja 表达式有效的地方。

例如,如果我在这样的剧本(和模板任务)中有数据:

- 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

我可以编写一个如下所示的模板:

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

这将在output.txt产生以下输出:

- bob likes thing2

请注意模板中有关json_query表达式的两件事:

  • 因为我们正在寻找一个文字字符串,所以我们需要使用反引号来引用该值。
  • 我们使用 Python 字符串格式将我们的目标值包含在查询字符串中。

代替字符串格式化语法,我们可以写成:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM