我正在爲需要創建的ansible
模塊而苦苦掙扎。一切都完成了,模塊從第三方獲得json
文件到fs
。這個json
文件預計是(只)輸出能夠訪問以註冊json
文件並訪問內容 - 或者至少使輸出正常訪問。使用存儲在fs/disk上的json文件作爲Ansible模塊的輸出
輸出文件包含一個合適的json
文件,我嘗試過不同的東西來達到我的目標。
包括:
只需打印出使用
print
或os.stdout.write
的json
文件,因爲根據文件,ansible
簡單地取stdout
。導入
json
和轉儲使用json.dumps(data)
或像這樣:with open('path-to-file', 'r') as tmpfile: data = json.load(tmpfile) module.exit_json(changed=True, message="API call to %s successfull" % endpoint, meta=data)
- 這最後不得不在輸出的JSON,但在逃脫變種和
ansible
拒絕訪問逃脫的一部分。
- 這最後不得不在輸出的JSON,但在逃脫變種和
什麼會提出關於進一步使用的json
數據訪問的正確方法?
編輯:
的json
看起來是這樣的(當然,這是一個巨大json
,這只是其中的一部分):register
{
"total_results": 51,
"total_pages": 2,
"prev_url": null,
"next_url": "/v2/apps?order-direction=asc&page=2&results-per-page=50",
後,debug
輸出看起來像這樣和我例如,不能訪問output.meta.total_results
。
ok: [localhost] => {
"output": {
"changed": true,
"message": "API call filtering /v2/apps with name and yes-no was successfull",
"meta": "{\"total_results\": 51, \"next_url\": \"/v2/apps?order-direction=asc&page=2&results-per-page=50\", \"total_pages\": 2, \"prev_url\": null, (...)
的ansible
輸出試圖訪問VAR時:
ok: [localhost] => {
"output.meta.total_results": "VARIABLE IS NOT DEFINED!"
}
請提供您的json文件示例以及ansible爲您提供的錯誤消息。一般情況下,你的代碼應該可以正常工作。 –
@KonstantinSuvorov:當然,我會將它附加到主帖 – Jan