Mark Caudill
Mark Caudill

Reputation: 37

How can I use Ansible lineinfile to remove all but a few specific lines?

I'm attempting to ensure all but a few specific lines are removed from a file. I can get halfway there with the following task.

- name: ensure only the correct lines are present
  lineinfile: dest=/path/to/file
    regexp="pattern1|pattern2[0-9]*|pattern3[0-9]*"
    state=present
    backup=yes

Ultimately I want to ensure that pattern1, pattern2[0-9]*, and pattern3[0-9]* are the only lines that remain in this file. I've attempted to come up with a regex that negates this one and then specify state=absent but I've been unsuccessful so far.

Upvotes: 0

Views: 1352

Answers (1)

Mxx
Mxx

Reputation: 9354

If you want only specific lines in your file, why don't you just transfer that file with your desired lines to the remote server? You can use copy module to transfer that file as is or template module to dynamically substitute some variables inside or even assemble module to generate a file from some fragments(such as config).

Upvotes: 1

Related Questions