atw*_*147 2 ansible ansible-2.x ansible-inventory
我正在尝试创建仅在特定组(称为pi)中的框上运行的任务。
我正在使用Ansible版本:
ansible 2.3.2.0
config file = /Users/user/Development/raspbian-homeassistant/ansible.cfg
configured module search path = Default w/o overrides
python version = 3.6.3 (default, Dec 3 2017, 10:37:53) [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.38)]
Run Code Online (Sandbox Code Playgroud)
这是我当前的代码:
- name: Set up zwave USB dongle
when: inventory_hostname in groups['pi']
blockinfile:
path: /var/local/homeassistant/.homeassistant/configuration.yaml
marker: "# {mark} ANSIBLE MANAGED BLOCK #"
insertafter: "# zwave dongle"
content: |2
zwave:
usb_path: /dev/ttyACM0
tags:
- config
- hass
Run Code Online (Sandbox Code Playgroud)
当主机名在组中时,它似乎可以正常工作,而在主机名不在时,则抛出错误。
这是我在无业游民的盒子(在组中vagrant)上运行时遇到的错误:
fatal: [192.168.33.123]: FAILED! => {"failed": true, "msg": "The conditional check 'inventory_hostname in groups['pi']' failed. The error was: error while evaluating conditional (inventory_hostname in groups['pi']): Unable to look up a name or access an attribute in template string ({% if inventory_hostname in groups['pi'] %} True {% else %} False {% endif %}).\nMake sure your variable name does not contain invalid characters like '-': argument of type 'StrictUndefined' is not iterable\n\nThe error appears to have been in '/Users/andy/Development/raspbian-homeassistant/ansible/roles/configure-hass/tasks/main.yml': line 21, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Set up zwave USB dongle\n ^ here\n"}
Run Code Online (Sandbox Code Playgroud)
检查列表中是否包含Ansible中的项目,建议我具有正确的语法,但我想这是针对较旧版本的Ansible吗?
我该如何解决?
通常,如果您希望任务仅适用于特定组中的主机,则可以通过创建针对该组的播放方式来做到这一点:
- hosts: pi
tasks:
- name: Set up zwave USB dongle
blockinfile:
path: /var/local/homeassistant/.homeassistant/configuration.yaml
marker: "# {mark} ANSIBLE MANAGED BLOCK #"
insertafter: "# zwave dongle"
content: |2
zwave:
usb_path: /dev/ttyACM0
tags:
- config
- hass
Run Code Online (Sandbox Code Playgroud)
您收到的错误是因为is undefined. There are a couple of ways of preventing the error. For example, you can explicitly check that在尝试使用groups ['pi'] groups ['pi']`之前已对其进行了定义:
- name: set up zwave USB dongle
when: groups['pi'] is defined and inventory_hostname in groups['pi']
Run Code Online (Sandbox Code Playgroud)
或者,您可以使用default过滤器提供默认值:
- name: set up zwave USB dongle
when: inventory_hostname in groups['pi']|default([])
Run Code Online (Sandbox Code Playgroud)