We have a file on a managed node and we want to change the line within the file. For example on the remote box this file structure exists.

cd /etc/ansible/playbooks
sudo nano testfile.txt
1.random1
2.random2
3.random3
4.random5
5.random6

So we will create a playbook to modify line 4 which is 4.random5 to changethisline ok.

sudo nano ChangeLineInFile.yml
---
- hosts: webservers
  become: true
  tasks:
  - name: change line in file
    lineinfile:
      path: /etc/ansible/playbooks/testfile.txt
      regexp: '^4.random5'
      line: changethisline ok
    when: ansible_distribution == 'Ubuntu'
ansible-playbook --ask-become-pass ChangeLineInFile.yml

screen shots below

By Kad

Leave a Reply

Your email address will not be published. Required fields are marked *