简体   繁体   English

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

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

I have a very strange problem with an Ansible playbook.我对 Ansible 剧本有一个非常奇怪的问题。

I use ansible with a Flask API, so I use ansible-runner to pass my variables to my playbook.我将 ansible 与 Flask API 一起使用,因此我使用 ansible-runner 将我的变量传递给我的剧本。

My playbook is just a debug of my dictionary and its ip attribute:我的剧本只是我的字典及其 ip 属性的调试:

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

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

And that's when I don't understand anything anymore.那是我什么都不懂的时候。 My application is in a docker container and this is the error I get when I launch my playbook:我的应用程序位于 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

Except if I run my playbook outside the docker I have no errors, and I use the same versions of python is not possible locally or on my docker.除非我在 docker 之外运行我的剧本,否则我没有错误,并且我无法在本地或我的 docker 上使用相同版本的 python。

Do you have any idea what that is?你知道那是什么吗?

Ansible 2.7.4 2.7.4 版

Python 3.5.3蟒蛇 3.5.3

If you need more details don't hesitate to ask.如果您需要更多详细信息,请随时询问。

[EDIT] [编辑]

I've try new things about this issues, and it's appear to be a format problem.我已经尝试了有关此问题的新方法,但似乎是格式问题。 So i've made a more complete post HERE所以我在这里做了一个更完整的帖子

Your error is stating: 'str object' has no attribute 'ip'您的错误说明: 'str object' has no attribute 'ip'

So your variable device is a string, not a dict.所以你的可变device是一个字符串,而不是一个字典。 It happens that this string is the serialization of a json object.正好这个字符串是一个json对象的序列化。

You can fix this by using the from_json filter which will transform your json string to the corresponding data structure.您可以使用from_json过滤器解决此问题,该过滤器会将您的 json 字符串转换为相应的数据结构。

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

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

Then you will have to find out why you get a correct json object when you run from localhost and a json formated string when your run from your docker container.然后,您必须找出为什么从 localhost 运行时会得到正确的 json 对象,而从 docker 容器中运行时会得到 json 格式的字符串。 But that is an other story....不过那是另一回事了....

Try as below尝试如下

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

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

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