Ansible: Adding a line to a file. At the end of file, and replacement of line

Ansible_Logo

Ansible allows admins to add lines to files. The below playbook adds a sudoers configuration file, and adds an entry to allow users of group “webadmin” sudo to all users and execute all commands as root.

It creates a file “/etc/sudoers.d/webadmin” and adds an entry to it. The validate parameter specifies the command to run to verify that the file is correct.

[root@centos9vm ~]# cat addaline.yml

====
–  – –
– name: Add a line in sudoers file
    hosts: testGRP
    tasks:
        – name: create a sudoers config file and add a line
            ansible.builtin.lineinfile:
                path: /etc/sudoers.d/webadmin
                state: present
                create: yes
                mode: 0440
                line: “%webadmin ALL=(ALL) NOPASSWD :ALL \n#Helo\n####\n###aaa”
                validate: /usr/sbin/visudo -cf %s

====

[root@centos9vm ~]# ansible-navigator run -m stdout addaline.yml

=====

PLAY [Add a line in sudoers file] **********************************************

TASK [Gathering Facts] *********************************************************
ok: [192.168.48.129]
ok: [192.168.48.132]

TASK [create a sudoers config file and add a line] *****************************
ok: [192.168.48.129]
ok: [192.168.48.132]

PLAY RECAP *********************************************************************
192.168.48.129 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.48.132 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

======

Now let us check from the node centos9test1

[user1@centos9test1 ~]$ echo “Hello” > stemp.txt

[user1@centos9test1 ~]$ echo “Hawdi” >> stemp.txt
[user1@centos9test1 ~]$ sudo cp stemp.txt /root/
[user1@centos9test1 ~]$ cat /root/stemp.txt
cat: /root/stemp.txt: Permission denied
[user1@centos9test1 ~]$ sudo cat /root/stemp.txt

===== ====
Hello
Hawdi
===== ====

Now let us write a playbook to use the above stemp.txt file that was created and replace the line “Hello” with “goooogle”

[root@centos9vm ~]# cat replace.yml

==== ==
– – –
– name: Play book to replace a line in a file
    hosts: 192.168.48.132
    tasks:
        – name: Task to replace the line
            ansible.builtin.lineinfile:
                path: /root/stemp.txt
                regexp: ‘^Hello’
                line: Goooogle

==== ==

Now let us run the playbook

[root@centos9vm ~]# ansible-navigator run -m stdout replace.yml

==== ====

PLAY [Play book to replace a line in a file] ***********************************

TASK [Gathering Facts] *********************************************************
ok: [192.168.48.132]

TASK [Task to replace the line] ************************************************
changed: [192.168.48.132]

PLAY RECAP *********************************************************************
192.168.48.132 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

==== ===

Verify the outcome by reading the file in 192.168.48.132

[user1@centos9test1 ~]$ sudo cat /root/stemp.txt

===== ==
Goooogle
Hawdi

===== ==