我对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,
}
答案 0 :(得分:0)
您的playbook语法不正确 - 只定义了一个任务(每个任务应作为新的列表项开始(短划线符号))。
不确定您的问题是什么,但要打印具有不同任务的所有变量,您可以使用:
- hosts: localhost
vars:
jsonVar: "{{ lookup('file', 'ConfigData.json') | from_json }}"
tasks:
- debug: msg=" {{ jsonVar.cpsSiteName }}"
- debug: msg=" {{ jsonVar.dmsDataCacheAddress }}"
- debug: msg=" {{ jsonVar.htsHttpClientIdleConnectionTimeout }}"
# etc...