【问题标题】:Json parsing in AnsibleAnsible 中的 Json 解析
【发布时间】:2016-11-28 12:46:24
【问题描述】:

我必须解析以下命令的输出:

mongo <dbname> --eval "db.isMaster()"

输出如下:

 {
    "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
}

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

目前,我只是使用以下代码检查输出中是否显示文本 "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"

但是,使用 Ansible 的 json 处理来检查同样的情况会很好。请指教。

【问题讨论】:

    标签: json ansible


    【解决方案1】:

    在 Ansible 中有很多有用的 filters

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

    【讨论】:

    • 当我尝试执行此操作时出现此错误:“条件检查 '(output_text.stdout | from_json).ismaster' 失败。错误是:无法解码 JSON 对象\n\n错误似乎位于“xxxx”:第 xx 行,第 x 列,但可能\n位于文件中的其他位置,具体取决于确切的语法问题。\n\n有问题的行似乎是:\n\n\n - 名称:运行主控上的命令\n ^ 这里\n"}
    • 哎呀......你没有像ObjectId()ISODate()这样的纯JSON内容。不确定是否有一个简单的解决方法。
    • 已使用此命令代替输出“true”。 mongo --eval "db.isMaster().ismaster"。谢谢指导。
    【解决方案2】:

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

    使用 ANSIBLE:

    使用主机提示选择envid中的

    1。 curl 文件如下:


    2。提取值:

    • 名称:从文件中获取值 外壳:猫文件.json | jq '.globals.environments.{{envid}}."legacy-claimcenter-hostname"' | sed 's/"//g' 参数: chdir:/tmp/ 注册:apiaccountclaims

    3。注册为变量:

    • 名称:set-fact1 设置事实: 声明1:“{{ apiaccountclaims.stdout }}”

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

    这是剧本:


    • 主机:“{{主机|默认('全部')}}” 变成:真

      vars_prompt: - 名称:“羡慕” 提示:“请输入环境 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 }})'
      

    【讨论】:

    • 我的 2 美分:通常,至少从我的角度来看,使用 shell 模块意味着你做得不对……
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-18
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多