atwright147
atwright147

Reputation: 3811

Only run Ansible task on box in specific group

I am trying to create a task that will only run on a box that is in a specific group (called pi).

I am using Ansible version:

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)]

This is my current code:

- 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

It seems to work correctly when the hostname is in the group but throws an error when not.

This is the error I get when I run it on my vagrant box (in group 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"}

Check if a list contains an item in Ansible suggests I have the syntax right but I guess that is for an older version of Ansible?

How do I fix this?

Upvotes: 3

Views: 7718

Answers (1)

larsks
larsks

Reputation: 311576

Generally, if you want tasks to apply only to hosts in a particular group, the way you do that is by creating a play that targets that group:

- 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

The error you're getting is because groups['pi']is undefined. There are a couple of ways of preventing the error. For example, you can explicitly check thatgroups['pi']` is defined before trying to use it:

- name: set up zwave USB dongle
  when: groups['pi'] is defined and inventory_hostname in groups['pi']

Or you can use the default filter to provide a default value:

- name: set up zwave USB dongle
  when: inventory_hostname in groups['pi']|default([])

Upvotes: 8

Related Questions