Json parsing in Ansible

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.

+11
json ansible
source share
2 answers

Ansible has quite a few useful filters .

Try: when: (output_text.stdout | from_json).ismaster

+27
source share

Brother Coder, to be honest, I have a better method, because for 3 weeks I just couldn’t parse it using a filter that was complicated and never worked. I just collapsed the FILE and used a regular expression JQ parser. The only thing required is that JQ PARSER must be installed on the server:

To do this with ANSIBLE:

Use the host prompt to select envid in

1. curl file like this:


2. Retrieve the value:

  • name: get value from file shell: cat file.json | jq '.globals.environments. {{envid}}. "legacy -c laimcenter-hostname" '| sed 's / "// g' args: chdir: / tmp / register: apiaccountclaims

3. Register as a variable:

  • name: set-fact1 set_fact: arguments1: "{{apiaccountclaims.stdout}}"

    1. Use it anywhere:
  • name: enter the tdiapiaccountclaims shell service: sudo / usr / share / jbossas / bin / jboss-cli.sh -c - -c ommand = '/ system-property = tdigi.api.uri.edge.account.claims: add (value = {{claim1}}) '

Here is a game book:


  • hosts: "{{hosts | default ('all')}} become: true

    vars_prompt: - name: invitation "envid": "Please enter ID env"

    tasks:

      - name: Get json file shell: curl --output file.json -k -O https://example.tp.com/services/getMasterExtract.php?env_id={{envid}}&product=all&du=all&format=json&resolved=true args: chdir: /tmp/ - name: get value from file shell: cat file.json | jq '.globals.environments.{{envid}}."legacy-claimcenter-hostname"' | sed 's/"//g' args: chdir: /tmp/ register: tdiapiaccountclaims - name: set-fact1 set_fact: claims1: "{{ apiaccountclaims.stdout }}" - name: copy command file copy: src: "cli/systemprops2-2.cli" dest: "/opt/jboss/profiles/{{jboss_profile}}/configuration/" - name: backup standalone-full.xml shell: cp "/opt/jboss/profiles/{{jboss_profile}}/configuration/standalone-full.xml" "/opt/jboss/profiles/{{jboss_profile}}/configuration/standalone-full.xml.backup.old" - name: Delete Configs in file of standalone-full.xml shell: sudo /usr/share/jbossas/bin/jboss-cli.sh -c --file=systemprops2-2.cli args: chdir: /opt/jboss/profiles/{{ jboss_profile }}/configuration register: delvar - name: Enter service tdiapiaccountclaims shell: sudo /usr/share/jbossas/bin/jboss-cli.sh -c --command='/system-property=tdigi.api.uri.edge.account.claims:add(value={{ claims1 }})' 
+1
source share

All Articles