无法使用项目列表(数组)读取自定义事实

yor*_*hai 3 ansible ansible-playbook ansible-2.x

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

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

 - debug: var=ansible_local.hdfs
   run_once: true
Run Code Online (Sandbox Code Playgroud)

我得到了以下答案:

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"
                }
            }
        ]
    }
}
Run Code Online (Sandbox Code Playgroud)

我的问题是如何从该 json 解析特定值。我已经尝试了很多语法都没有成功

debug: var=ansible_local.hdfs.items[0].config.displayName
debug: var=ansible_local.hdfs.items.config.displayName
Run Code Online (Sandbox Code Playgroud)

hdfs.fact 内容:

{
  "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" : [ ]
    }
  } ]
}
Run Code Online (Sandbox Code Playgroud)

谢谢

Tva*_*tom 5

debug: var=ansible_local.hdfs.items.config.displayName 失败的原因 是这items是一个保留字。

尝试

debug: var=ansible_local.hdfs['items'].config.displayName 反而。

我发现从帖子的解决方案lazartravica这里 https://github.com/ansible/ansible/issues/10581


Sha*_*a99 3

items是一个列表,它的每个元素都是一个字典。项目的每个字典元素都有displayName属性。如果您想打印项目列表中存在的每个字典元素的显示名称,您可以使用以下代码:

- debug: msg="{{item.displayName}}"
  with_items:
    - "{{ansible_local.hdfs.items}}" 
Run Code Online (Sandbox Code Playgroud)

编辑:
正如您所提到的,“{{ansible_local.hdfs.items}}”正在打印0x7f81f42b2c58 处 dict 对象的内置方法项

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


关于解析的一点:

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

L=[1,2,3,4]


L[0] will give you 1.

L[1] will give you 2.
Run Code Online (Sandbox Code Playgroud)

字典中的元素可以通过使用它们的来引用,您可以使用两种约定:

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.
Run Code Online (Sandbox Code Playgroud)