从json输出中提取变量,然后调试并使用ansible注册outout [英] extracting a variable from json output then debug and register the outout with ansible
问题描述
我有一个问题,是在进行curl解析并重新注册回ansible后,要从json输出中提取出其中一个变量
Hi I have a problem of getting one of the variables extracted from a json output after doing a curl to be parsed and registered back to ansible
剧本:
- name: debug stdout
debug:
msg: "{{ result.stdout | from_json }}"
register: dataresult
- name: debug fact
debug:
msg: "{{ dataresult.data.start_time_string }}"
输出:
TASK [backup_api : debug stdout]
***********************************************
task path: /home/ansible/cm-dha/roles/backup_api/tasks/main.yml:36
ok: [127.0.0.1] => {
"msg": {
"data": [
{
"backup_id": 40362,
"certified": null,
"instance_id": 148,
"start_time": 1506985211,
"start_time_string": "10/03/2017 03:00:11 am"
}
],
"timestamp": 1507022232
}
}
错误:
致命:[127.0.0.1]:失败! => { 失败":是的, "msg":字段'args'的值无效,该值似乎包含未定义的变量.错误是:'dict object'没有属性'data'\ n \ n错误似乎出在'/home/ansible/cm-dha/roles/backup_api/tasks/main.yml':第48行第5列,但根据确切的语法问题,\ n可能不在文件的其他位置.\ n \ n出现问题的行可能是:\ n \ n \ n-名称:调试事实\ n ^这里\ n"
fatal: [127.0.0.1]: FAILED! => { "failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'dict object' has no attribute 'data'\n\nThe error appears to have been in '/home/ansible/cm-dha/roles/backup_api/tasks/main.yml': line 48, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: debug fact\n ^ here\n"
尝试提取值start_time_string时发生错误
The error is happening when trying to extract the value start_time_string
所以可能发生的原因是我尝试了很多事情,例如使用with_items,with_dict,模拟data []输出进行调试,甚至进行json查询,但都没有成功
so how to do it probably as I tried too many things like using with_items, with_dict , simulating the data[] output to debug and even doing a json query but without success
这里有什么帮助吗?
推荐答案
请勿使用debug
分配事实,而应使用set_fact
:
Don't use debug
to assign facts, use set_fact
instead:
- name: debug stdout
set_fact:
dataresult: "{{ result.stdout | from_json }}"
- name: debug fact
debug:
msg: "{{ dataresult.data[0].start_time_string }}"
这篇关于从json输出中提取变量,然后调试并使用ansible注册outout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!