I need to parse the output of the following command:
mongo <dbname> --eval "db.isMaster()"
which produces the result as follows:
{ "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 }
I need to parse the above output to check the value of "ismaster", that's true. Please let me know how I can do this in an indispensable way.
For the moment, I'm just checking that the text "ismaster": true is output as 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"
However, it would be nice to use Ansible json processing to verify the same. Please inform.
json ansible
trial999
source share