如何从带有空格的数组中设置ansible事实 [英] how to set ansible fact from an array with whitespace

查看:121
本文介绍了如何从带有空格的数组中设置ansible事实的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从json数组中设置事实,因为密钥包含我无法解析的空间,有人可以在这里帮我吗, 我想将事实设置为名称":"IN-FG-04",而"vdom":"vdom-shop"

i am trying to set facts from a json array, since the key contains space i am unable to parse, can someone help me here, i want to set fact as "name": "IN-FG-04" when "vdom": "vdom-shop"

请参阅我的示例剧本条目

Please see my sample playbook entry

  - name: Iterate JSON
    set_fact:
      app_item: "{{ item['scope member'] }}"
    with_items: "{{ result.results }}"
    register: app_result

请参阅json输入,这是我上一个任务的输出

please see the json input and this is an output of my previous task

{
    "msg": {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        },
        "changed": false,
        "failed": false,
        "msg": "Custom Query Success",
        "results": [
            {
                "name": "FG-04-Policy",
                "obj ver": 3,
                "oid": 1196,
                "package settings": {
                    "central-nat": "disable",
                    "fwpolicy-implicit-log": "disable",
                    "fwpolicy6-implicit-log": "disable",
                    "inspection-mode": "proxy"
                },
                "scope member": [
                    {
                        "name": "IN-FG-04",
                        "vdom": "vdom-shop"
                    }
                ],
                "type": "pkg"
            },
            {
                "name": "FG-04-DC",
                "obj ver": 23,
                "oid": 1216,
                "package settings": {
                    "central-nat": "disable",
                    "fwpolicy-implicit-log": "disable",
                    "fwpolicy6-implicit-log": "disable",
                    "inspection-mode": "proxy"
                },
                "scope member": [
                    {
                        "name": "IN-FG-04",
                        "vdom": "vdom1-dc"
                    }
                ],
                "type": "pkg"
            }
        ]
    }
}

推荐答案

注意:尽管这对于练习循环来说是一个很好的练习,但您绝对应该考虑修正给出结果的前一个任务,或者

Note: although this is quite a good exercice for practicing loops, you should definitely consider fixing the previous task giving the result or write your own filter.

首先,您正在set_fact任务上使用register.它本身不是错误,但是兴趣非常有限.如果您为变量正确分配了一个值,而不是值本身,这将注册.因此,您将永远无法获得预期的结果.

First of all, you are using register on a set_fact task. It is not an error in itself but is of very limited interest. This will register if you correctly assigned a value to a variable, not the values themselves. So you will never get the expected result with this.

您要做的是在set_fact任务中遍历json时将项目追加到列表中.通过使用

What you want to do is to append items to a list while you are looping over your json in the set_fact task. This is done by initializing your set_fact variable to an empty list with the default filter on first loop and appending a new list with your items.

此外,由于您需要同时查看对象的多个级别以做出决定,因此需要使用 ternary 过滤器决定要包括哪个名称.

Moreover, since you need to look at several level of your object at the same time to take decisions, you need to use a subelements loop: loop level 0 on each result and level 1 on each element of the scope member list. On each iteration, you will append an object to the list. In the example below, I used the ternary filter to decide which name to include.

---
- name: SO Test
  hosts: localhost

  vars:
    result: {
      "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
      },
      "changed": false,
      "failed": false,
      "msg": "Custom Query Success",
      "results": [
        {
          "name": "FG-04-Policy",
          "obj ver": 3,
          "oid": 1196,
          "package settings": {
            "central-nat": "disable",
            "fwpolicy-implicit-log": "disable",
            "fwpolicy6-implicit-log": "disable",
            "inspection-mode": "proxy"
          },
          "scope member": [
            {
              "name": "IN-FG-04",
              "vdom": "vdom-shop"
            }
          ],
          "type": "pkg"
        },
        {
          "name": "FG-04-DC",
          "obj ver": 23,
          "oid": 1216,
          "package settings": {
            "central-nat": "disable",
            "fwpolicy-implicit-log": "disable",
            "fwpolicy6-implicit-log": "disable",
            "inspection-mode": "proxy"
          },
          "scope member": [
            {
              "name": "IN-FG-04",
              "vdom": "vdom1-dc"
            }
          ],
          "type": "pkg"
        }
      ]
    }

  tasks:
    - name: Get all names + vdom as requested in a list
      set_fact:
        app_result: >-
          {{
            app_result
            | default([])
            +
            [{
              'name': (item.1.vdom == 'vdom-shop') | ternary(item.0.name, item.1.name),
              'vdom': item.1.vdom
            }]
          }}
      loop: "{{ lookup('subelements', result.results, 'scope member') }}"

    - name: show result
      debug:
        var: app_result

会导致

PLAY [SO Test] *****************************************************************

TASK [Gathering Facts] *********************************************************
ok: [localhost]

TASK [Get all names + vdom as requested in a list] *****************************
ok: [localhost] => (item=[{'name': 'FG-04-Policy', 'obj ver': 3, 'oid': 1196, 'package settings': {'central-nat': 'disable', 'fwpolicy-implicit-log': 'disable', 'fwpolicy6-implicit-log': 'disable', 'inspection-mode': 'proxy'}, 'type': 'pkg'}, {'name': 'IN-FG-04', 'vdom': 'vdom-shop'}])
ok: [localhost] => (item=[{'name': 'FG-04-DC', 'obj ver': 23, 'oid': 1216, 'package settings': {'central-nat': 'disable', 'fwpolicy-implicit-log': 'disable', 'fwpolicy6-implicit-log': 'disable', 'inspection-mode': 'proxy'}, 'type': 'pkg'}, {'name': 'IN-FG-04', 'vdom': 'vdom1-dc'}])

TASK [show result] *************************************************************
ok: [localhost] => {
    "app_result": [
        {
            "name": "FG-04-Policy",
            "vdom": "vdom-shop"
        },
        {
            "name": "IN-FG-04",
            "vdom": "vdom1-dc"
        }
    ]
}

PLAY RECAP *********************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0

这篇关于如何从带有空格的数组中设置ansible事实的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆