Json parsing in Ansible
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:
Use host prompt to choose the envid in the
1. curl file like this:
- name: Get json fileshell: curl --output file.json -k -O https://example.tp.com/services/getMasterExtract.php?env_id={{envid}}&product=all&du=all&format=json&resolved=trueargs:chdir: /tmp/
2. Extract the Value:
- name: get value from fileshell: cat file.json | jq '.globals.environments.{{envid}}."legacy-claimcenter-hostname"' | sed 's/"//g'args:chdir: /tmp/register: apiaccountclaims
3. Register as Variable :
name: set-fact1set_fact:claims1: "{{ apiaccountclaims.stdout }}"
- USE IT ANYWHERE:
name: Enter service tdiapiaccountclaimsshell: 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 }})'