我对 Ansible 剧本有一个非常奇怪的问题。
我将 ansible 与 Flask API 一起使用,因此我使用 ansible-runner 将变量传递给我的剧本。
我的剧本只是我的字典及其 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
除非我在 docker 之外运行我的剧本,否则我没有错误,并且我无法在本地或我的 docker 上使用相同版本的 python。
你知道那是什么吗?
Ansible 2.7.4
Python 3.5.3
如果您需要更多详细信息,请随时询问。
[编辑]
我已经尝试过有关此问题的新方法,这似乎是格式问题。所以我在这里写了一个更完整的帖子