Question:
I have to parse the output of the following command:
1 2 |
mongo |
which gives output as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
{ "hosts" : [ "xxx: "xxx: "xxx: ], "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 } |
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.
At the moment i am simply checking that the text “ismaster” : true is shown in the output using the following code:
1 2 3 4 5 6 7 8 9 |
tasks: - name: Check if the mongo node is primary shell: mongo register: output_text - name: Run command on master shell: when: "'\"ismaster\\\" : true,' in output_text.stdout" |
However it would be nice to use Ansible’s json processing to check the same. Please advise.
Answer:
There are quite a bit of helpful filters in Ansible.
Try: when: (output_text.stdout | from_json).ismaster