1

I want to add the line "*{My-Dept$2}" in file.yaml after *{My-Dept$1} this on every finding, I was able to find a line that starts with *{My-Dept$1} and inserted only one time, and ended with this trouble.

I have this in Multiple lines in the file and wanted to insert after "*{My-Dept$1}".

The reference are below:

        Infrastructure:
                - *{My-Dept$0}
                - *{My-Dept$1}
        Applications:
                - *{My-Dept$1}
                - *{My-Dept$3}
                - *{My-Dept$4}
        Database:
                - *{My-Dept$0}
                - *{My-Dept$1}
                - *{My-Dept$3}

With the below code i was able to add the line "- *{My-Dept$2}" after only in the last "Database arry " entry.

  - name: Add missing Dept
    lineinfile:
       dest: ./file.yaml
       insertafter: '[*{]My-Dept[$]1[}]' #
       line: '                    - *{My-Dept$2}'

and the Output:

    Infrastructure:
            - *{My-Dept$0}
            - *{My-Dept$1}
                Applications:
                    - *{My-Dept$1}
                    - *{My-Dept$3}
                    - *{My-Dept$4}
                    Database:
                            - *{My-Dept$0}
                            - *{My-Dept$1}
                            - *{My-Dept$2}
                            - *{My-Dept$3}

I wish to get "*{My-Dept$2}" on every next line of each of the findings of *{My-Dept$1} entries. Most of the search find talks about replace, but my requirement is to insert multiple times on each finding.

ravinayag
  • 33
  • 4

1 Answers1

0

Make the file.yaml valid YAML. For example

shell> cat file.yaml
---
Infrastructure:
 - '*{My-Dept$0}'
 - '*{My-Dept$1}'
Applications:
 - '*{My-Dept$1}'
 - '*{My-Dept$3}'
 - '*{My-Dept$4}'
Database:
 - '*{My-Dept$0}'
 - '*{My-Dept$1}'
 - '*{My-Dept$3}'

Q: "Put *{My-Dept$2} on every next line of each of the findings of *{My-Dept$1} entries."

A: It's possible to edit the file on your own.

  1. Read the variables from the file into a dictionary. For examle
    - include_vars:
        file: file.yaml
        name: my_dict
    - debug:
        var: my_dict

gives

  my_dict:
    Applications:
    - '*{My-Dept$1}'
    - '*{My-Dept$3}'
    - '*{My-Dept$4}'
    Database:
    - '*{My-Dept$0}'
    - '*{My-Dept$1}'
    - '*{My-Dept$3}'
    Infrastructure:
    - '*{My-Dept$0}'
    - '*{My-Dept$1}'
  1. Modify the data. For example
    - set_fact:
        my_dict1: "{{ my_dict1|default({})|
                      combine({item: (my_dict[item] + add_items)|unique|sort}) }}"
      loop: "{{ my_dict.keys()|list }}"
      vars:
        add_items:
          - '*{My-Dept$2}'
    - debug:
        var: my_dict1

gives

  my_dict1:
    Applications:
    - '*{My-Dept$1}'
    - '*{My-Dept$2}'
    - '*{My-Dept$3}'
    - '*{My-Dept$4}'
    Database:
    - '*{My-Dept$0}'
    - '*{My-Dept$1}'
    - '*{My-Dept$2}'
    - '*{My-Dept$3}'
    Infrastructure:
    - '*{My-Dept$0}'
    - '*{My-Dept$1}'
    - '*{My-Dept$2}'
  1. Create template
shelll> cat file.yaml.j2
---
{% for k,v in my_dict1.items() %}
{{ k }}:
{% for item in v %}
  - '{{ item }}'
{% endfor %}
{% endfor %}
  1. Write the modified data into the file
    - template:
        src: file.yaml.j2
        dest: file.yaml

gives

shell> cat file.yaml
---
Infrastructure:
  - '*{My-Dept$0}'
  - '*{My-Dept$1}'
  - '*{My-Dept$2}'
Applications:
  - '*{My-Dept$1}'
  - '*{My-Dept$2}'
  - '*{My-Dept$3}'
  - '*{My-Dept$4}'
Database:
  - '*{My-Dept$0}'
  - '*{My-Dept$1}'
  - '*{My-Dept$2}'
  - '*{My-Dept$3}'
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63
  • Great explanation, I'm having issues to amend this due to the alignment of file.yaml file. Looks the alignment should be intact as original what I posted. However I keep post update, Could you explain about this file "file.yaml.j2" – ravinayag Sep 22 '20 at 12:22
  • See details about the templates in the documentation [Jinja](https://jinja.palletsprojects.com/en/2.11.x/). There shouldn't be any issues if file.yaml is valid YAML. – Vladimir Botka Sep 26 '20 at 16:47
  • got it, i tried with that now works well, many thanks – ravinayag Oct 10 '20 at 10:07