Ansible 中的 Json 解析 [英] Json parsing in Ansible
本文介绍了Ansible 中的 Json 解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须解析以下命令的输出:
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"的值是否为真.请让我知道我如何在 ansible 中做到这一点.
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.
推荐答案
有很多有用的过滤器 在 Ansible 中.
There are quite a bit of helpful filters in Ansible.
尝试:when:(output_text.stdout | from_json).ismaster
这篇关于Ansible 中的 Json 解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文