remove block of text from config file using ansibl

2019-08-17 09:10发布

问题:

I am trying to remove the below section from samba config file smb.conf.

[public]
  path = /opt/samba/public
  guest ok = yes
  browsable = yes
  writable = yes
  read only = no

Blockinfile module won't work as there are no markers . Lineinfile will also have a problem as there are lines which are common to other sections. e.g

 browsable = yes
 writable = yes

How do I remove these lines using ansible?

PS: replacing the config file with a new one is not possible as each server has a unique user mapped to it (not ideal when running batch jobs)

回答1:

You can use replace module:

- replace:
    path: /etc/smb.conf
    regexp: '^\[public\][^[]+'
    replace: ''
    backup: yes

This should remove everything between [public] and [ or EOF.



回答2:

Ansible has native ini-file support, which is a much cleaner way to accomplish this.

    - name: remove public block
      ini_file:
        path: /etc/smb.conf
        section: public
        state: absent


标签: ansible