How to assign an array to a variable in an Ansible-Playbook

前端 未结 5 784
南方客
南方客 2020-12-23 14:49

In a playbook I got the following code:

---
- hosts: db
  vars:
    postgresql_ext_install_contrib: yes
    postgresql_pg_hba_passwd_hosts: [\'10.129.181.241         


        
5条回答
  •  心在旅途
    2020-12-23 15:21

    in playbook:

    vars:
         - arrayname:
            - name: itemname
              value1: itemvalue1
              value2: itemvalue2
            - name: otheritem
              value1: itemvalue3
              value2: itemvalue4
    

    in template: (example is of type ini file, with sections, keys and values):

    {% for item in arrayname %}
    [{{ item.name }}]
    key1 = {{ item.value1 }}
    key2 = {{ item.value2 }}
    {% endfor %}
    

    This should render the template as:

    [itemname]
    key1 = itemvalue1
    key2 = itemvalue2
    [otheritem]
    key1 = itemvalue3
    key2 = itemvalue4
    

提交回复
热议问题