# Search and remove string that matches a pattern at beginning of a line

**URL:** <https://forum.ansible.com/t/search-and-remove-string-that-matches-a-pattern-at-beginning-of-a-line/34147>\
**Category:** Ansible Project\
**Created:** [November 3, 2020, 4:14am UTC](https://forum.ansible.com/t/search-and-remove-string-that-matches-a-pattern-at-beginning-of-a-line/34147 "2020-11-03T04:14:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris\_Bidwell](https://avatars.discourse-cdn.com/v4/letter/c/da6949/32.png) [@Chris\_Bidwell](https://forum.ansible.com/u/Chris_Bidwell)\
**Post date:** [November 3, 2020, 4:14am UTC](https://forum.ansible.com/t/search-and-remove-string-that-matches-a-pattern-at-beginning-of-a-line/34147/1 "2020-11-03T04:14:47Z")

</div>

Hey all,

Interesting question here. I want to remove the string noexec out of a line in my /etc/fstab. In this instance, I only want it to be in the /tmp line.  
I was thinking a replace module, but not sure about a regexp that would work. Any thoughts?

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [November 3, 2020, 4:20pm UTC](https://forum.ansible.com/t/search-and-remove-string-that-matches-a-pattern-at-beginning-of-a-line/34147/2 "2020-11-03T16:20:24Z")

</div>

Try this:

&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- slurp:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src: /etc/fstab  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: fstab64

&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmp\_parts: "{{ (fstab64.content|b64decode |  
regex\_search('.\*\\S+\\s+\/tmp\\s+\\S+\\s+.\*')).split() }}"

&nbsp;&nbsp;&nbsp;&nbsp;- mount:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fstab: /etc/fstab  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: present  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: "{{ tmp\_parts[1] }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src: "{{ tmp\_parts[0] }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;opts: "{{ tmp\_parts[3].split(',')|difference(['noexec'])|join(',') }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fstype: "{{ tmp\_parts[2] }}"

---

<div class="post-metadata">

**Author:** ![Lars\_Liedtke](https://avatars.discourse-cdn.com/v4/letter/l/c68b51/32.png) [@Lars\_Liedtke](https://forum.ansible.com/u/Lars_Liedtke)\
**Post date:** [November 3, 2020, 4:21pm UTC](https://forum.ansible.com/t/search-and-remove-string-that-matches-a-pattern-at-beginning-of-a-line/34147/3 "2020-11-03T16:21:29Z")

</div>

Hey,

You could use the lineinfile-module.

With something like:

lineinfile:  
regexp: “^/dev/mapper/vg\_rhel-var\_tmp”  
line: “/dev/mapper/vg\_rhel-var\_tmp /var/tmp xfs defaults,noexec,nosuid,nodev 0 0”

Take into account that the regexp definition has to match **before** and **after** replacement

Cheers

Lars

---

<div class="post-metadata">

**Author:** ![Lars\_Liedtke](https://avatars.discourse-cdn.com/v4/letter/l/c68b51/32.png) [@Lars\_Liedtke](https://forum.ansible.com/u/Lars_Liedtke)\
**Post date:** [November 3, 2020, 4:26pm UTC](https://forum.ansible.com/t/search-and-remove-string-that-matches-a-pattern-at-beginning-of-a-line/34147/4 "2020-11-03T16:26:28Z")

</div>

Of course I made a copy&paste error:

the line definition must be without the noexec then 😉
