Ansible 无法从 stdout_lines 将字符串转换为字典 [英] Ansible unable to convert string to dictionary from the stdout_lines

查看:34
本文介绍了Ansible 无法从 stdout_lines 将字符串转换为字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从字典中获取计数"值

I am trying to get the "count" value from the dictionary

"{ \"_id\" : ObjectId(\"5d3a1643c43c898d01a3c740\"), \"count\" : 2 }"

"{ \"_id\" : ObjectId(\"5d3a1643c43c898d01a3c740\"), \"count\" : 2 }"

出现在 ansible stdout_lines 的最后一个元素.

present at the last element of the ansible stdout_lines.

TASK [version_update : debug] ******************************************************************************************************************************************
ok: [192.168.27.125] => {
    "count_info.stdout": "MongoDB shell version v4.0.6\nconnecting to: mongodb://127.0.0.1:27017/configure-db?gssapiServiceName=mongodb\nImplicit session: session { \"id\" : UUID(\"4bfad3ba-981f-47de-86f9-a1fadbe28e12\") }\nMongoDB server version: 4.0.6\n{ \"_id\" : ObjectId(\"5d3a1643c43c898d01a3c740\"), \"count\" : 2 }"
}

TASK [version_update : debug] ******************************************************************************************************************************************
ok: [192.168.27.125] => {
    "count_info.stdout_lines": [
        "MongoDB shell version v4.0.6",
        "connecting to: mongodb://127.0.0.1:27017/configure-db?gssapiServiceName=mongodb",
        "Implicit session: session { \"id\" : UUID(\"4bfad3ba-981f-47de-86f9-a1fadbe28e12\") }",
        "MongoDB server version: 4.0.6",
        "{ \"_id\" : ObjectId(\"5d3a1643c43c898d01a3c740\"), \"count\" : 2 }"
    ]
}

我尝试了以下两种方法但都没有成功.

I tried the following two ways but not successful.

- debug:
    msg: "{{ (count_info.stdout_lines[-1] | from_json).count }}"

- name: count value
  debug:
    msg: "{{ count_info.stdout_lines[-1] | json_query('count') }}"

错误日志:

TASK [version_update : debug] ******************************************************************************************************************************************
fatal: [192.168.27.125]: FAILED! => {"msg": "the field 'args' has an invalid value ({u'msg': u'{{ (count_info.stdout_lines[-1] | from_json).count }}'}), and could not be converted to an dict.The error was: No JSON object could be decoded\n\nThe error appears to have been in '/home/admin/playbook-3/roles/version_update/tasks/version_update.yml': line 73, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- debug:\n  ^ here\n"}
        to retry, use: --limit @/home/admin/playbook-3/version_update.retry

TASK [version_update : count value] ************************************************************************************************************************************
ok: [192.168.27.125] => {
    "msg": ""
}

推荐答案

您在输出中的最后一行不是纯 json 字符串(可能是 来自 MongoDB 输出的 bson).您得到的错误实际上来自过滤器本身没有获得正确的输入和失败.

Your last line in the output is not a pure json string (probably bson from your MongoDB output). The error you get is actually coming from the filter itself not getting a correct input and failing.

您必须先将其转换为纯 json,然后才能使用 from_json 过滤器.违规数据是不能被过滤器反序列化的ObjectId(\5d3a1643c43c898d01a3c740\").这应该在您用来注册变量的任务/命令中进行更改.您可以查看关于该主题的这个有趣的问题,其中有许多答案可能会给你一些线索.

You will have to translate that to pure json before you can use the from_json filter. The offending data is the ObjectId(\"5d3a1643c43c898d01a3c740\") that cannot be deserialized by the filter. This should be changed in the task/command you use to register your variable. You can have a look at this interesting question on the subject with many answers that will probably give you some clues.

一旦完成,访问您的数据就变得很容易,正如您已经想到的那样.这是一个修改后的样本数据的例子(我认为你最终应该得到它的方式)只是为了确认你在正确的轨道上的位置.

Once this is done, accessing your data becomes easy as you had already figured out. Here is an example with a modified sample data (the way I think you should finally get it) just to confirm your where on the right track.

- name: Get count in json serialized string
  hosts: localhost
  gather_facts: false

  vars:
    "count_info":
      "stdout_lines": [
        "MongoDB shell version v4.0.6",
        "connecting to: mongodb://127.0.0.1:27017/configure-db?gssapiServiceName=mongodb",
        "Implicit session: session { \"id\" : UUID(\"4bfad3ba-981f-47de-86f9-a1fadbe28e12\") }",
        "MongoDB server version: 4.0.6",
        "{ \"_id\" : \"someDeserializedId\", \"count\" : 2 }"
      ]

  tasks:
    - name: Get count
      debug:
        msg: "{{ (count_info.stdout_lines[-1] | from_json).count }}"

结果

PLAY [Get count in json serialized string] ********************************************************************************************************************************************************************************

TASK [Get count] **********************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "2"
}

这篇关于Ansible 无法从 stdout_lines 将字符串转换为字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆