2016-11-11 88 views
0

我是相当新的ansible,并试图探索词典插件选项。 下面是我的剧本,我想使用from_json模块将json文件转换为字典,然后我只想打印特定的键。 无法实现第二部分。Ansible:with_dict访问从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文件:

[email protected]:/etc/ansible# less ConfigData.json 
    { 
     "appSupCommonPullSendProxyAuthInfo":0, 
     "appSupCommonPullSendWebAuthInfo":0, 
     "appSupDdcSupervisionEnabled":1, 
     "appSupDdcSupervisionInterval":15, 
     "appSupDdcSupervisionMaxNoOfFailures":4, 
     "appSupDNS_number_consequtive_failures":5, 
     "appSupDNS_supervision_interval":60, 
    } 

回答

0

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

不知道你的问题是什么,但打印不同任务的所有增值经销商可以使用:

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