{"id":1573,"date":"2024-07-05T17:27:48","date_gmt":"2024-07-05T17:27:48","guid":{"rendered":"http:\/\/shijuvarghese.com\/?p=1573"},"modified":"2025-05-23T18:10:24","modified_gmt":"2025-05-23T18:10:24","slug":"ansible-download-a-fine-from-website-and-replace-value","status":"publish","type":"post","link":"http:\/\/shijuvarghese.com\/?p=1573","title":{"rendered":"Ansible: Download a file from website and replace value"},"content":{"rendered":"<p>In this post we are going to see how we can use the below two modules to download a file from a URL or and FTP server to the managed nodes, and replace certain content of that file.<\/p>\n<ul>\n<li>ansible.builtin.get_url<\/li>\n<li>ansible.builtin.replace\n<ul>\n<li>This module will replace all instances of a pattern within a file<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>In the below example we use three nodes:<\/p>\n<ul>\n<li>centos9vm (192.168.48.128) &#8211; Controller node<\/li>\n<li>centosMYOBvm\u00a0(192.168.48.129) &#8211; Managed node &#8211; Part of dev group in inventory<\/li>\n<li>centos9test1\u00a0(192.168.48.132)\u00a0&#8211; Managed node\u00a0&#8211; Part of dev group\u00a0in inventory<\/li>\n<\/ul>\n<p>Note: Both the managed nodes have \/dev\/sda device, but do not have \/dev\/vdb<\/p>\n<p>Let us check the disk size in these nodes:<\/p>\n<p><strong>[root@centos9test1 ~]#<\/strong> fdisk -l | grep \/dev\/sda<br \/>\nDisk \/dev\/sda: 1 GiB, 1073741824 bytes, 2097152 sectors<\/p>\n<p><strong>[root@<span style=\"color: #ff9900;\">centosMYOBvm<\/span> ~]#<\/strong> fdisk -l | grep \/dev\/sda<br \/>\nDisk \/dev\/sda: 6 GiB, 6442450944 bytes, 12582912 sectors<\/p>\n<p>The below playbook downloads a sample template to\u00a0make a note of the sizes of the sda and vdb disks, and after that it populated the file with actual disk size. In case \/dev\/vdb is present, it records the actual value, but makes it as NONE if the disk is not present<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> wget http:\/\/192.168.48.132\/hardwareInfo.empty<\/p>\n<p><em>&#8211;2024-07-06 00:43:50&#8211; http:\/\/192.168.48.132\/hardwareInfo.empty<\/em><br \/>\n<em>Connecting to 192.168.48.132:80&#8230; connected.<\/em><br \/>\n<em>HTTP request sent, awaiting response&#8230; 200 OK<\/em><br \/>\n<em>Length: 42<\/em><br \/>\n<em>Saving to: \u2018hardwareInfo.empty\u2019<\/em><\/p>\n<p><em>hardwareInfo.empty 100%[===================&gt;] 42 &#8211;.-KB\/s in 0s<\/em><\/p>\n<p><em>2024-07-06 00:43:50 (4.60 MB\/s) &#8211; \u2018hardwareInfo.empty\u2019 saved [42\/42]<\/em><\/p>\n<p><em>=== ===\u00a0<\/em><\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> cat hardwareInfo.empty<\/p>\n<p><em>my_vda=disk_sda_size<\/em><br \/>\n<em>my_vdb=disk_vdb_size<\/em><\/p>\n<p>===== ==<\/p>\n<p>Let us look at the playbook created<\/p>\n<p>&nbsp;<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> cat hardware.yml<\/p>\n<p>====== ==<br \/>\n<em>&#8211; &#8211; &#8211;<\/em><br \/>\n<em>&#8211; name: Playbook to capture hardware info<\/em><br \/>\n<em>\u00a0 hosts: dev<\/em><br \/>\n<em> \u00a0\u00a0become: true<\/em><br \/>\n<em> \u00a0\u00a0<span style=\"color: #ff6600;\">ignore_errors: yes<\/span><\/em><br \/>\n<em> \u00a0\u00a0tasks:<\/em><br \/>\n<em>\u00a0\u00a0\u00a0\u00a0&#8211; name: Task to download the file from URL<\/em><br \/>\n<em> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ansible.builtin.get_url:<\/em><br \/>\n<em> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0url: http:\/\/192.168.48.132\/hardwareInfo.empty<\/em><br \/>\n<em> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0dest: \/root\/hardwareInfo.txt<\/em><br \/>\n<em> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0mode: 0755<\/em><\/p>\n<p><em>\u00a0\u00a0\u00a0\u00a0&#8211; name: Task to replace the \/dev\/sda_size<\/em><br \/>\n<em> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ansible.builtin.replace:<\/em><br \/>\n<em> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0path: \/root\/hardwareInfo.txt<\/em><br \/>\n<em> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0regexp: &#8220;disk_sda_size&#8221;<\/em><br \/>\n<em> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0replace: &#8220;{{ ansible_facts.devices.sda.size }}&#8221;<\/em><br \/>\n<em> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #ff6600;\">register:<\/span> theOutput1<\/em><\/p>\n<p><em>\u00a0\u00a0\u00a0\u00a0&#8211; name: Task to print the output of sda size<\/em><br \/>\n<em> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ansible.builtin.debug:<\/em><br \/>\n<em>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0var: theOutput1<\/em><\/p>\n<p><em>\u00a0 \u00a0 &#8211; name: Task to replace the \/dev\/vdb_size<\/em><br \/>\n<em> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ansible.builtin.replace:<\/em><br \/>\n<em> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0path: \/root\/hardwareInfo.txt<\/em><br \/>\n<em> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0regexp: &#8220;disk_vdb_size&#8221;<\/em><br \/>\n<em> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0replace: &#8220;{{ ansible_facts.devices.vdb.size }}&#8221;<\/em><br \/>\n<em> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0register: theOutput2<\/em><\/p>\n<p><em>\u00a0 \u00a0 &#8211; name: Task to print the output of vdb size<\/em><br \/>\n<em> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ansible.builtin.debug:<\/em><br \/>\n<em> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0var: theOutput2<\/em><\/p>\n<p><em>\u00a0\u00a0\u00a0\u00a0&#8211; name: Task to replace \/dev\/vdb_size value with NONE<\/em><br \/>\n<em> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ansible.builtin.replace:<\/em><br \/>\n<em> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0path: \/root\/hardwareInfo.txt<\/em><br \/>\n<em> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0regexp: &#8220;disk_vdb_size&#8221;<\/em><br \/>\n<em> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0replace: NONE<\/em><br \/>\n<em> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #ff6600;\">when:<\/span><\/em><br \/>\n<em> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0theOutput2.failed == true<\/em><\/p>\n<p>====== ===<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> ansible-navigator run -m stdout hardware.yml<\/p>\n<p>==== ===<\/p>\n<p><em>PLAY [Playbook to capture hardware info] **********************************************************************************************<\/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 [Task to download the file from URL] *********************************************************************************************<\/em><br \/>\n<em>ok: [192.168.48.132]<\/em><br \/>\n<em>ok: [192.168.48.129]<\/em><\/p>\n<p><em>TASK [Task to replace the \/dev\/sda_size] **********************************************************************************************<\/em><br \/>\n<em>ok: [192.168.48.129]<\/em><br \/>\n<em>ok: [192.168.48.132]<\/em><\/p>\n<p><em>TASK [Task to print the output of sda size] *******************************************************************************************<\/em><br \/>\n<em>ok: [192.168.48.132] =&gt; {<\/em><br \/>\n<em> &#8220;theOutput1&#8221;: {<\/em><br \/>\n<em> &#8220;changed&#8221;: false,<\/em><br \/>\n<em> &#8220;failed&#8221;: false,<\/em><br \/>\n<em> &#8220;msg&#8221;: &#8220;&#8221;,<\/em><br \/>\n<em> &#8220;rc&#8221;: 0<\/em><br \/>\n<em> }<\/em><br \/>\n<em>}<\/em><br \/>\n<em>ok: [192.168.48.129] =&gt; {<\/em><br \/>\n<em> &#8220;theOutput1&#8221;: {<\/em><br \/>\n<em> &#8220;changed&#8221;: false,<\/em><br \/>\n<em> &#8220;failed&#8221;: false,<\/em><br \/>\n<em> &#8220;msg&#8221;: &#8220;&#8221;,<\/em><br \/>\n<em> &#8220;rc&#8221;: 0<\/em><br \/>\n<em> }<\/em><br \/>\n<em>}<\/em><\/p>\n<p><em>TASK [Task to replace the \/dev\/vdb_size] **********************************************************************************************<\/em><br \/>\n<em>fatal: [192.168.48.132]: FAILED! =&gt; {&#8220;msg&#8221;: &#8220;The task includes an option with an undefined variable.\u00a0<\/em><\/p>\n<p>&#8230;&#8230;..<br \/>\n<em>fatal: [192.168.48.129]: FAILED! =&gt; {&#8220;msg&#8221;: &#8220;The task includes an option with an undefined variable.\u00a0<\/em><br \/>\n<em>&#8230;ignoring<\/em><\/p>\n<p><em>TASK [Task to print the output of vdb size] *******************************************************************************************<\/em><br \/>\n<em>ok: [192.168.48.132] =&gt; {<\/em><br \/>\n<em> &#8220;theOutput2&#8221;: {<\/em><br \/>\n<em> &#8220;failed&#8221;: true,<\/em><br \/>\n<em>&#8230;..<\/em><\/p>\n<p><em>TASK [Task to replace \/dev\/vdb_size value with NONE] **********************************************************************************<\/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=7 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=1<\/em><br \/>\n<em>192.168.48.132 : ok=7 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=1<\/em><\/p>\n<p>===== ====<\/p>\n<p>Now let us check the updated file in the managed nodes<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> ssh 192.168.48.129 &#8216;cat \/root\/hardwareInfo.txt&#8217;<br \/>\nmy_vda=6.00 GB<br \/>\nmy_vdb=NONE<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> ssh 192.168.48.132 &#8216;cat \/root\/hardwareInfo.txt&#8217;<br \/>\nmy_vda=1.00 GB<br \/>\nmy_vdb=NONE<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>In this post we are going to see how we can use the below two modules to download a file from a URL or and <a class=\"mh-excerpt-more\" href=\"http:\/\/shijuvarghese.com\/?p=1573\" title=\"Ansible: Download a file from website and replace value\">[&#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-1573","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\/1573","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=1573"}],"version-history":[{"count":10,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/1573\/revisions"}],"predecessor-version":[{"id":1584,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/1573\/revisions\/1584"}],"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=1573"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1573"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1573"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}