Reputation: 6236
I have a playbook and I want to define a list of strings in my hosts file.
Here's my host file:
[dashboard]
1.2.3.4 dashboard_domain=test site_domain=['one','two','foo', 'bar']
Here's my playbook that I attempted to write using the list documentation:
---
- hosts: dashboard
gather_facts: False
remote_user: ubuntu
become: yes
tasks:
- name: ping
ping:
- debug:
msg: "Domain: {{dashboard_domain}}"
- debug:
msg: "Site: {{ item }}"
with_items: "{{site_domain}}"
However running this playbook with ansible-playbook -i hosts ping.yml
causes this error:
TASK: [debug ] ****************************************************************
fatal: [1.2.3.4] => with_items expects a list or a set
This seems to be an issue of transferring the defined list from the host file to the playbook because defining the list directly in the playbook works:
---
- hosts: dashboard
gather_facts: False
remote_user: ubuntu
become: yes
vars:
site_domain: ['one','two','foo', 'bar']
tasks:
#### APPLY HTTP-AUTH ####
- name: ping
ping:
- debug:
msg: "Domain: {{dashboard_domain}}"
- debug:
msg: "Site: {{ item }}"
with_items: "{{site_domain}}"
Upvotes: 3
Views: 8260
Reputation: 356
@techraf does answer your question and their solution is perfect if every host in the dashboard
group has a site_domain
list with different values.
Looking at your playbook, though, it seems that site_domain
is constant across the whole dashboard
group. If you had 10 hosts in dashboard
, you would have to copy the list into each host's line. To avoid the repetition, you could have a dashboard:vars
section in your inventory, where you can define variables that have the same value for all the hosts in the group:
[dashboard:vars]
site_domain="['one','two','foo', 'bar']"
[dashboard]
1.2.3.4 dashboard_domain=test
1.2.3.5 dashboard_domain=uat
1.2.3.6 dashboard_domain=integ
If your inventory folder is more structured, you could also define variables for the dashboard
group in a separate file, in YAML. Your inventory folder tree could be:
inventories
|
+-- group_vars
| \-- dashboard.yml
|
+-- hosts.ini
In that configuration, dashboard.yml
could simply be:
site_domain: ['one', 'two', 'foo', 'bar']
...or:
site_domain:
- one
- two
- foo
- bar
Upvotes: 1
Reputation: 68639
Just quote the variable value:
[dashboard]
1.2.3.4 dashboard_domain=test site_domain="['one','two','foo', 'bar']"
It seems in case of INI-formatted inventory files, Ansible does not parse the variable value if it starts with an unquoted [
and passes it as a string.
Regarding your example: I'm not sure why you're not getting an expected key=value
error on reading the inventory file, if you really have a space inside.
Upvotes: 4