# Prevent to\_nice\_yaml filter from quoting integers when populated from variable

**URL:** <https://forum.ansible.com/t/prevent-to-nice-yaml-filter-from-quoting-integers-when-populated-from-variable/8823>\
**Category:** Get Help\
**Tags:** yaml, ansible-core\
**Created:** [September 27, 2024, 4:16pm UTC](https://forum.ansible.com/t/prevent-to-nice-yaml-filter-from-quoting-integers-when-populated-from-variable/8823 "2024-09-27T16:16:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![spike](https://avatars.discourse-cdn.com/v4/letter/s/8797f3/32.png) [@spike](https://forum.ansible.com/u/spike)\
**Post date:** [September 27, 2024, 4:16pm UTC](https://forum.ansible.com/t/prevent-to-nice-yaml-filter-from-quoting-integers-when-populated-from-variable/8823/1 "2024-09-27T16:16:09Z")

</div>

Hi,

I’m trying to deploy the contents of a dictionary to a yaml file:

```auto
- name: Deploy template
  vars:
    - cloud_init_config:
        fqdn: "{{ inventory_hostname }}"
        prefer_fqdn_over_hostname: true
        manage_etc_hosts: true
        ssh_pwauth: true
        users:
          - name: "{{ cloud_init_user }}"
            uid: "{{ cloud_init_user_uid | int }}"
  ansible.builtin.template:
    src: cloud_init.j2
    dest: /tmp/cloud_init
  delegate_to: localhost
  become: false

```

The template I’m using looks like this:

```auto
{{ cloud_init_config | to_nice_yaml(indent=2) }}

```

Which results in:

```auto
$ cat /tmp/cloud_init 
fqdn: test.example.com
manage_etc_hosts: true
prefer_fqdn_over_hostname: true
ssh_pwauth: true
users:
- name: spike
  uid: '1000'

```

Is there a way to prevent the `uid` from being quoted? Using a string for the `uid` parameter was deprecated with cloud-init 22.3 and I’m trying to fix it before it becomes an issue.

When I don’t set the value for the `uid` key from a variable everything works as expected, i.e.:

```auto
- name: Deploy template
  vars:
    - cloud_init_config:
        fqdn: "{{ inventory_hostname }}"
        prefer_fqdn_over_hostname: true
        manage_etc_hosts: true
        ssh_pwauth: true
        users:
          - name: "{{ cloud_init_user }}"
            uid: 1000
  ansible.builtin.template:
    src: cloud_init.j2
    dest: /tmp/cloud_init
  delegate_to: localhost
  become: false

```

leads to

```auto
$ cat /tmp/cloud_init 
fqdn: test.example.com
manage_etc_hosts: true
prefer_fqdn_over_hostname: true
ssh_pwauth: true
users:
- name: spike
  uid: 1000

```

Any pointers are greatly appreciated!

---

<div class="post-metadata">

**Author:** ![flowerysong](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/flowerysong/32/2305_2.png) [@flowerysong](https://forum.ansible.com/u/flowerysong)\
**Post date:** [September 27, 2024, 4:27pm UTC](https://forum.ansible.com/t/prevent-to-nice-yaml-filter-from-quoting-integers-when-populated-from-variable/8823/2 "2024-09-27T16:27:27Z")

</div>

Use [`jinja2_native`](https://docs.ansible.com/ansible/latest/reference_appendices/config.html#default-jinja2-native) so that templating can return non-strings, or template more of the structure so that Ansible can parse the returned string back into a data structure.

```auto
        users:
          - "{{ { 'name': cloud_init_user, 'uid': cloud_init_user_uid | int } }}"

```

---

<div class="post-metadata">

**Author:** ![spike](https://avatars.discourse-cdn.com/v4/letter/s/8797f3/32.png) [@spike](https://forum.ansible.com/u/spike)\
**Post date:** [September 27, 2024, 4:35pm UTC](https://forum.ansible.com/t/prevent-to-nice-yaml-filter-from-quoting-integers-when-populated-from-variable/8823/3 "2024-09-27T16:35:37Z")

</div>

Enabling `jinja2_native` didn’t make a difference I’m afraid.

This worked though:

```auto
users:
  - "{{ { 'name': cloud_init_user, 'uid': cloud_init_user_uid | int } }}"

```

Thanks a bunch!

---

<div class="post-metadata">

**Author:** ![flowerysong](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/flowerysong/32/2305_2.png) [@flowerysong](https://forum.ansible.com/u/flowerysong)\
**Post date:** [September 27, 2024, 5:21pm UTC](https://forum.ansible.com/t/prevent-to-nice-yaml-filter-from-quoting-integers-when-populated-from-variable/8823/4 "2024-09-27T17:21:20Z")

</div>

Ah, I always forget that the template module is not affected by `jinja2_native`.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [October 27, 2024, 5:21pm UTC](https://forum.ansible.com/t/prevent-to-nice-yaml-filter-from-quoting-integers-when-populated-from-variable/8823/5 "2024-10-27T17:21:29Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
