0

I am using lineinfile to add multiple host details in a file but some details were the same only different in hosts. I am looking for a way to prevent lineinfile to skip same line. Is that possible ?

grace
  • 1
  • 1
  • Welcome to SO. Maybe a better title woud be something like "How to prevent lineinfile to skip same lines" - And if possible, give some more details. – Andy A. Aug 30 '21 at 07:50

1 Answers1

0

If you are looking to validate if lines are present before adding, maybe this would work. Regex is the string to search and state is set to absent to remove if the line is already present.

      ---
        - hosts: localhost
          tasks:
            - name: Check if it is the file
              become: yes
              tags: example
              lineinfile:
                path: /path/to/file
                regexp: "Hello World" 
                state: absent
                backup: yes