# Create config from array with 1 element

**URL:** <https://forum.ansible.com/t/create-config-from-array-with-1-element/43186>\
**Category:** Get Help\
**Created:** [June 5, 2025, 4:12pm UTC](https://forum.ansible.com/t/create-config-from-array-with-1-element/43186 "2025-06-05T16:12:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mak\_S](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/mak_s/32/8708_2.png) [@Mak\_S](https://forum.ansible.com/u/Mak_S)\
**Post date:** [June 5, 2025, 4:12pm UTC](https://forum.ansible.com/t/create-config-from-array-with-1-element/43186/1 "2025-06-05T16:12:59Z")

</div>

Hi all.

I have an application with j2 config.yaml:

```auto
    address1:
      redirect_urls: {{ web1_address }}
    address2:
      redirect_urls: {{ web2_address }}

```

and variables:

```auto
web1_address:
  - https://web1.{{ stack_dns_zone }}/endpoint
web2_address:
  - https://web2.{{ stack_dns_zone }}/endpoint

```

When I use array with only 1 element, it generates a wrong config(in one string, with square brackets and quotes):

```auto
    address1:
      redirect_urls: ["https://web1.{{ stack_dns_zone }}/endpoint"]
    address2:
      redirect_urls: ["https://web2.{{ stack_dns_zone }}/endpoint"]

```

but I need to get this structure:

```auto
    address1:
      redirect_urls: 
        - https://web1.{{ stack_dns_zone }}/endpoint
    address2:
      redirect_urls: 
        - https://web2.{{ stack_dns_zone }}/endpoint

```

Could you answer, please: how I need to retrieve vars from array with 1 element, to generate right config with required structure?

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [June 5, 2025, 5:52pm UTC](https://forum.ansible.com/t/create-config-from-array-with-1-element/43186/2 "2025-06-05T17:52:36Z")

</div>

Your template says to interpolate complex (more than simple scalars) data without specifying the format. The default formatted yaml is equivalent to what you want, but if you want it to look another way you need to be specific. Try this in your template:

```yaml
    address1:
      redirect_urls: {{ web1_address | to_nice_yaml() }}
    address2:
      redirect_urls: {{ web2_address | to_nice_yaml() }}

```

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [June 5, 2025, 6:57pm UTC](https://forum.ansible.com/t/create-config-from-array-with-1-element/43186/3 "2025-06-05T18:57:20Z")

</div>

The filter _to\_nice\_yaml_ is a good start. Complete it with the filter _indent_. For example,

```yaml
- copy:
    dest: /tmp/config.yml
    content: |
      address1:
        redirect_urls:
          {{ web1_address | to_nice_yaml(indent=2) | indent(4) }}
      address2:
        redirect_urls:
          {{ web2_address | to_nice_yaml(indent=2) | indent(4) }}
  vars:
    stack_dns_zone: example.com
    web1_address:
      - https://web1.{{ stack_dns_zone }}/endpoint
      - https://web3.{{ stack_dns_zone }}/endpoint
    web2_address:
      - https://web2.{{ stack_dns_zone }}/endpoint
      - https://web4.{{ stack_dns_zone }}/endpoint

```

gives

```bash
shell> cat /tmp/config.yml
address1:
  redirect_urls:
    - https://web1.example.com/endpoint
    - https://web3.example.com/endpoint

address2:
  redirect_urls:
    - https://web2.example.com/endpoint
    - https://web4.example.com/endpoint

```

---

<div class="post-metadata">

**Author:** ![Mak\_S](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/mak_s/32/8708_2.png) [@Mak\_S](https://forum.ansible.com/u/Mak_S)\
**Post date:** [June 6, 2025, 6:29am UTC](https://forum.ansible.com/t/create-config-from-array-with-1-element/43186/4 "2025-06-06T06:29:50Z")

</div>

@utoddl @vbotka good morning!  
Thank you for reply with good examples and description:-)  
I’ll try it now.

---

<div class="post-metadata">

**Author:** ![Mak\_S](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/mak_s/32/8708_2.png) [@Mak\_S](https://forum.ansible.com/u/Mak_S)\
**Post date:** [June 9, 2025, 6:06am UTC](https://forum.ansible.com/t/create-config-from-array-with-1-element/43186/5 "2025-06-09T06:06:59Z")

</div>

Hello!  
It works.  
Thank you very much!
