Peter George
Peter George

Reputation: 29

How to remove a line from a file in ansible?

I have a file, It has a line

DocumentRoot path/to/folder

I have to remove this line, What should be the regular expression. ?

Upvotes: 1

Views: 13561

Answers (2)

Valeriy Solovyov
Valeriy Solovyov

Reputation: 5648

You can do with http://docs.ansible.com/ansible/lineinfile_module.html:

- lineinfile: 
    dest: /etc/apache2/sites-enabled/000-default 
    state: absent 
    regexp: "^DocumentRoot"

or http://docs.ansible.com/ansible/replace_module.html:

- replace: 
    dest: /etc/apache2/sites-enabled/000-default
    regexp: "^DocumentRoot"    
    regexp: '^(DocumentRoot)\s+[^\n]*\n$' 
    replace: '#\1'  
    validate: '/usr/sbin/apache2ctl -f %s -t'

PS:

But I didn't check if regexp's were right =) Also you can use template module: http://docs.ansible.com/ansible/template_module.html or template and assemble module: http://docs.ansible.com/ansible/assemble_module.html

Upvotes: 6

Willem
Willem

Reputation: 3253

http://docs.ansible.com/ansible/lineinfile_module.html

Regex: "^DocumentRoot path/to/folder$"

Upvotes: 3

Related Questions