13

我正在尝试将 nodev 添加到我的/etc/fstab文件中。我正在使用下面的 Ansible 命令,但没有运气。我的问题在于正则表达式,我不是正则表达式的专家。

- name: Add nodev to /etc/fstab
  lineinfile:
    dest=/etc/fstab
    backup=yes
    backrefs=yes
    state=present
    regexp='(^/dev[\w/_-]+(\s+(?!nodev)[\w,]+)*)'
    line='\1,nodev'

/etc/fstab我要添加的其中一条nodev是:

/dev/mapper/ex_sys-ex_home /home /ext4 rw,exec,auto,nouser,sync 1 2
4

7 回答 7

22

虽然这可能不是最优雅的答案,但它对我有用。

- name: Ensure fstab uses nodev
  mount:
    name: "{{ item.mount }}"
    src: "{{ item.device }}"
    fstype: "{{ item.fstype }}"
    opts: "{{ item.options }},nodev"
    state: present
  with_items: ansible_mounts
  when: item.options.find(",") >= 0 and item.options.find("nodev") == -1
于 2014-09-22T17:11:03.980 回答
8

受到乔的回答的启发,我制作了这个版本,/etc/fstab如果它还没有,它将向特定行添加一个选项。这也将保留该行已有的任何其他选项。

主要的.yml

- import_tasks: fstab-opt-present.yml point=/home opt=nodev

fstab-opt-present.yml

- name: '/etc/fstab: Set opt "{{ opt }}" for mount point {{ point }}'
  lineinfile:
    path: /etc/fstab
    backup: yes
    backrefs: yes
    regexp: '^(\S+\s+{{ point }}\s+\S+\s+)(?!(?:\S*,)?{{ opt }}(?:,\S*)?\s+)(\S+)(\s+.+)$'
    line: '\1{{ opt }},\2\3'
  register: fstab

- name: 'If {{ point }} changed, remount'
  command: 'mount {{ point }} -o remount'
  when: fstab.changed

https://regex101.com/是一个非常有用的工具,用于构建和测试这些正则表达式。只需在此处启用“多行”选项并打开“替换”面板,您甚至可以粘贴/etc/fstab并查看您的正则表达式将匹配哪些行以及它将对它们做什么。请记住在测试时使用真实值而不是 Ansible 变量{{ point }}

于 2018-01-15T09:00:07.307 回答
3

我们开发了一个 3rd-party ansible 模块来添加、设置或删除挂载选项。一探究竟!

  - mountopts:
      name: /
      option: nodev

https://github.com/Uberspace/ansible-mountopts

于 2017-01-15T16:50:32.277 回答
2

我想说似乎有一个新的 ansible 模块可以更轻松地涵盖所有这些: https ://docs.ansible.com/ansible/latest/modules/mount_module.html

于 2019-04-05T08:26:30.323 回答
2

测试和工作正常

- name: Set  nodev option
  replace:
    path: /etc/fstab
    backup: yes
    regexp: '^(\S+\s+)(\/\S+)(\s+)((?:ext4|xfs)\s+)(?!(?:\S*,)?nodev(?:,\S*)?\s+)(\S+)(\s+.+)$'
    replace: '\1\2 \4 \5,nodev \6'

它不包括将 nodev 添加到 /(root),仅设置为 ext4 和 xfs 文件系统。不添加到临时文件系统。

注意:在测试 regexp101 时,请确保选择 python

于 2019-02-19T21:31:11.000 回答
0

登陆这里寻找答案,最后为我的用例滚动了我自己的:

主要的.yml

- include: fstab-opts.yml point=/tmp opts=noexec,nodev,nosuid,noatime
- include: fstab-opts.yml point=/backup opts=noatime

fstab-opts.yml

---

- name: 'Ensure {{ point }} flags'
  lineinfile:
    path: /etc/fstab
    # uses "(not-spaces spaces /tmp spaces )(not-spaces)(the rest)" pattern to match column content and capture args
    regexp: '^([^ ]+[ ]+\{{ point }}[ ]+[^ ]+[ ]+)([^ ]+)(.*)'
    line: '\1{{ opts }}\3'
    backrefs: yes
  register: fstab

- name: 'If {{ point }} changed, remount'
  command: mount -o remount {{ point }}
  when: fstab.changed
于 2017-08-08T18:28:07.460 回答
0

我在 /etc/fstab 中为 /var/tmp 挂载点添加了 noexec,nodev,nosuid 选项。

要求是:

  1. 确保在 /var/tmp 分区上设置 noexec 选项
  2. 确保在 /var/tmp 分区上设置了 nodev 选项
  3. 确保在 /var/tmp 分区上设置 nosuid 选项

如果需要,使用以下命令安装 ansible.posix.mount 模块。

# ansible-galaxy collection install ansible.posix

剧本:

---
 - name: "STEP 1: Get /var/tmp mounted SRC device"
   shell: mount | grep -E '\s/var/tmp\s' | awk '{print $1}'
   register: "vartmpsrc"

 - debug:
     msg: "Validated the /var/tmp mount output: {{ vartmpsrc.stdout }}"

 - name: "Add mount noexec,nodev,nosuid options for /var/tmp"
   mount:
     path: "/var/tmp"
     src: "{{ vartmpsrc.stdout }}"
     fstype: "tmpfs"
     opts: "nosuid,nodev,noexec"
     state: "present"
   when: vartmpsrc.stdout == "/var/tmp"

 - name: Remount /var/tmp mounted volume with mount options noexec,nodev,nosuid
   ansible.posix.mount:
    path: /var/tmp
    state: remounted
   when: vartmpsrc.stdout == "/var/tmp"

 - name: 'STEP 2: Validate noexec,nodev,nosuid option set on /var/tmp partition'
   shell: mount | grep -E '\s/var/tmp\s' | grep -v {{ item }}
   loop:
    - noexec
    - nodev
    - nosuid
   register: vartmp_exists
   ignore_errors: yes
   when: vartmpsrc.stdout == "/var/tmp"
于 2021-07-26T12:52:43.937 回答