Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
仅对特定组中的框运行Ansible任务_Ansible_Ansible 2.x_Ansible Inventory - Fatal编程技术网

仅对特定组中的框运行Ansible任务

仅对特定组中的框运行Ansible任务,ansible,ansible-2.x,ansible-inventory,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

我正在尝试创建一个任务,该任务将仅在特定组(称为
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框(在组
vagrant
中)上运行它时遇到的错误:

建议我语法正确,但我想这是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']
未定义。有几种方法可以防止错误。例如,您可以在尝试使用前明确检查是否定义了
groups['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([])
- name: set up zwave USB dongle
  when: inventory_hostname in groups['pi']|default([])