0

I have the following value

TASK [install_mongodb : debug] *************************************************
ok: [mongodb.primary] => 
  msg: '"{\n\t\"set\" : \"mycluster\",\n\t\"date\" : ISODate(\"2019-03-19T14:57:25.638Z\"),\n\t\"myState\" : 1,\n\t\"term\" : NumberLong(10),\n\t\"syncingTo\" : \"\",\n\t\"syncSourceHost\" : \"\",\n\t\"syncSourceId\" : -1,\n\t\"heartbeatIntervalMillis\" : NumberLong(2000),\n\t\"optimes\" : {\n\t\t\"lastCommittedOpTime\" : {\n\t\t\t\"ts\" : Timestamp(1553007444, 1),\n\t\t\t\"t\" : NumberLong(10)\n\t\t},\n\t\t\"readConcernMajorityOpTime\" : {\n\t\t\t\"ts\" : Timestamp(1553007444, 1),\n\t\t\t\"t\" : NumberLong(10)\n\t\t},\n\t\t\"appliedOpTime\" : {\n\t\t\t\"ts\" : Timestamp(1553007444, 1),\n\t\t\t\"t\" : NumberLong(10)\n\t\t},\n\t\t\"durableOpTime\" : {\n\t\t\t\"ts\" : Timestamp(1553007444, 1),\n\t\t\t\"t\" : NumberLong(10)\n\t\t}\n\t},\n\t\"lastStableCheckpointTimestamp\" : Timestamp(1553007393, 1),\n\t\"members\" : [\n\t\t{\n\t\t\t\"_id\" : 0,\n\t\t\t\"name\" : \"mongodb.primary:27017\",\n\t\t\t\"health\" : 1,\n\t\t\t\"state\" : 1,\n\t\t\t\"stateStr\" : \"PRIMARY\",\n\t\t\t\"uptime\" : 524,\n\t\t\t\"optime\"
    : {\n\t\t\t\t\"ts\" : Timestamp(1553007444, 1),\n\t\t\t\t\"t\" : NumberLong(10)\n\t\t\t},\n\t\t\t\"optimeDate\" : ISODate(\"2019-03-19T14:57:24Z\"),\n\t\t\t\"syncingTo\" : \"\",\n\t\t\t\"syncSourceHost\" : \"\",\n\t\t\t\"syncSourceId\" : -1,\n\t\t\t\"infoMessage\" : \"\",\n\t\t\t\"electionTime\" : Timestamp(1553006922, 1),\n\t\t\t\"electionDate\" : ISODate(\"2019-03-19T14:48:42Z\"),\n\t\t\t\"configVersion\" : 1,\n\t\t\t\"self\" : true,\n\t\t\t\"lastHeartbeatMessage\" : \"\"\n\t\t}\n\t],\n\t\"ok\" : 1,\n\t\"operationTime\" : Timestamp(1553007444, 1),\n\t\"$clusterTime\" : {\n\t\t\"clusterTime\" : Timestamp(1553007444, 1),\n\t\t\"signature\" : {\n\t\t\t\"hash\" : BinData(0,\"AAAAAAAAAAAAAAAAAAAAAAAAAAA=\"),\n\t\t\t\"keyId\" : NumberLong(0)\n\t\t}\n\t}\n}"'

accessed as follows:

  - name: debug
    debug:
      msg: "{{ (rv_cluster_status.stdout | to_json) }}"

How can I get the value corresponding to the ok field from the json above?

The following does not work

"{{ (rv_cluster_status.stdout | from_json).ok }}"

It fails as follows:

fatal: [mongodb.primary]: FAILED! => 
  msg: |-
    the field 'args' has an invalid value ({u'msg': u"{{ rv_cluster_status.stdout | replace('\n', '') | replace('\t', '') | from_json }}"}), and could not be converted to an dict.The error was: No JSON object could be decoded
pkaramol
  • 16,451
  • 43
  • 149
  • 324
  • could you please post the raw json output, which you use as input for the `debug` statement, i.e. the raw contents of `rv_cluster_status.stdout`. – JGK Mar 19 '19 at 16:12

1 Answers1

0

This should work:

- name: debug
  debug:
    msg: "{{ rv_cluster_status.stdout | json_query('ok') }}"
JGK
  • 3,710
  • 1
  • 21
  • 26
  • I have tried the same in my case I got nothing in output. Please check the query - https://stackoverflow.com/questions/57217842/ansible-unable-to-convert-string-to-dictionary-from-the-stdout-lines – Bhavani Prasad Jul 26 '19 at 10:10