Regex 使用Ansible添加fstab选项
我正在尝试将nodev添加到我的Regex 使用Ansible添加fstab选项,regex,ansible,mount,mount-point,Regex,Ansible,Mount,Mount Point,我正在尝试将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,]+)*)' li
/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
虽然这可能不是最优雅的答案,但它对我很有效
- 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
我们开发了一个第三方ansible模块,用于添加、设置或删除装载选项。看看
- mountopts:
name: /
option: nodev
降落在这里寻找答案,最后为我的用例滚动我自己的: main.yml
- include: fstab-opts.yml point=/tmp opts=noexec,nodev,nosuid,noatime
- include: fstab-opts.yml point=/backup opts=noatime
- import_tasks: fstab-opt-present.yml point=/home opt=nodev
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
受Joe回答的启发,我制作了这个版本,它将在
/etc/fstab
中的特定行中添加一个选项(如果还没有)。这也将保留该行已有的任何其他选项
main.yml
- include: fstab-opts.yml point=/tmp opts=noexec,nodev,nosuid,noatime
- include: fstab-opts.yml point=/backup opts=noatime
- 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
是构建和测试此类regexp的非常有用的工具。只需在那里启用“多行”选项并打开“替换”面板,您甚至可以粘贴到/etc/fstab
中,查看您的正则表达式将匹配哪些行以及它将对它们做什么。只要记住使用实值而不是Ansible变量{{point}}
等。在那里测试时测试&效果很好
- 名称:设置nodev选项 替换: 路径:/etc/fstab 备份:是的 regexp:“^(\S+\S+)(/\S+)(?:ext4 | xfs)\S+(?:\S*,)?nodev(?,\S*)?\S+(\S+)(\S+)(\S+)$” 替换:'\1\2\4\5,nodev\6'
注意:在测试regexp101时,请确保选择python,我想说的是,似乎有一个新的ansible模块可以更轻松地涵盖所有这些:
定义“没有运气”。实际结果如何??我猜/etc/fstab没有改变,而是在一些行中添加了“nodev”?@LarsH马上就会在这里提供结果,我将regexp修改为(^/dev[\w/\u-]+\ s+/[\w/\u-]+\ s+/[\w/\u-]+\ s+((?!nodev)[\w,]+]),我认为这可能有效,我认为你在重复组中有
\s+
选项的事实阻碍了匹配。如果不起作用,请告诉我。之前的regexp将nodev添加到交换驱动器(我不希望添加到的驱动器之一)…应用新的regex不会修改任何内容。删除\s+只将其添加到一个映射器驱动器(在字符串的末尾,而不是它需要去的地方),实际上我觉得它非常漂亮和简洁。为此,我只需要编辑一些装载路径,所以我添加了一个路径列表(mnt_路径)。然后添加了附加条件,说明mnt_路径中的item.mount
何时更有针对性。不确定这是否取决于版本,但在ansible 2.4.2.0中,我必须对_项使用:{{ansible_mounts}”
相反。这种方法的问题是,它可能会将临时挂载合并到静态/etc/fstab
,而且它还会将不太稳定的变体的某些项目更改为动态/dev/mapper/*
路径。欢迎链接到解决方案,但是请确保你的答案在没有它的情况下是有用的:这样你的其他用户就会知道它是什么以及它为什么在那里,然后引用你链接到的页面中最相关的部分,以防目标页面不可用。