无法读取具有列表(数组)的自定义事实 [英] can't read custom facts with list (array) of items

查看:77
本文介绍了无法读取具有列表(数组)的自定义事实的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了自定义事实--->/etc/ansible/facts.d/hdfs.fact

i have created custom fact ---> /etc/ansible/facts.d/hdfs.fact

当我使用以下命令运行剧本时

when i'm running the playbook with the following command

 - debug: var=ansible_local.hdfs
   run_once: true

我正按预期得到以下答案:

i'm getting as expected the following answer:

PLAY [all] *********************************************************************

TASK [setup] *******************************************************************
ok: [cdh-2]
ok: [cdh-3]
ok: [cdh-1]

TASK [preparation : debug] *****************************************************
ok: [cdh-1] => {
    "ansible_local.hdfs": {
        "items": [
            {
                "base": true,
                "config": {
                    "items": []
                },
                "displayName": "Failover Controller Default Group",
                "name": "hdfs-FAILOVERCONTROLLER-BASE",
                "roleType": "FAILOVERCONTROLLER",
                "serviceRef": {
                    "clusterName": "cluster",
                    "serviceName": "hdfs"
                }
            },
            {
                "base": true,
                "config": {
                    "items": [
                        {
                            "name": "balancer_java_heapsize",
                            "value": "491782144"
                        }
                    ]
                },
                "displayName": "Balancer Default Group",
                "name": "hdfs-BALANCER-BASE",
                "roleType": "BALANCER",
                "serviceRef": {
                    "clusterName": "cluster",
                    "serviceName": "hdfs"
                }
            },
            {
                "base": true,
                "config": {
                    "items": []
                },
                "displayName": "HttpFS Default Group",
                "name": "hdfs-HTTPFS-BASE",
                "roleType": "HTTPFS",
                "serviceRef": {
                    "clusterName": "cluster",
                    "serviceName": "hdfs"
                }
            }
        ]
    }
}

我的问题是如何解析该json中的特定值.我已经尝试了很多语法,但没有成功

my question in how can i parse specific value from that json. I already tried many syntax without any success

debug: var=ansible_local.hdfs.items[0].config.displayName
debug: var=ansible_local.hdfs.items.config.displayName

hdfs.fact内容:

hdfs.fact content:

{
  "items" : [ {
    "name" : "hdfs-FAILOVERCONTROLLER-BASE",
    "displayName" : "Failover Controller Default Group",
    "roleType" : "FAILOVERCONTROLLER",
    "base" : true,
    "serviceRef" : {
      "clusterName" : "cluster",
      "serviceName" : "hdfs"
    },
    "config" : {
      "items" : [ ]
    }
  }, {
    "name" : "hdfs-BALANCER-BASE",
    "displayName" : "Balancer Default Group",
    "roleType" : "BALANCER",
    "base" : true,
    "serviceRef" : {
      "clusterName" : "cluster",
      "serviceName" : "hdfs"
    },
    "config" : {
      "items" : [ {
        "name" : "balancer_java_heapsize",
        "value" : "491782144"
      } ]
    }
  }, {
    "name" : "hdfs-HTTPFS-BASE",
    "displayName" : "HttpFS Default Group",
    "roleType" : "HTTPFS",
    "base" : true,
    "serviceRef" : {
      "clusterName" : "cluster",
      "serviceName" : "hdfs"
    },
    "config" : {
      "items" : [ ]
    }
  } ]
}

谢谢

推荐答案

项目是一个列表,每个元素都是一个字典.的每个字典元素都具有 displayName 属性.如果要打印项目列表中存在的每个词典元素的 displayName ,则可以使用以下代码:

items is a list and each of its element is a dictionary. Each dictionary element of items has displayName property. In-case you want to print displayName of each dictionary element present in items list, you can use the following piece of code:

- debug: msg="{{item.displayName}}"
  with_items:
    - "{{ansible_local.hdfs.items}}" 


修改:
正如您提到的那样,"{{ansible_local.hdfs.items}}" 正在将 dict对象的内置方法项打印在0x7f81f42b2c58 .

之所以发生这种情况,是因为名称 items 与某些内置方法的名称冲突.因此,您只需要将名称更改为其他名称,就不能在hdfs.fact文件中使用 items 名称.

This is happening because the name items is clashing with the name of some built-in method. So you just need to change the name to something else, you can not use items name in your hdfs.fact file.

一些解析:

列表中的元素可以通过使用它们的位置作为索引来引用.

Elements in a List can be referred by using their position as the index.

L=[1,2,3,4]


L[0] will give you 1.

L[1] will give you 2.

字典中的元素可以通过使用它们的进行引用,并且可以使用两种约定:

Elements in a dictionary can be referred by using their key and there are 2 conventions that you can use:

D ={"one" : 1, "two" : 2, "three" : 3}


D["1"] will give you 1. 

D.two will give you 2.

D.one will give you 1.

D["two"] will give you 2.

这篇关于无法读取具有列表(数组)的自定义事实的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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