仅 运行 特定组中盒子上的 Ansible 任务
Only run Ansible task on box in specific group
我正在尝试创建一个任务,该任务只会 运行 在特定组(称为 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)]
这是我当前的代码:
- 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 box 上 运行 时得到的错误(在组 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"}
表明我的语法正确,但我猜这是针对旧版本的 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
你得到的错误是因为组['pi']is undefined. There are a couple of ways of preventing the error. For example, you can explicitly check that
组['pi']`在尝试使用它之前被定义:
- 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([])
我正在尝试创建一个任务,该任务只会 运行 在特定组(称为 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)]
这是我当前的代码:
- 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 box 上 运行 时得到的错误(在组 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"}
我该如何解决这个问题?
通常,如果您希望任务仅适用于特定组中的主机,您可以通过创建一个针对该组的游戏来实现:
- 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
你得到的错误是因为组['pi']is undefined. There are a couple of ways of preventing the error. For example, you can explicitly check that
组['pi']`在尝试使用它之前被定义:
- 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([])