#ansible (2021-10)

ansible

Discussions related to ansible configuration management

2021-10-24

Ozzy Aluyi avatar
Ozzy Aluyi

Hi All, I am trying to comment out a line in an SNMP conf. file using ansible. The line has some whitespaces. How can I use ansible to comment out this line. I have tried using the replace command, no joy!

Ozzy Aluyi avatar
Ozzy Aluyi
- name: Comment out agent address.
    replace:    
      path: /etc/snmp/snmpd.conf
      regexp: '^(.*agentaddress  127.0.0.1,[::1].*)'
      replace: '#agentaddress  127.0.0.1,[::1]'
Zoltan K avatar
Zoltan K

hi, in your regexp you can refer your bracketed matched pattern as \1 see here as  well https://www.mydailytutorials.com/uncommentcomment-lines-files-using-ansible/ also your pattern looks to be faulty check examples in docs here//docs.ansible.com/ansible/latest/collections/ansible/builtin/replace_module.html> I would replace spaces with \s+ also if this is a full line match I would add $ at the end… cheers.

Ozzy Aluyi avatar
Ozzy Aluyi

Great thanks @Zoltan K . That worked

1

2021-10-31

    keyboard_arrow_up