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 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 }}"

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>>*

On Tuesday, June 14, 2022 at 11:05:54 PM UTC+5:30 [email protected] wrote:

    Where are these variables defined?

    subcluster_to_subnet_map
    input_subcluster
    input_subnet

    Please share the complete playbook and variables.



    On Tue, 14 Jun 2022 at 15:35, Shivani Arora <[email protected]> wrote:
     >
     > 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
     >
     > --
     > 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 [email protected].
     > To view this discussion on the web visit
    
https://groups.google.com/d/msgid/ansible-project/80ee5278-ada8-498f-a412-0887e7d79717n%40googlegroups.com
    
<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 [email protected] <mailto:[email protected]>. 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>.

--
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/dc5f0119-4a70-91d3-e361-d1c258e2405f%40gmail.com.

Attachment: OpenPGP_0x266713D4E6EF488D.asc
Description: OpenPGP public key

Attachment: OpenPGP_signature
Description: OpenPGP digital signature

Reply via email to