如何使用JMESPath过滤此JSON数据的子元素

时间:2018-01-12 02:09:49

标签: jmespath

我正在使用Ansible在多个环境中创建用户。我试图整合我的用户var,其中包含创建新的unix用户到全局共享var所需的所有数据。为此,我计划使用子元素来控制在哪些主机上创建哪些用户。

但是,我正在努力弄清楚如何过滤掉这些数据,以便为当前主机提供正确的凭据集。

我需要过滤"环境"只需使用" name"我正在寻找的值,但在结果中保留父对象的键。

我拥有什么/我想要什么:

JSON:

{
  "users": [
    {
      "name": "user1",
      "token": "token",
      "unix": "unixstring",
      "mysql": "mysqlstring",
      "environments": [
        {
          "name": "env_one",
          "key": "keystring",
          "user_groups": [
            "one",
            "two"
          ],
          "host_groups": "all"
        },
        {
          "name": "env_two",
          "key": "keystring",
          "user_groups": [
            "three",
            "four"
          ],
          "host_groups": "all"
        }
      ]
    },
    {
      "name": "user2",
      "token": "token",
      "unix": "unixstring",
      "mysql": "mysqlstring",
      "environments": [
        {
          "name": "env_three",
          "key": "keystring",
          "user_groups": [
            "one",
            "two"
          ],
          "host_groups": "all"
        }
      ]
    }
  ]
}

我想要的是什么:

{
  "users": [
    {
      "name": "user1",
      "token": "token",
      "unix": "unixstring",
      "mysql": "mysqlstring",
      "environments": [
        {
          "name": "env_one",
          "key": "keystring",
          "user_groups": [
            "one",
            "two"
          ],
          "host_groups": "all"
        }
      ]
    }
  ]
}

更多信息:

我能够接近使用此查询,但我不知道如何进一步缩小结果,以便每个"用户"中的所有键。对象保留但过滤掉子元素"环境"与我的查询匹配的那些。

JMESPath查询:users[?environments[?name=='env_one']]

输出:

{
  "users": [
    {
      "name": "user1",
      "token": "token",
      "unix": "unixstring",
      "mysql": "mysqlstring",
      "environments": [
        {
          "name": "env_one",
          "key": "keystring",
          "user_groups": [
            "one",
            "two"
          ],
          "host_groups": "all"
        },
        {
          "name": "env_two",
          "key": "keystring",
          "user_groups": [
            "three",
            "four"
          ],
          "host_groups": "all"
        }
      ]
    }
  ]
}

如果有用,这是YAML中的原始数据集:

YAML:

---
users:
  - name: user1
    token: token
    unix: unixstring
    mysql: mysqlstring
    environments:

      - name: env_one
        key: keystring
        user_groups:
            - one
            - two
        host_groups: all

      - name: env_two
        key: keystring
        user_groups: 
            - three
            - four
        host_groups: all

  - name: user2
    toke n: token
    unix: unixstring
    mysql: mysqlstring
    environments:

      - name: env_three
        key: keystring
        user_groups:
            - one
            - two
        host_groups: all

1 个答案:

答案 0 :(得分:0)

好吧,我找到了一个可行的解决方案:

- set_fact:
    users: >
      {{ users
      | json_query("[?environments[?name=='" + ansible_environment + "']].{
        name: name,
        yubikey_public_id: yubikey_public_id,
        unix_initial_password: unix_initial_password,
        mysql_initial_password: mysql_initial_password,
        environments: environments[?name=='" + ansible_environment + "']}") }}

如果我能以某种方式包含所有“users”元素的键而不显式调用它们(name,token,unix ......)会更好,这样如果添加更多字段,查询就不必更改那个元素。但我不知道这是否可能。

查询结果:

[
  {
    "name": "user1",
    "token": "token",
    "unix": "unixstring",
    "mysql": "mysqlstring",
    "environments": [
      {
        "name": "env_one",
        "key": "keystring",
        "user_groups": [
          "one",
          "two"
        ],
        "host_groups": "all"
      }
    ]
  }
]