In a vars/main.yml file I have the following:
var_list: "{% if somevar == 'someval' %}
- host1
- host2
- host3
- host4
{% elif somevar == 'differentval' %}
- host5
- host6
{% endif %}"
Then I have a tasks/main.yml with this:
- name: Do these things on specific hosts
command:
cmd: "echo hello"
delegate_to: "{{ item }}"
with_items: "{{ var_list }}"
The output I get when I run the playbook fails with
ansible_loop_var: item
item: ' - host5 - host6 '
msg: 'Failed to connect to the host....
So Ansible is treating "var_list" as a string with all values stringed together (including the hyphens), rather than as a list as desired. I have tried adding and removing hyphens to the opening and closing {%- -%}, and I tried the alternate method of using [host1,host2] on a single line, but again it was not turned into a list. What am I missing here?