我正在尝试创建一个只能在特定组(称为axis
)中的框上运行的任务。
我使用的是Ansible版本:
pi
这是我目前的代码:
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)]
当主机名在组中时似乎正常工作但在没有主机名时抛出错误。
这是我在我的流浪盒上(在- 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
组中)运行时出现的错误:
vagrant
Check if a list contains an item in Ansible建议我的语法正确,但我想这是旧版本的Ansible?
我该如何解决这个问题?
答案 0 :(得分:4)
通常,如果您希望任务仅应用于特定组中的主机,则执行此操作的方法是创建以该组为目标的游戏:
- 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
您获得的错误是因为在尝试使用群组之前定义了群组['] is undefined. There are a couple of ways of preventing the error. For example, you can explicitly check that
群组[' pi']`: / p>
- name: set up zwave USB dongle
when: groups['pi'] is defined and inventory_hostname in groups['pi']
或者您可以使用default
过滤器提供默认值:
- name: set up zwave USB dongle
when: inventory_hostname in groups['pi']|default([])