Alex Cohen
Alex Cohen

Reputation: 6236

Ansible, how to define a list in host inventory?

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

Answers (2)

fabio
fabio

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

https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html#assigning-a-variable-to-many-machines-group-variables

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

https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html#organizing-host-and-group-variables

Upvotes: 1

techraf
techraf

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

Related Questions