Json在Ansible中解析 [英] Json parsing in Ansible
本文介绍了Json在Ansible中解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须解析以下命令的输出:
I have to parse the output of the following command:
mongo <dbname> --eval "db.isMaster()"
其输出如下:
{
"hosts" : [
"xxx:<port>",
"xxx:<port>",
"xxx:<port>"
],
"setName" : "xxx",
"setVersion" : xxx,
"ismaster" : true,
"secondary" : false,
"primary" : "xxx",
"me" : "xxx",
"electionId" : ObjectId("xxxx"),
"maxBsonObjectSize" : xxx,
"maxMessageSizeBytes" : xxxx,
"maxWriteBatchSize" : xxx,
"localTime" : ISODate("xxx"),
"maxWireVersion" : 4,
"minWireVersion" : 0,
"ok" : 1
}
我需要解析上面的输出以检查"ismaster"的值是否正确.请让我知道我怎么能做到这一点.
I need to parse the above output to check the value of "ismaster" is true. Please let me know how i can do this in ansible.
此刻,我只是使用以下代码检查文本"ismaster":true是否显示在输出中:
At the moment i am simply checking that the text "ismaster" : true is shown in the output using the following code:
tasks:
- name: Check if the mongo node is primary
shell: mongo <dbname> --eval "db.isMaster()"
register: output_text
- name: Run command on master
shell: <command to execute>
when: "'\"ismaster\\\" : true,' in output_text.stdout"
但是,最好使用Ansible的json处理进行检查.请告知.
However it would be nice to use Ansible's json processing to check the same. Please advise.
推荐答案
There are quite a bit of helpful filters in Ansible.
尝试:when: (output_text.stdout | from_json).ismaster
这篇关于Json在Ansible中解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文