can't read custom facts with list (array) of items
ansible, ansible-2.x
Solution
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.
Problem
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