{"id":17948,"date":"2022-08-03T18:49:56","date_gmt":"2022-08-03T18:49:56","guid":{"rendered":"https:\/\/linuxways.net\/?p=17948"},"modified":"2022-08-17T04:30:41","modified_gmt":"2022-08-17T04:30:41","slug":"replace-string-file-using-ansible","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/centos\/replace-string-file-using-ansible\/","title":{"rendered":"How to Replace a String in a File Using Ansible"},"content":{"rendered":"<p>Ansible has a \u2018replace\u2019, \u2018blockinfile\u2019 and \u2018lineinfile\u2019 modules for handling lines in a file. Each of them is used for a specific purpose. The choice of choosing one among them depends on the situation.<\/p>\n<h2><strong>What Will We Cover?<\/strong><\/h2>\n<p>In this post, we will explore how to replace any string in a file using Ansible. Ansible has a \u2018ansible.builtin.replace\u2019 module for the same purpose. Let us explore the replace module below.<\/p>\n<p>The general syntax for the Replace module is as follows:<\/p>\n<pre>\r\n- name: Name of the task\r\n\r\nansible.builtin.replace:\r\n\r\npath: path of the file that needed to be changed\r\n\r\nregexp: regex expression for old string to be replaced\r\n\r\nreplace: new string that needs to be added to file\r\n<\/pre>\n<p>Let us see some example playbooks below. The below playbook will check for the hosts file at path provided and replace the old host name(managed1.anslab.com) with new host name(linuxhint.com):<\/p>\n<pre>\r\n---\r\n\r\n- hosts: managed1(Name of your target node)\r\n\r\ngather_facts: no\r\n\r\nbecome: true\r\n\r\ntasks:\r\n\r\n- name: Replace module demo\r\n\r\nansible.builtin.replace:\r\n\r\npath: \/etc\/hosts\r\n\r\nregexp: '(\\s+)managed1\\.anslab\\.com(\\s+.*)?$'\r\n\r\nreplace: '\\1linuxhint.com\\2'\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"953\" height=\"659\" class=\"wp-image-17963\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17948-1.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17948-1.png 953w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17948-1-300x207.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17948-1-768x531.png 768w\" sizes=\"auto, (max-width: 953px) 100vw, 953px\" \/><\/p>\n<p>You can see the output of this playbook below. It replaced every occurrence of \u201cmanaged1.anslab.com\u201d in the file with \u201clinuxhint.com\u201d.<\/p>\n<p>I am using regex expression here. regex helps in matching multiple strings or a single string. Learn more about python regex here <a href=\"https:\/\/docs.python.org\/3\/library\/re.html\">https:\/\/docs.python.org\/3\/library\/re.html<\/a>.<\/p>\n<p>Now, let us explore the other parameters of this module:<\/p>\n<h3><strong>1. \u2018after\u2019<\/strong><\/h3>\n<p>When we have the \u2018after\u2019 parameter along with \u2018replace\u2019, it is the \u2018after\u2019 parameter from where the replacement of the content will actually start and it continues till the end of the file.<\/p>\n<p>To be clearer, let us see the below playbook. Here, we have the \u2018after\u2019 parameter value as \u201cPubkeyAuthentication\u201d. After encountering the \u201cPubkeyAuthentication\u201d string, it will replace the word \u201cyes\u201d with \u201cno\u201d.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<pre>\r\n- name: Syntax for Replace module with the \u2018after\u2019 expression.\r\n\r\nansible.builtin.replace:\r\n\r\npath: path of the file\r\n\r\nafter: word after which replacement starts\r\n\r\nregexp: regular expression to match the word that needs to be changed\r\n\r\nreplace: string that will replace old word\r\n<\/pre>\n<p>In the playbook below, observe the usage of the \u2018after\u2019 expression:<\/p>\n<pre>\r\n---\r\n\r\n- hosts: managed1 (Name of your target node)\r\n\r\ngather_facts: no\r\n\r\nbecome: true\r\n\r\ntasks:\r\n\r\n- name: Replace module demo\r\n\r\nansible.builtin.replace:\r\n\r\npath: \/etc\/ssh\/sshd_config\r\n\r\nregexp: 'yes'\r\n\r\nafter: 'PubkeyAuthentication'\r\n\r\nreplace: 'no'\r\n<\/pre>\n<p>If you check the output below, you may notice after the string \u201cPubkeyAuthentication\u201d. The string \u201cyes\u201d is now replaced with \u201cno\u201d. I am using sed to get just one line from the file for easy visibility.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"957\" height=\"519\" class=\"wp-image-17968\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17948-2.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17948-2.png 957w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17948-2-300x163.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17948-2-768x417.png 768w\" sizes=\"auto, (max-width: 957px) 100vw, 957px\" \/><\/p>\n<h3><strong>2. \u2018before\u2019<\/strong><\/h3>\n<p>\u2018before\u2019 is opposite to \u2018after\u2019. It will replace every matching string specified for the \u2018before\u2019 parameter.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<pre>\r\n- name: Syntax for Replace module with the \u2018before\u2019 expression.\r\n\r\nansible.builtin.replace:\r\n\r\npath: path of the file that needs to be updated\r\n\r\nbefore: before this word, it will match the regex and replace it with new word\r\n\r\nregexp: regular expression to match the word to be changed\r\n\r\nreplace: string that will replace the old word\r\n<\/pre>\n<p>Let us see an example playbook for the same. The below playbook uses the \u2018before\u2019 keyword:<\/p>\n<pre>\r\n---\r\n\r\n- hosts: managed1(Name of your target node)\r\n\r\ngather_facts: no\r\n\r\nbecome: true\r\n\r\ntasks:\r\n\r\n- name: Replace module demo\r\n\r\nansible.builtin.replace:\r\n\r\npath: \/etc\/hosts\r\n\r\nbefore: 'managed1'\r\n\r\nregexp: 'linuxhint.com'\r\n\r\nreplace: 'linuxways.net'\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"959\" height=\"471\" class=\"wp-image-17972\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17948-3.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17948-3.png 959w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17948-3-300x147.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17948-3-768x377.png 768w\" sizes=\"auto, (max-width: 959px) 100vw, 959px\" \/><\/p>\n<p>In the above playbook, before the word \u201cmanaged1\u201d, if there is any string like \u201clinuxhint.com\u201d, it will be replaced with \u201clinuxways.net\u201d for simplicity purposes. Here, I am not using any complex regex expressions. You can see the file before and after running output in the above screenshot.<\/p>\n<h3><strong>3. \u2018before and after\u2019<\/strong><\/h3>\n<p>You can also use before and after parameters simultaneously to replace words in between.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<pre>\r\n- name: Syntax for Replace module with the \u2018before\u2019 and \u2018after\u2019 expressions.\r\n\r\nansible.builtin.replace:\r\n\r\npath: path of the file\r\n\r\nafter: after this word the word will be replaced\r\n\r\nbefore: before this word the word will be replaced\r\n\r\nregexp: regular expression to match the word to be replaced\r\n\r\nreplace: string that will replace old word\r\n<\/pre>\n<p>Now, let us look into a simple example playbook below that demonstrates before and after.<\/p>\n<pre>\r\n---\r\n\r\n- hosts: managed1\r\n\r\ngather_facts: no\r\n\r\nbecome: true\r\n\r\ntasks:\r\n\r\n- name: Replace module demo\r\n\r\nansible.builtin.replace:\r\n\r\npath: \/etc\/hosts\r\n\r\nafter: '127.0.2.1'\r\n\r\nbefore: 'managed1'\r\n\r\nregexp: 'linuxways.net'\r\n\r\nreplace: 'tecofers.com'\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"955\" height=\"510\" class=\"wp-image-17977\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17948-4.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17948-4.png 955w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17948-4-300x160.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17948-4-768x410.png 768w\" sizes=\"auto, (max-width: 955px) 100vw, 955px\" \/><\/p>\n<p>In the above playbook, you can clearly observe that the string before \u2018managed1\u2019 and after \u201c127.0.2.1\u201d string is replaced with the string \u201ctecofers.com\u201d.<\/p>\n<p>Going further, there are few more interesting attributes for this module, for example backup<strong>.<\/strong><\/p>\n<h3><strong>4. \u2018backup\u2019<\/strong><\/h3>\n<p>The \u2018backup\u2019 property has two options that can be set as \u2018yes\u2019 or \u2018no\u2019. If you set backup as yes, it will create a backup file of the original file along with its timestamp. So, in case you messed up the original file, you can refer to backup file (suggested for production systems).<\/p>\n<h2><strong>Conclusion<\/strong><\/h2>\n<p>In this guide, we have seen the usage of the \u2018replace\u2019 module in Ansible for replacing a string in a file. Also, you can refer to the official ansible document for more parameters that go with this module.<\/p>","protected":false},"excerpt":{"rendered":"<p>In this post, we will explore how to replace any string in a file using Ansible. Ansible has a \u2018ansible.builtin.replace\u2019 module for the same purpose.<\/p>","protected":false},"author":102,"featured_media":18091,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-17948","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-centos"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/17948","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/102"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=17948"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/17948\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/18091"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=17948"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=17948"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=17948"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}