Dylan_
Dylan_

Reputation: 53

Multiple times adding same line into yaml file (ansible playbook)

I want to add one line multiple times in a specific between lines. I tried with my playbook but just once added to the last one and it should add same line with From Earth.

My playbook

- name: Update the file
  lineinfile:
    dest: /sample/config.file
    insertafter: ' Earth .* '
    line: '    This is template'

My initial file

Hello World
   [From Earth]
Hello World
   [From Earth]
Hello World
   [From Earth]
Hello World
   [From Earth]

My Desire Output

Hello World
   [From Earth]
   This is template
Hello World
   [From Earth]
   This is template
Hello World
   [From Earth]
   This is template
Hello World
   [From Earth]
   This is template

Upvotes: 0

Views: 909

Answers (1)

Jack
Jack

Reputation: 6168

Try replace:

 - name: Replace
   replace:
       path: file.txt
       regexp: 'Earth]\n(?!{{repl_str}})'
       replace: 'Earth]\n{{repl_str}}'
   vars:
     repl_str: '   This is template\n'

Upvotes: 1

Related Questions