Reputation: 1716
I have to parse the output of the following command:
mongo <dbname> --eval "db.isMaster()"
which gives output 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" 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:
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's json processing to check the same. Please advise.
Upvotes: 28
Views: 70130
Reputation: 68319
There are quite a bit of helpful filters in Ansible.
Try: when: (output_text.stdout | from_json).ismaster
Upvotes: 60
Reputation: 25
Brother Coder, honestly I got a better Method, because for 3 weeks I just could not parse it with ansible filters as it was to complicated and never worked. I just curled the FILE and used JQ parser with regex. The only thing that is required is that JQ PARSER has to be installed on the server:
To do it with ANSIBLE:
name: set-fact1 set_fact: claims1: "{{ apiaccountclaims.stdout }}"
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 }})'
Here is the playbook:
hosts: "{{ hosts | default('all') }}" become: true
vars_prompt: - name: "envid" prompt: "Please put env ID"
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 }})'
Upvotes: 0