can't read custom facts with list (array) of items

折月煮酒 提交于 2021-02-08 01:21:21

问题


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"
                }
            }
        ]
    }
}

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 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" : [ ]
    }
  } ]
}

thanks


回答1:


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

Edit:
As you mentioned that "{{ansible_local.hdfs.items}}" is printing built-in method items of dict object at 0x7f81f42b2c58.

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.


A little on parsing:

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.



回答2:


The reason debug: var=ansible_local.hdfs.items.config.displayName fails is that items is a reserved word.

Try

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

I found the solution in the post from lazartravica here https://github.com/ansible/ansible/issues/10581




回答3:


In your example displayName is a property of item and not of item.config. So ansible_local.hdfs.items[0].displayName will work in this case.



来源:https://stackoverflow.com/questions/40281706/cant-read-custom-facts-with-list-array-of-items

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!