奇怪的错误:错误是:“ str对象”没有属性“ ip”

问题描述 投票:0回答:3

我对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。

你知道那是什么吗?

Ansible 2.7.4

Python 3.5.3

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

python docker ansible debian ansible-runner
3个回答
0
投票

尝试如下

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


0
投票

您的错误指出:'str object' has no attribute 'ip'

因此您的变量device是字符串,而不是字典。碰巧这个字符串是json对象的序列化。

您可以使用from_json filter修复此问题,该操作会将json字符串转换为相应的数据结构。

from_json

然后,您必须找出为什么从本地主机运行时得到正确的json对象,而从docker容器运行时得到json格式的字符串的原因。但这是另一个故事。...


0
投票

Zeitounator:

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

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

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

我现在有新的错误消息:

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

- debug: 
    msg: '{{ (device | from_json)["ip"] }}'
© www.soinside.com 2019 - 2024. All rights reserved.