Ansible weird error : The error was: 'str object' has no attribute 'ip'
Ansible weird error : The error was: 'str object' has no attribute 'ip'
我在使用 Ansible 剧本时遇到了一个非常奇怪的问题。
我将 ansible 与 Flask API 一起使用,所以我使用 ansible-运行ner 将我的变量传递到我的 playbook。
我的剧本只是我的字典及其 ip 属性的调试:
- hosts: localhost
connection: local
any_errors_fatal: no
tasks:
- debug:
msg: '{{ device }}'
- debug:
msg: '{{ device["ip"] }}'
那时我什么都不懂了。
我的应用程序在 docker 容器中,这是我启动 playbook 时遇到的错误:
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
除非我 运行 我的 playbook 在 docker 之外我没有错误,而且我使用相同版本的 python 在本地或我的 [=40= 上是不可能的].
你知道那是什么吗?
Ansible 2.7.4
Python 3.5.3
如果您需要更多详细信息,请随时询问。
[编辑]
关于这个问题我尝试了一些新方法,它似乎是一个格式问题。
所以我做了一个更完整的 post HERE
您的错误是:'str object' has no attribute 'ip'
所以你的变量 device
是一个字符串,而不是字典。正好这个字符串是一个json对象的序列化。
您可以使用 from_json
filter 解决此问题,它将您的 json 字符串转换为相应的数据结构。
- debug:
msg: '{{ device | from_json }}'
- debug:
msg: '{{ (device | from_json)["ip"] }}'
然后你将不得不找出为什么当你从本地主机 运行 时得到一个正确的 json 对象,当你从你的 运行 时得到一个 json 格式的字符串docker 容器。但那是另外一回事....
尝试如下
- debug:
msg: "{{device.ip}}"
我在使用 Ansible 剧本时遇到了一个非常奇怪的问题。
我将 ansible 与 Flask API 一起使用,所以我使用 ansible-运行ner 将我的变量传递到我的 playbook。
我的剧本只是我的字典及其 ip 属性的调试:
- hosts: localhost
connection: local
any_errors_fatal: no
tasks:
- debug:
msg: '{{ device }}'
- debug:
msg: '{{ device["ip"] }}'
那时我什么都不懂了。 我的应用程序在 docker 容器中,这是我启动 playbook 时遇到的错误:
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
除非我 运行 我的 playbook 在 docker 之外我没有错误,而且我使用相同版本的 python 在本地或我的 [=40= 上是不可能的].
你知道那是什么吗?
Ansible 2.7.4
Python 3.5.3
如果您需要更多详细信息,请随时询问。
[编辑]
关于这个问题我尝试了一些新方法,它似乎是一个格式问题。 所以我做了一个更完整的 post HERE
您的错误是:'str object' has no attribute 'ip'
所以你的变量 device
是一个字符串,而不是字典。正好这个字符串是一个json对象的序列化。
您可以使用 from_json
filter 解决此问题,它将您的 json 字符串转换为相应的数据结构。
- debug:
msg: '{{ device | from_json }}'
- debug:
msg: '{{ (device | from_json)["ip"] }}'
然后你将不得不找出为什么当你从本地主机 运行 时得到一个正确的 json 对象,当你从你的 运行 时得到一个 json 格式的字符串docker 容器。但那是另外一回事....
尝试如下
- debug:
msg: "{{device.ip}}"