{"id":1585,"date":"2024-07-06T05:43:59","date_gmt":"2024-07-06T05:43:59","guid":{"rendered":"http:\/\/shijuvarghese.com\/?p=1585"},"modified":"2025-05-23T18:09:34","modified_gmt":"2025-05-23T18:09:34","slug":"ansible-the-copy-module","status":"publish","type":"post","link":"http:\/\/shijuvarghese.com\/?p=1585","title":{"rendered":"Ansible: The copy module"},"content":{"rendered":"<p>The module\u00a0<em>ansible.builtin.copy<\/em> enables users to copy files from the controller node to managed nodes. It also helps to add\/modify a destination file with a specific <em>content.<\/em><\/p>\n<p>In the below example we use three groups in inventory file as below:<\/p>\n<ul>\n<li>[testGRP]<br \/>\n192.168.48.129<br \/>\n192.168.48.132<\/li>\n<\/ul>\n<ul>\n<li>[dev]<br \/>\n192.168.48.132<\/li>\n<\/ul>\n<ul>\n<li>[test]<br \/>\n192.168.48.129<\/li>\n<\/ul>\n<p>The playbook uses to\u00a0<em>copy<\/em> module to replace the content in\u00a0<em><strong>\/etc\/issues<\/strong><\/em> file in the managed nodes with content based on the inventory group the specific managed node belongs, and also copies a file name <em><strong>sample.txt<\/strong><\/em> from the controller node to the managed nodes based on conditions.<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> cat sample.txt<br \/>\n<em>Hello SAMPLE<\/em><\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> ssh 192.168.48.132 &#8216;cat \/etc\/issues&#8217;<br \/>\n<em>Hi<\/em><\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> ssh 192.168.48.129 &#8216;cat \/etc\/issues&#8217;<br \/>\n<em>Hi<\/em><\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> ssh 192.168.48.132 &#8216;cat \/root\/sample.txt&#8217;<br \/>\n<em>cat: \/root\/sample.txt: No such file or directory<\/em><\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> ssh 192.168.48.129 &#8216;cat \/root\/sample.txt&#8217;<br \/>\n<em>cat: \/root\/sample.txt: No such file or directory<\/em><\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> cat modify.yml<\/p>\n<p>======== ===<br \/>\n<em>&#8211; &#8211; &#8211;<\/em><br \/>\n<em>&#8211; name: Playbook to modify content of \/etc\/issues<\/em><br \/>\n<em>\u00a0 hosts: testGRP<\/em><br \/>\n<em> \u00a0\u00a0tasks:<\/em><br \/>\n<em> \u00a0\u00a0\u00a0\u00a0&#8211; name: Replace the content in dev hosts<\/em><br \/>\n<em> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ansible.builtin.copy:<\/em><br \/>\n<em> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0content: &#8220;Development\\n&#8221;<\/em><br \/>\n<em> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0dest: \/etc\/issues<\/em><br \/>\n<em> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0when:<\/em><br \/>\n<em> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0inventory_hostname is in groups[&#8216;dev&#8217;]<\/em><\/p>\n<p><em>\u00a0\u00a0\u00a0\u00a0&#8211; name: Replace the content in test hosts<\/em><br \/>\n<em> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ansible.builtin.copy:<\/em><br \/>\n<em> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0content: &#8220;Test\\n&#8221;<\/em><br \/>\n<em> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0dest: \/etc\/issues<\/em><br \/>\n<em> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0when:<\/em><br \/>\n<em> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0inventory_hostname in groups[&#8216;test&#8217;]<\/em><\/p>\n<p><em>\u00a0\u00a0\u00a0\u00a0&#8211; name: Copy a file to nodes in groups other than in TEST<\/em><br \/>\n<em> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ansible.builtin.copy:<\/em><br \/>\n<em> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0src: sample.txt<\/em><br \/>\n<em> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0dest: \/root\/sample.txt<\/em><br \/>\n<em> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0owner: root<\/em><br \/>\n<em> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0group: apache<\/em><br \/>\n<em> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0mode: 0775<\/em><br \/>\n<em> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0when:<\/em><br \/>\n<em> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0inventory_hostname <span style=\"color: #ff6600;\">not in<\/span> groups[&#8216;test&#8217;]<\/em><\/p>\n<p>==== ===<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> ansible-navigator run -m stdout modify.yml<\/p>\n<p><em>PLAY [Playbook to modify content of \/etc\/issues] **************************************************************************************<\/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 [Replace the content in dev hosts] ***********************************************************************************************<\/em><br \/>\n<em>skipping: [192.168.48.129]<\/em><br \/>\n<em>changed: [192.168.48.132]<\/em><\/p>\n<p><em>TASK [Replace the content in test hosts] **********************************************************************************************<\/em><br \/>\n<em>skipping: [192.168.48.132]<\/em><br \/>\n<em>changed: [192.168.48.129]<\/em><\/p>\n<p><em>TASK [Copy a file to nodes in groups other than in TEST] ******************************************************************************<\/em><br \/>\n<em>skipping: [192.168.48.129]<\/em><br \/>\n<em>changed: [192.168.48.132]<\/em><\/p>\n<p><em>PLAY RECAP ****************************************************************************************************************************<\/em><br \/>\n<em>192.168.48.129 : ok=2 changed=1 unreachable=0 failed=0 skipped=2 rescued=0 ignored=0<\/em><br \/>\n<em>192.168.48.132 : ok=3 changed=2 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0<\/em><\/p>\n<p>==== ====<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> ssh 192.168.48.129 &#8216;cat \/root\/sample.txt&#8217;<br \/>\n<em>cat: \/root\/sample.txt: No such file or directory<\/em><\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> ssh 192.168.48.132 &#8216;cat \/root\/sample.txt&#8217;<br \/>\n<em>Hello SAMPLE<\/em><\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> ssh 192.168.48.129 &#8216;cat \/etc\/issues&#8217;<br \/>\n<em>Test<\/em><\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> ssh 192.168.48.132 &#8216;cat \/etc\/issues&#8217;<br \/>\n<em>Development<\/em><\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> ssh 192.168.48.132 &#8216;ls -l \/root\/sample.txt&#8217;<br \/>\n<em>-rwxrwxr-x. 1 root <span style=\"color: #ff6600;\">apache<\/span> 13 Jul 6 02:06 \/root\/sample.txt<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>The module\u00a0ansible.builtin.copy enables users to copy files from the controller node to managed nodes. It also helps to add\/modify a destination file with a specific <a class=\"mh-excerpt-more\" href=\"http:\/\/shijuvarghese.com\/?p=1585\" title=\"Ansible: The copy module\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":1792,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32,21,3],"tags":[],"class_list":["post-1585","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ansible","category-devops","category-linux"],"_links":{"self":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/1585","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=1585"}],"version-history":[{"count":3,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/1585\/revisions"}],"predecessor-version":[{"id":1592,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/1585\/revisions\/1592"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/media\/1792"}],"wp:attachment":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1585"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1585"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1585"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}