Ansible, how to define a list in host inventory?

北城以北 提交于 2019-12-13 21:31:11

问题


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}}"

回答1:


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.



来源:https://stackoverflow.com/questions/42149975/ansible-how-to-define-a-list-in-host-inventory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!