selectattr中的Ansible Jinja2转义虚线键

时间:2019-07-11 13:08:31

标签: ansible jinja2

我目前正在尝试遍历k8s_facts的子集。我的事实看起来像:

{
  "resources": [
  { 
    "metadata": {
      "annotations": {
         "com.foo.bar/name": "foo",
         "com.foo.bar/foo-name": "baz"
       },
       "creationTimestamp": "2018-12-20T02:29:50Z",
       "name": "foo-bar"
    }
  },
  ...

我要过滤com.foo.bar/foo-name键的特定值。由于键具有.-/,因此与Jinja2 selectattr函数配合使用效果不佳。我试图做这样的事情,但徒劳无功:

- debug:
    msg: "{{ item }}"
    loop: "{{ my_fact.resources | selectattr('metadata.annotations[\\'com.foo.bar/foo-name\\']', 'defined') | selectattr('metadata.annotations[\\'com.foo.bar/foo-name\\']', 'match', 'baz') | list }}"
  loop_control: 
    label: "{{ item.metadata.name }}"

执行上一个时,出现此错误:

fatal: [<redacted>]: FAILED! => {"msg": "template error while templating string: expected token ',', got 'com'. String: {{ my_fact.resources | selectattr('metadata.annotations[\\\\'com.foo.bar/foo-name\\\\']', 'defined') | selectattr('metadata.annotations[\\\\'com.foo.bar/foo-name\\\\']', 'match', 'baz') | list }}"}

我的问题是,如何在Jinja2中转义包含点的复杂字符串?

2 个答案:

答案 0 :(得分:3)

当我进入ansible逃避地狱的时候,我倾向于利用yaml folded and literal block syntax的优势。优点是它使您无需在引号周围就可以编写jinja块,从而消除了一个引号级别,从而也消除了一个转义级别。

就您而言,我认为您可以直接使用json_query filter而不是使用一长串过滤器。

这是一个演示剧本:

---
- name: Test var names with dots
  hosts: localhost
  gather_facts: false

  vars:
    my_fact: {
      "resources": [
        {
          "metadata": {
            "annotations": {
              "com.foo.bar/name": "foo",
              "com.foo.bar/foo-name": "baz"
            },
            "creationTimestamp": "2018-12-20T02:29:50Z",
            "name": "foo-bar"
          }
        },
        {
          "metadata": {
            "annotations": {
              "com.foo.bar/name": "toto",
              "com.foo.bar/foo-name": "titi"
            },
            "creationTimestamp": "2018-12-21T02:30:50Z",
            "name": "foo-bla"
          }
        },
        {
          "metadata": {
            "annotations": {
              "com.foo.bar/name": "johnsmith",
              "com.foo.bar/foo-name": "baz"
            },
            "creationTimestamp": "2018-12-22T02:31:50Z",
            "name": "foo-john"
          }
        }
      ]
    }

  tasks:
    - name: Show results where metadata.annotations."com.foo.bar/foo-name"=='baz'
      vars:
        query: >-
          [?(metadata.annotations."com.foo.bar/foo-name")=='baz']
      debug:
        msg: "{{ item }}"
      loop: "{{ my_fact.resources | json_query(query) }}"
      loop_control:
        label: "{{ item.metadata.name }}"

结果

PLAY [Test var names with dots] *******************************************************************************************************************************************************************************************

TASK [Show results where metadata.annotations."com.foo.bar/foo-name"=='baz'] **********************************************************************************************************************************************
ok: [localhost] => (item=foo-bar) => {
    "msg": {
        "metadata": {
            "annotations": {
                "com.foo.bar/foo-name": "baz",
                "com.foo.bar/name": "foo"
            },
            "creationTimestamp": "2018-12-20T02:29:50Z",
            "name": "foo-bar"
        }
    }
}
ok: [localhost] => (item=foo-john) => {
    "msg": {
        "metadata": {
            "annotations": {
                "com.foo.bar/foo-name": "baz",
                "com.foo.bar/name": "johnsmith"
            },
            "creationTimestamp": "2018-12-22T02:31:50Z",
            "name": "foo-john"
        }
    }
}

PLAY RECAP ****************************************************************************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

答案 1 :(得分:0)

Q:“ I want to filter on a specific value of the com.foo.bar/foo-name key

A:下面的任务可以完成

- debug:
    msg: "{{ resources|json_query(my_query) }}"
  vars:
    my_query: '[?metadata.annotations."com.foo.bar/foo-name"==`baz`]'