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

38 views
Skip to first unread message

Chris Bidwell - NOAA Federal

unread,
Nov 2, 2020, 11:15:24 PM11/2/20
to ansible...@googlegroups.com
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?

#
# /etc/fstab
# Created by anaconda on Tue Nov  3 01:51:55 2020
#
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
/dev/mapper/vg_rhel-root /                       xfs     defaults        0 0
UUID=3da5b7f5-fc36-4e8f-beb4-d270d3dff5f8 /boot ext4 defaults,nosuid,nodev 0 0
/dev/mapper/vg_rhel-home /home xfs defaults,nosuid,nodev 0 0
/dev/mapper/vg_rhel-opt /opt                    xfs     defaults        0 0
/dev/mapper/vg_rhel-tmp /tmp xfs defaults,noexec,nodev,nosuid 0 0
/dev/mapper/vg_rhel-usr /usr                    xfs     defaults        0 0
/dev/mapper/vg_rhel-usr_local /usr/local              xfs     defaults        0 0
/dev/mapper/vg_rhel-var /var xfs defaults,nodev 0 0
/dev/mapper/vg_rhel-var_lib_docker /var/lib/docker         xfs     defaults        0 0
/dev/mapper/vg_rhel-var_log /var/log xfs defaults,nodev,noexec,nosuid 0 0
/dev/mapper/vg_rhel-var_log_audit /var/log/audit xfs defaults,nodev,noexec,nosuid 0 0
/dev/mapper/vg_rhel-var_tmp /var/tmp xfs defaults,noexec,nosuid,nodev 0 0
/dev/mapper/vg_rhel-swap swap                    swap    defaults        0 0
tmpfs /dev/shm tmpfs rw,nosuid,nodev,noexec,seclabel 0 0

Dick Visser

unread,
Nov 3, 2020, 11:21:02 AM11/3/20
to ansible...@googlegroups.com
Try this:

tasks:
- slurp:
src: /etc/fstab
register: fstab64

- set_fact:
tmp_parts: "{{ (fstab64.content|b64decode |
regex_search('.*\\S+\\s+\/tmp\\s+\\S+\\s+.*')).split() }}"

- mount:
fstab: /etc/fstab
state: present
path: "{{ tmp_parts[1] }}"
src: "{{ tmp_parts[0] }}"
opts: "{{ tmp_parts[3].split(',')|difference(['noexec'])|join(',') }}"
fstype: "{{ tmp_parts[2] }}"
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAHKi8CiS-9EK%2BmL1bHY3-VO5RkYOzboh8d0Bz5oUYMYnTi9AoQ%40mail.gmail.com.



--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

Lars Liedtke

unread,
Nov 3, 2020, 11:21:51 AM11/3/20
to ansible...@googlegroups.com

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

Am 03.11.20 um 05:14 schrieb 'Chris Bidwell - NOAA Federal' via Ansible Project:
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAHKi8CiS-9EK%2BmL1bHY3-VO5RkYOzboh8d0Bz5oUYMYnTi9AoQ%40mail.gmail.com.
-- 
---
punkt.de GmbH
Lars Liedtke
.infrastructure

Kaiserallee 13a	
76133 Karlsruhe

Tel. +49 721 9109 500
https://infrastructure.punkt.de
in...@punkt.de

AG Mannheim 108285
Geschäftsführer: Jürgen Egeling, Daniel Lienert, Fabian Stein
signature.asc

Lars Liedtke

unread,
Nov 3, 2020, 11:26:52 AM11/3/20
to ansible...@googlegroups.com

Of course I made a copy&paste error:

the line definition must be without the noexec then ;-)

Am 03.11.20 um 17:21 schrieb Lars Liedtke:
signature.asc
Reply all
Reply to author
Forward
0 new messages