0

I'm working on an Ansible role that can add hosts from a host group to a cluster. However, I'd like to make sure the task doesn't execute if there is only a single host in the group. That way a single node won't attempt to join itself in a cluster. I've used something like this in jinja2 templates before:

{% if var is iterable %}

But is there a way to do this using a "when" statement and a host group? I'm looking for something like this:

task: Join cluster
shell: do the join {{item}}
with_items: "{{ groups['lab'] }}"
when: groups['lab'] is iterable

Solution: when: "groups['lab'] | length > 1"

user3270760
  • 1,444
  • 5
  • 23
  • 45
  • This isn't a jinja2 template, this is an Ansible task inside of a role – user3270760 Jun 12 '18 at 15:11
  • I found an answer: `when: "groups['lab'] | length > 1"` – user3270760 Jun 12 '18 at 15:16
  • 1
    This is a Jinja2 template. Whatever you write in `when` value **is** a Jinja2 expression. Quote from docs: ["This is easy to do in Ansible with the *when* clause, which contains a raw Jinja2 expression without double curly braces"](https://docs.ansible.com/ansible/latest/user_guide/playbooks_conditionals.html#the-when-statement) – techraf Jun 12 '18 at 15:29

0 Answers0