Append new key value pair with correct indentation in yml file

630 views
Skip to first unread message

Shivani Arora

unread,
Jun 14, 2022, 9:34:56 AM6/14/22
to Ansible Project
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

Shivani Arora

unread,
Jun 14, 2022, 1:05:39 PM6/14/22
to Ansible Project
Hi Team,

Looking forward to any input.

Thanks

Dick Visser

unread,
Jun 14, 2022, 1:35:54 PM6/14/22
to ansible...@googlegroups.com
Where are these variables defined?

subcluster_to_subnet_map
input_subcluster
input_subnet

Please share the complete playbook and variables.
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/80ee5278-ada8-498f-a412-0887e7d79717n%40googlegroups.com.

Shivani Arora

unread,
Jun 15, 2022, 1:10:16 AM6/15/22
to Ansible Project
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: "{{ subcluster_to_subnet_map | to_nice_yaml }}"
          dest: environment/Dev/subcluster_to_subnet_map.yml


=============================================

Location and content of subcluster_to_subnet_map.yml file:  environment/Dev/subcluster_to_subnet_map.yml

subcluster_to_subnet_map:
    ee: ee-ci9-sc8
    test: test-ci1-sc1

    <<new value should have 2 spaces>>

Dick Visser

unread,
Jun 15, 2022, 6:38:38 AM6/15/22
to ansible...@googlegroups.com


On 2022-06-15 (Wed) 07:10, Shivani Arora wrote:
> I'm taking user input for variables: *input_subcluster, input_subnet
> *and *subcluster_to_subnet_map* is defined in
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 }}"


>           dest: environment/Dev/subcluster_to_subnet_map.yml
>
>
> =============================================
>
> Location and content of subcluster_to_subnet_map.yml file:
> environment/Dev/subcluster_to_subnet_map.yml
>
> *subcluster_to_subnet_map:
>     ee: ee-ci9-sc8
>     test: test-ci1-sc1*
> *    <<new value should have 2 spaces>>*
> <https://groups.google.com/d/msgid/ansible-project/80ee5278-ada8-498f-a412-0887e7d79717n%40googlegroups.com>.
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to ansible-proje...@googlegroups.com
> <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/807a1167-c6bf-4d89-b964-9cdebbb905d1n%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/807a1167-c6bf-4d89-b964-9cdebbb905d1n%40googlegroups.com?utm_medium=email&utm_source=footer>.
OpenPGP_0x266713D4E6EF488D.asc
OpenPGP_signature

Shivani Arora

unread,
Jun 15, 2022, 7:04:47 AM6/15/22
to Ansible Project
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

Shivani Arora

unread,
Jun 16, 2022, 11:25:25 PM6/16/22
to Ansible Project
Hi All,

Any help would be appreciated here.

Thanks

Reply all
Reply to author
Forward
0 new messages