Json parsing in Ansible

后端 未结 2 1669
萌比男神i
萌比男神i 2021-01-03 22:08

I have to parse the output of the following command:

mongo  --eval \"db.isMaster()\"

which gives output as follows:



        
相关标签:
2条回答
  • 2021-01-03 22:36

    There are quite a bit of helpful filters in Ansible.

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

    0 讨论(0)
  • 2021-01-03 22:40

    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 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/

    2. Extract the Value:

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

    3. Register as Variable :

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

      1. USE IT ANYWHERE:
    • 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 }})'
      
    0 讨论(0)
提交回复
热议问题