Ansible 中的 Json 解析

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40844720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:43:22  来源:igfitidea点击:

Json parsing in Ansible

jsonansible

提问by trial999

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.

我需要解析上面的输出来检查“ismaster”的值是否为真。请让我知道我如何在 ansible 中做到这一点。

At the moment i am simply checking that the text "ismaster" : true is shown in the output using the following code:

目前,我只是使用以下代码检查输出中是否显示文本 "ismaster" : true :

  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.

然而,使用 Ansible 的 json 处理来检查它会很好。请指教。

回答by Konstantin Suvorov

There are quite a bit of helpful filtersin Ansible.

Ansible 中有很多有用的过滤器

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

尝试: when: (output_text.stdout | from_json).ismaster

回答by alexei G.

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:

Coder 兄弟,老实说我得到了一个更好的方法,因为在 3 周的时间里,我无法使用 ansible 过滤器解析它,因为它很复杂而且从来没有工作过。我只是卷曲了 FILE 并使用了带有正则表达式的 JQ 解析器。唯一需要的是必须在服务器上安装 JQ PARSER:

To do it with ANSIBLE:

用 ANSIBLE 做到这一点:

Use host prompt to choose the envid in the

使用主机提示在

1. curl file like this:

1. curl 文件是这样的:



2. Extract the Value:

2. 提取值:

  • name: get value from file shell: cat file.json | jq '.globals.environments.{{envid}}."legacy-claimcenter-hostname"' | sed 's/"//g'args: chdir: /tmp/ register: apiaccountclaims
  • 名称:从文件shell 中获取值 :cat file.json | jq '.globals.environments.{{envid}}."legacy-claimcenter-hostname"' | sed 's/"//g'args: chdir: /tmp/ register: apiaccountclaims


3. Register as Variable :

3. 注册为变量:

  • 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 }})'

  • 名称:set-fact1 set_fact:claim1:“{{ apiaccountclaims.stdout }}”

    1. 在任何地方使用它:
  • 名称:进入服务tdiapiaccountclaims shell:sudo /usr/share/jbossas/bin/jboss-cli.sh -c --command='/system-property=tdigi.api.uri.edge.account.claims:add(value= {{ claim1 }})'

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 }})'
    
  • 主机:“{{ hosts | default('all') }}” 变为:true

    vars_prompt: - name: "envid" prompt: "请输入 env ID"

    任务:

     - 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 }})'