<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://ebasso.net/wiki/index.php?action=history&amp;feed=atom&amp;title=Ansible%3A_Replacing_text_in_file_complex_example</id>
	<title>Ansible: Replacing text in file complex example - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://ebasso.net/wiki/index.php?action=history&amp;feed=atom&amp;title=Ansible%3A_Replacing_text_in_file_complex_example"/>
	<link rel="alternate" type="text/html" href="https://ebasso.net/wiki/index.php?title=Ansible:_Replacing_text_in_file_complex_example&amp;action=history"/>
	<updated>2026-06-02T11:18:04Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.40.0</generator>
	<entry>
		<id>https://ebasso.net/wiki/index.php?title=Ansible:_Replacing_text_in_file_complex_example&amp;diff=8721&amp;oldid=prev</id>
		<title>Ebasso at 20:45, 13 March 2024</title>
		<link rel="alternate" type="text/html" href="https://ebasso.net/wiki/index.php?title=Ansible:_Replacing_text_in_file_complex_example&amp;diff=8721&amp;oldid=prev"/>
		<updated>2024-03-13T20:45:44Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 20:45, 13 March 2024&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l1&quot;&gt;Line 1:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 1:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;I need to replace umask into /etc/profile from:&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;I need to replace umask &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;values &lt;/ins&gt;into /etc/profile from:&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;  if [ $UID -gt 199 ] &amp;amp;&amp;amp; [ &amp;quot;`/usr/bin/id -gn`&amp;quot; = &amp;quot;`/usr/bin/id -un`&amp;quot; ]; then&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;  if [ $UID -gt 199 ] &amp;amp;&amp;amp; [ &amp;quot;`/usr/bin/id -gn`&amp;quot; = &amp;quot;`/usr/bin/id -un`&amp;quot; ]; then&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Ebasso</name></author>
	</entry>
	<entry>
		<id>https://ebasso.net/wiki/index.php?title=Ansible:_Replacing_text_in_file_complex_example&amp;diff=8720&amp;oldid=prev</id>
		<title>Ebasso at 20:45, 13 March 2024</title>
		<link rel="alternate" type="text/html" href="https://ebasso.net/wiki/index.php?title=Ansible:_Replacing_text_in_file_complex_example&amp;diff=8720&amp;oldid=prev"/>
		<updated>2024-03-13T20:45:11Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 20:45, 13 March 2024&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l19&quot;&gt;Line 19:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 19:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;  &amp;lt;nowiki&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;  &amp;lt;nowiki&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;- name: &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;Verify existence of &lt;/del&gt;file &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;/etc/profile locally&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;- name: &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;Replacing text in &lt;/ins&gt;file &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;complex example&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;   hosts: localhost&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;   hosts: localhost&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;   gather_facts: false&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;   gather_facts: false&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;

&lt;!-- diff cache key ebasso_wikidb:diff::1.12:old-8719:rev-8720 --&gt;
&lt;/table&gt;</summary>
		<author><name>Ebasso</name></author>
	</entry>
	<entry>
		<id>https://ebasso.net/wiki/index.php?title=Ansible:_Replacing_text_in_file_complex_example&amp;diff=8719&amp;oldid=prev</id>
		<title>Ebasso: Created page with &quot;I need to replace umask into /etc/profile from:   if [ $UID -gt 199 ] &amp;&amp; [ &quot;`/usr/bin/id -gn`&quot; = &quot;`/usr/bin/id -un`&quot; ]; then    umask &#039;&#039;&#039;022&#039;&#039;&#039;  else    umask &#039;&#039;&#039;027&#039;&#039;&#039;  fi  to   if [ $UID -gt 199 ] &amp;&amp; [ &quot;`/usr/bin/id -gn`&quot; = &quot;`/usr/bin/id -un`&quot; ]; then    umask &#039;&#039;&#039;002&#039;&#039;&#039;  else    umask &#039;&#039;&#039;022&#039;&#039;&#039;  fi   Below, an ansible script that do this:   &lt;nowiki&gt; - name: Verify existence of file /etc/profile locally   hosts: localhost   gather_facts: false   vars:     myfile:  /etc/...&quot;</title>
		<link rel="alternate" type="text/html" href="https://ebasso.net/wiki/index.php?title=Ansible:_Replacing_text_in_file_complex_example&amp;diff=8719&amp;oldid=prev"/>
		<updated>2024-03-13T20:44:38Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;I need to replace umask into /etc/profile from:   if [ $UID -gt 199 ] &amp;amp;&amp;amp; [ &amp;quot;`/usr/bin/id -gn`&amp;quot; = &amp;quot;`/usr/bin/id -un`&amp;quot; ]; then    umask &amp;#039;&amp;#039;&amp;#039;022&amp;#039;&amp;#039;&amp;#039;  else    umask &amp;#039;&amp;#039;&amp;#039;027&amp;#039;&amp;#039;&amp;#039;  fi  to   if [ $UID -gt 199 ] &amp;amp;&amp;amp; [ &amp;quot;`/usr/bin/id -gn`&amp;quot; = &amp;quot;`/usr/bin/id -un`&amp;quot; ]; then    umask &amp;#039;&amp;#039;&amp;#039;002&amp;#039;&amp;#039;&amp;#039;  else    umask &amp;#039;&amp;#039;&amp;#039;022&amp;#039;&amp;#039;&amp;#039;  fi   Below, an ansible script that do this:   &amp;lt;nowiki&amp;gt; - name: Verify existence of file /etc/profile locally   hosts: localhost   gather_facts: false   vars:     myfile:  /etc/...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;I need to replace umask into /etc/profile from:&lt;br /&gt;
&lt;br /&gt;
 if [ $UID -gt 199 ] &amp;amp;&amp;amp; [ &amp;quot;`/usr/bin/id -gn`&amp;quot; = &amp;quot;`/usr/bin/id -un`&amp;quot; ]; then&lt;br /&gt;
   umask &amp;#039;&amp;#039;&amp;#039;022&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
 else&lt;br /&gt;
   umask &amp;#039;&amp;#039;&amp;#039;027&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
 fi&lt;br /&gt;
&lt;br /&gt;
to&lt;br /&gt;
&lt;br /&gt;
 if [ $UID -gt 199 ] &amp;amp;&amp;amp; [ &amp;quot;`/usr/bin/id -gn`&amp;quot; = &amp;quot;`/usr/bin/id -un`&amp;quot; ]; then&lt;br /&gt;
   umask &amp;#039;&amp;#039;&amp;#039;002&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
 else&lt;br /&gt;
   umask &amp;#039;&amp;#039;&amp;#039;022&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
 fi&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Below, an ansible script that do this:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
- name: Verify existence of file /etc/profile locally&lt;br /&gt;
  hosts: localhost&lt;br /&gt;
  gather_facts: false&lt;br /&gt;
  vars:&lt;br /&gt;
    myfile:  /etc/profile&lt;br /&gt;
  tasks:&lt;br /&gt;
    - name: &amp;quot;Read {{ myfile }}&amp;quot;&lt;br /&gt;
      ansible.builtin.slurp:&lt;br /&gt;
        src: &amp;quot;{{ myfile }}&amp;quot;&lt;br /&gt;
      register: etc_profile&lt;br /&gt;
&lt;br /&gt;
    - name: Convert file content to string&lt;br /&gt;
      ansible.builtin.set_fact:&lt;br /&gt;
        profile_txt: &amp;quot;{{ etc_profile[&amp;#039;content&amp;#039;] | b64decode }}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
    - name: Change to &amp;#039;umask_002&amp;#039; between then_else&lt;br /&gt;
      ansible.builtin.replace:&lt;br /&gt;
        path: &amp;quot;{{ myfile }}&amp;quot;&lt;br /&gt;
        regexp: &amp;#039;(then\s+)umask\s\d+(\s+else)&amp;#039;&lt;br /&gt;
        replace: &amp;#039;\1umask 002\2&amp;#039;&lt;br /&gt;
      when: profile_txt | regex_search(&amp;#039;then\s+umask\s002\s+else&amp;#039;) is none&lt;br /&gt;
&lt;br /&gt;
    - name: Check &amp;#039;umask 022&amp;#039; between else_fi&lt;br /&gt;
      ansible.builtin.replace:&lt;br /&gt;
        path: &amp;quot;{{ myfile }}&amp;quot;&lt;br /&gt;
        regexp: &amp;#039;(else\s+)umask\s\d+(\s+fi)&amp;#039;&lt;br /&gt;
        replace: &amp;#039;\1umask 022\2&amp;#039;&lt;br /&gt;
      when: profile_txt | regex_search(&amp;#039;else\s+umask\s022\s+fi&amp;#039;) is none&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= See also =&lt;br /&gt;
&lt;br /&gt;
* [[DevOps| Artigos sobre DevOps]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Ansible]]&lt;br /&gt;
[[Category:DevOps]]&lt;br /&gt;
[[Category:Cloud]]&lt;/div&gt;</summary>
		<author><name>Ebasso</name></author>
	</entry>
</feed>