我正在尝试将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发布于 2014-09-23 01:11:04
虽然这可能不是最优雅的答案,但它对我很有效。
- 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发布于 2018-01-15 17:00:07
受乔的回答的启发,我制作了这个版本,它将在/etc/fstab中的特定行中添加一个选项,如果它还没有出现的话。这也将保留该行已有的任何其他选项。
main.yml
- import_tasks: fstab-opt-present.yml point=/home opt=nodevfstab-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/是一个非常有用的工具。只需启用"multiline“选项并打开”替换“面板,您甚至可以粘贴到您的/etc/fstab中,看看您的正则表达式将匹配哪些行,以及它将对它们做什么。只需记住在那里测试时使用实数值而不是Ansible变量{{ point }}等
发布于 2017-01-16 00:50:32
我们已经开发了一个第三方ansible模块来添加、设置或删除挂载选项。看看这个!
- mountopts:
name: /
option: nodevhttps://stackoverflow.com/questions/25977410
复制相似问题