{"id":1340,"date":"2024-05-26T15:53:10","date_gmt":"2024-05-26T15:53:10","guid":{"rendered":"http:\/\/shijuvarghese.com\/?p=1340"},"modified":"2024-06-11T23:51:26","modified_gmt":"2024-06-11T23:51:26","slug":"ansible-adding-a-line-to-a-file","status":"publish","type":"post","link":"http:\/\/shijuvarghese.com\/?p=1340","title":{"rendered":"Ansible: Adding a line to a file. At the end of file, and replacement of line"},"content":{"rendered":"<p>Ansible allows admins to add lines to files.\u00a0The below playbook adds a sudoers configuration file, and adds an entry to allow users of group &#8220;webadmin&#8221; sudo to all users and execute all commands as root.<\/p>\n<p>It creates a file &#8220;\/etc\/sudoers.d\/webadmin&#8221; and adds an entry to it. The <em>validate<\/em> parameter specifies the command to run to verify that the file is correct.<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> cat addaline.yml<\/p>\n<p>====<br \/>\n<em>&#8211;\u00a0 &#8211; &#8211;<\/em><br \/>\n<em>&#8211; name: Add a line in sudoers file<\/em><br \/>\n<em> &nbsp; &nbsp; hosts: testGRP<\/em><br \/>\n<em> &nbsp; &nbsp; tasks:<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &#8211; name: create a sudoers config file and add a line<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ansible.builtin.lineinfile:<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; path: \/etc\/sudoers.d\/webadmin<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; state: present<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; create: yes<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mode: 0440<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; line: &#8220;%webadmin ALL=(ALL) NOPASSWD :ALL \\n#Helo\\n####\\n###aaa&#8221;<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; validate: \/usr\/sbin\/visudo -cf %s<\/em><\/p>\n<p>====<\/p>\n<p>[root@centos9vm ~]# ansible-navigator run -m stdout addaline.yml<\/p>\n<p>=====<\/p>\n<p><em>PLAY [Add a line in sudoers file] **********************************************<\/em><\/p>\n<p><em>TASK [Gathering Facts] *********************************************************<\/em><br \/>\n<em>ok: [192.168.48.129]<\/em><br \/>\n<em>ok: [192.168.48.132]<\/em><\/p>\n<p><em>TASK [create a sudoers config file and add a line] *****************************<\/em><br \/>\n<em>ok: [192.168.48.129]<\/em><br \/>\n<em>ok: [192.168.48.132]<\/em><\/p>\n<p><em>PLAY RECAP *********************************************************************<\/em><br \/>\n<em>192.168.48.129 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0<\/em><br \/>\n<em>192.168.48.132 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0<\/em><\/p>\n<p>======<\/p>\n<p>Now let us check from the node\u00a0centos9test1<\/p>\n<p><strong>[user1@centos9test1 ~]$<\/strong> echo &#8220;Hello&#8221; &gt; stemp.txt<\/p>\n<p><strong>[user1@centos9test1 ~]$<\/strong>\u00a0echo &#8220;Hawdi&#8221; &gt;&gt; stemp.txt<br \/>\n<strong>[user1@centos9test1 ~]$<\/strong> <span style=\"color: #ff0000;\">sudo<\/span> cp stemp.txt \/root\/<br \/>\n<strong>[user1@centos9test1 ~]$<\/strong> cat \/root\/stemp.txt<br \/>\ncat: \/root\/stemp.txt: Permission denied<br \/>\n<strong>[user1@centos9test1 ~]$<\/strong> <span style=\"color: #ff0000;\">sudo<\/span> cat \/root\/stemp.txt<\/p>\n<p><strong>===== ====<\/strong><br \/>\n<em>Hello<\/em><br \/>\n<em> Hawdi<\/em><br \/>\n<strong>===== ====<\/strong><\/p>\n<p>Now let us write a playbook to use the above stemp.txt file that was created and replace the line &#8220;Hello&#8221; with &#8220;goooogle&#8221;<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> cat replace.yml<\/p>\n<p>==== ==<br \/>\n<em>&#8211; &#8211; &#8211;<\/em><br \/>\n<em>&#8211; name: Play book to replace a line in a file<\/em><br \/>\n<em> &nbsp; &nbsp; hosts: 192.168.48.132<\/em><br \/>\n<em> &nbsp; &nbsp; tasks:<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &#8211; name: Task to replace the line<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ansible.builtin.lineinfile:<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; path: \/root\/stemp.txt<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; regexp: &#8216;^Hello&#8217;<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; line: Goooogle<\/em><\/p>\n<p>==== ==<\/p>\n<p>Now let us run the playbook<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> ansible-navigator run -m stdout replace.yml<\/p>\n<p>==== ====<\/p>\n<p><em>PLAY [Play book to replace a line in a file] ***********************************<\/em><\/p>\n<p><em>TASK [Gathering Facts] *********************************************************<\/em><br \/>\n<em>ok: [192.168.48.132]<\/em><\/p>\n<p><em>TASK [Task to replace the line] ************************************************<\/em><br \/>\n<em>changed: [192.168.48.132]<\/em><\/p>\n<p><em>PLAY RECAP *********************************************************************<\/em><br \/>\n<em>192.168.48.132 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0<\/em><\/p>\n<p>==== ===<\/p>\n<p>Verify the outcome by reading the file in\u00a0192.168.48.132<\/p>\n<p><strong>[user1@centos9test1 ~]$<\/strong> sudo cat \/root\/stemp.txt<\/p>\n<p>===== ==<br \/>\n<em>Goooogle<\/em><br \/>\n<em>Hawdi<\/em><\/p>\n<p>===== ==<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>Ansible allows admins to add lines to files.\u00a0The below playbook adds a sudoers configuration file, and adds an entry to allow users of group &#8220;webadmin&#8221; <a class=\"mh-excerpt-more\" href=\"http:\/\/shijuvarghese.com\/?p=1340\" title=\"Ansible: Adding a line to a file. At the end of file, and replacement of line\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32,21,3],"tags":[],"class_list":["post-1340","post","type-post","status-publish","format-standard","hentry","category-ansible","category-devops","category-linux"],"_links":{"self":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/1340","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1340"}],"version-history":[{"count":18,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/1340\/revisions"}],"predecessor-version":[{"id":1359,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/1340\/revisions\/1359"}],"wp:attachment":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1340"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1340"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1340"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}