繁体   English   中英

Ansible 奇怪的错误:错误是:'str object' 没有属性 'ip'

[英]Ansible weird error : The error was: 'str object' has no attribute 'ip'

我对 Ansible 剧本有一个非常奇怪的问题。

我将 ansible 与 Flask API 一起使用,因此我使用 ansible-runner 将我的变量传递给我的剧本。

我的剧本只是我的字典及其 ip 属性的调试:

- hosts: localhost
  connection: local
  any_errors_fatal: no
  tasks:
    - debug: 
        msg: '{{ device }}' 

    - debug: 
        msg: '{{ device["ip"] }}'

那是我什么都不懂的时候。 我的应用程序位于 docker 容器中,这是我启动剧本时遇到的错误:

deploy okay [WARNING]: Unable to parse /etc/ansible/hosts as an inventory source
 [WARNING]: No inventory was parsed, only implicit localhost is available
 [WARNING]: provided hosts list is empty, only localhost is available. Note
that the implicit localhost does not match 'all'

PLAY [localhost] ***************************************************************

TASK [Gathering Facts] *********************************************************
ok: [localhost]

TASK [shell] *******************************************************************
changed: [localhost]

TASK [Set date and time] *******************************************************
ok: [localhost]

TASK [Define log filepath] *****************************************************
ok: [localhost]

TASK [Create staging folder] ***************************************************
ok: [localhost]

TASK [begin of logging file] ***************************************************
changed: [localhost]

TASK [debug] *******************************************************************
ok: [localhost] => {
    "msg": {
        "admin_password": "",
        "admin_user": "user",
        "dns1_server_ip": "0.0.0.0",
        "equipement_name": "NEW_EQUIPMENT",
        "hostname": "EQUIPMENT",
        "ip": "127.0.0.1",
        "port": 80
    }
}

TASK [debug] *******************************************************************
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'str object' has no attribute 'ip'\n\nThe error appears to have been in '/path/main.yml': line 59, column 9, 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"}

PLAY RECAP *********************************************************************
localhost                  : ok=7    changed=2    unreachable=0    failed=1

除非我在 docker 之外运行我的剧本,否则我没有错误,并且我无法在本地或我的 docker 上使用相同版本的 python。

你知道那是什么吗?

2.7.4 版

蟒蛇 3.5.3

如果您需要更多详细信息,请随时询问。

[编辑]

我已经尝试了有关此问题的新方法,但似乎是格式问题。 所以我在这里做了一个更完整的帖子

您的错误说明: 'str object' has no attribute 'ip'

所以你的可变device是一个字符串,而不是一个字典。 正好这个字符串是一个json对象的序列化。

您可以使用from_json过滤器解决此问题,该过滤器会将您的 json 字符串转换为相应的数据结构。

    - debug: 
        msg: '{{ device | from_json }}' 

    - debug: 
        msg: '{{ (device | from_json)["ip"] }}'

然后,您必须找出为什么从 localhost 运行时会得到正确的 json 对象,而从 docker 容器中运行时会得到 json 格式的字符串。 不过那是另一回事了....

尝试如下

   - debug:
      msg: "{{device.ip}}"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM