【问题标题】:Ansible: with_dict to access particular key for the dictionary created from json fileAnsible:with_dict 访问从 json 文件创建的字典的特定键
【发布时间】:2016-11-11 21:08:03
【问题描述】:

我对 ansible 相当陌生,并试图探索字典插件选项。 下面是我的剧本,我正在尝试使用 from_json 模块将 json 文件转换为字典,然后我只想打印特定的键。 无法完成第二部分。

代码如下:

hosts: localhost
vars:
  jsonVar: "{{ lookup('file', 'ConfigData.json') | from_json }}"

tasks:
  - name: print numbers
    debug: msg=" {{ jsonVar.cpsSiteName }}"
    debug: msg=" {{ jsonVar.dmsDataCacheAddress }}"
    debug: msg=" {{ jsonVar.htsHttpClientIdleConnectionTimeout }}"
    debug: msg=" {{ jsonVar.palConsulServerList }}"
    debug: msg=" {{ jsonVar.prsMaxIncoReqProcTime }}"
    debug: msg=" {{ jsonVar.prsMaxNumberOfExecutedScriptInstructions }}"
    debug: msg=" {{ jsonVar.prsmschapv2Password }}"

输出仅显示最后一个调试语句。 渴望知道是否有更聪明的方法可以从转换后的字典中访问特定变量,然后在输出中打印出来。

我尝试使用 with_items,但不起作用。 提前感谢您的帮助。

下面是一个示例 json 文件:

    root@BVSETL04:/etc/ansible# less ConfigData.json 
    {
      "appSupCommonPullSendProxyAuthInfo":0,
      "appSupCommonPullSendWebAuthInfo":0,
      "appSupDdcSupervisionEnabled":1,
      "appSupDdcSupervisionInterval":15,
      "appSupDdcSupervisionMaxNoOfFailures":4,
      "appSupDNS_number_consequtive_failures":5,
      "appSupDNS_supervision_interval":60,
    }

【问题讨论】:

    标签: dictionary ansible


    【解决方案1】:

    您的剧本语法不正确 - 只定义了一个任务(每个任务都应该作为一个新的列表项(破折号)开始)。

    不确定您的问题是什么,但要打印您可以使用的具有不同任务的所有变量:

    - hosts: localhost
      vars:
        jsonVar: "{{ lookup('file', 'ConfigData.json') | from_json }}"
      tasks:
        - debug: msg=" {{ jsonVar.cpsSiteName }}"
        - debug: msg=" {{ jsonVar.dmsDataCacheAddress }}"
        - debug: msg=" {{ jsonVar.htsHttpClientIdleConnectionTimeout }}"
        # etc...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-17
      • 2017-12-03
      • 1970-01-01
      • 1970-01-01
      • 2021-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多