# Append new key value pair with correct indentation in yml file

**URL:** <https://forum.ansible.com/t/append-new-key-value-pair-with-correct-indentation-in-yml-file/36735>\
**Category:** Ansible Project\
**Tags:** ee\
**Created:** [June 14, 2022, 1:34pm UTC](https://forum.ansible.com/t/append-new-key-value-pair-with-correct-indentation-in-yml-file/36735 "2022-06-14T13:34:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shivani\_Arora](https://avatars.discourse-cdn.com/v4/letter/s/919ad9/32.png) [@Shivani\_Arora](https://forum.ansible.com/u/Shivani_Arora)\
**Post date:** [June 14, 2022, 1:34pm UTC](https://forum.ansible.com/t/append-new-key-value-pair-with-correct-indentation-in-yml-file/36735/1 "2022-06-14T13:34:56Z")

</div>

Hi,

I’m trying to append new key-value pair in the existing yml (**subcluster\_to\_subnet\_map.yml)** file using combine. New values are getting appended correctly but it’s getting added without spaces.

I need help with adding 2 spaces in starting so that the indentation is maintained. Please suggest how this can be achieved. (I don’t want to use lineinfile here)

Yaml file on which key-value pair should be added: **subcluster\_to\_subnet\_map.yml**

**subcluster\_to\_subnet\_map:**  
**ee: ee-ci9-sc8**  
**test: test-ci1-sc1**

**\<\<new value should have 2 spaces\>\>**

I’m using below code for this -

- name: append new subnet and subcluster values  
set\_fact:  
subcluster\_to\_subnet\_map: “{{ subcluster\_to\_subnet\_map | default({}) | combine ({input\_subcluster: input\_subnet}) }}”

- debug:  
var: subcluster\_to\_subnet\_map

- name: write var to file  
copy:  
content: “{{ subcluster\_to\_subnet\_map | to\_nice\_yaml }}”  
dest: environment/subcluster\_to\_subnet\_map.yml

The output I’m getting now is -

subcluster\_to\_subnet\_map:  
ee: ee-ci9-sc8  
test: test-ci1-sc1  
**test1: infra-nodes**

---

<div class="post-metadata">

**Author:** ![Shivani\_Arora](https://avatars.discourse-cdn.com/v4/letter/s/919ad9/32.png) [@Shivani\_Arora](https://forum.ansible.com/u/Shivani_Arora)\
**Post date:** [June 14, 2022, 5:05pm UTC](https://forum.ansible.com/t/append-new-key-value-pair-with-correct-indentation-in-yml-file/36735/2 "2022-06-14T17:05:39Z")

</div>

Hi Team,

Looking forward to any input.

Thanks

---

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [June 14, 2022, 5:35pm UTC](https://forum.ansible.com/t/append-new-key-value-pair-with-correct-indentation-in-yml-file/36735/3 "2022-06-14T17:35:19Z")

</div>

Where are these variables defined?

subcluster\_to\_subnet\_map  
input\_subcluster  
input\_subnet

Please share the complete playbook and variables.

---

<div class="post-metadata">

**Author:** ![Shivani\_Arora](https://avatars.discourse-cdn.com/v4/letter/s/919ad9/32.png) [@Shivani\_Arora](https://forum.ansible.com/u/Shivani_Arora)\
**Post date:** [June 15, 2022, 5:10am UTC](https://forum.ansible.com/t/append-new-key-value-pair-with-correct-indentation-in-yml-file/36735/4 "2022-06-15T05:10:16Z")

</div>

I’m taking user input for variables: **input\_subcluster, input\_subnet** and **subcluster\_to\_subnet\_map** is defined in subcluster\_to\_subnet\_map.yml file.

Here is my code -

- hosts: localhost  
connection: local  
gather\_facts: True

---

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [June 15, 2022, 10:38am UTC](https://forum.ansible.com/t/append-new-key-value-pair-with-correct-indentation-in-yml-file/36735/5 "2022-06-15T10:38:21Z")

</div>

> I'm taking user input for variables: \*input\_subcluster, input\_subnet \*and \*subcluster\_to\_subnet\_map\* is defined in subcluster\_to\_subnet\_map.yml file.
> 
> Here is my code -
> 
> - hosts: localhost  
> connection: local  
> gather\_facts: True
> 
> #  
> ## Taking inputs for subcluster and subnet  
> #  
> vars\_prompt:  
> - name: "input\_subcluster"  
> prompt: "Enter subcluster name"  
> private: no
> 
> - name: "input\_subnet"  
> prompt: "Enter subnet name"  
> private: no
> 
> - include\_vars:  
> file: environment/Dev/subcluster\_to\_subnet\_map.yml  
> name: subcluster\_to\_subnet\_map
> 
> - name: append new subnet and subcluster values  
> set\_fact:  
> subcluster\_to\_subnet\_map: "{{ subcluster\_to\_subnet\_map | default({}) | combine ({input\_subcluster: input\_subnet}) }}"
> 
> ```
> \- debug:
> var: subcluster\_to\_subnet\_map
> 
> \- name: write var to file
> copy:
> content: &quot;\{\{ subcluster\_to\_subnet\_map | to\_nice\_yaml \}\}&quot;
> 
> ```

This means the content of subcluster\_to\_subnet\_map variable, which does not include its own name. You should explicitly write that out.  
Try this:

content: "{{ {'subcluster\_to\_subnet\_map': subcluster\_to\_subnet\_map} | to\_nice\_yaml }}"

---

<div class="post-metadata">

**Author:** ![Shivani\_Arora](https://avatars.discourse-cdn.com/v4/letter/s/919ad9/32.png) [@Shivani\_Arora](https://forum.ansible.com/u/Shivani_Arora)\
**Post date:** [June 15, 2022, 11:04am UTC](https://forum.ansible.com/t/append-new-key-value-pair-with-correct-indentation-in-yml-file/36735/6 "2022-06-15T11:04:47Z")

</div>

It gave the below output:

subcluster\_to\_subnet\_map:  
subcluster\_to\_subnet\_map:  
ee: ee-ci9-sc8  
test: test-ci1-sc1  
test1: infra-nodes

**The expected output is:**

subcluster\_to\_subnet\_map:

ee: ee-ci9-sc8  
test: test-ci1-sc1  
test1: infra-nodes

---

<div class="post-metadata">

**Author:** ![Shivani\_Arora](https://avatars.discourse-cdn.com/v4/letter/s/919ad9/32.png) [@Shivani\_Arora](https://forum.ansible.com/u/Shivani_Arora)\
**Post date:** [June 17, 2022, 3:25am UTC](https://forum.ansible.com/t/append-new-key-value-pair-with-correct-indentation-in-yml-file/36735/7 "2022-06-17T03:25:25Z")

</div>

Hi All,

Any help would be appreciated here.

Thanks
