我很难相信没有什么可以涵盖此用例的,但是我的搜索却无济于事.
I find it hard to believe there isn't anything that covers this use case but my search has proved fruitless.
我在/etc/fstab中有一行来挂载不再可用的驱动器:
I have a line in /etc/fstab to mount a drive that's no longer available:
//archive/Pipeline /pipeline/Archives cifs ro,credentials=/home/username/.config/cifs 0 0我想要将其更改为
#//archive/Pipeline /pipeline/Archives cifs ro,credentials=/home/username/.config/cifs 0 0我正在使用
--- - hosts: slurm remote_user: root tasks: - name: Comment out pipeline archive in fstab lineinfile: dest: /etc/fstab regexp: '^//archive/pipeline' line: '#//archive/pipeline' state: present tags: update-fstab期望它只是插入注释符号(#),但取而代之的是替换了整行,最后我得到了
expecting it to just insert the comment symbol (#), but instead it replaced the whole line and I ended up with
#//archive/Pipeline是否有办法捕获行的其余部分或仅插入单个注释字符?
is there a way to glob-capture the rest of the line or just insert the single comment char?
regexp: '^//archive/pipeline *' line: '#//archive/pipeline *'或
regexp: '^//archive/pipeline *' line: '#//archive/pipeline $1'我正在尝试把头包裹在lineinfile上,从我读过的内容来看,好像insertafter是我要的东西,但是"insert after"不是我想要的吗?
I am trying to wrap my head around lineinfile and from what I"ve read it looks like insertafter is what I'm looking for, but "insert after" isn't what I want?
推荐答案您可以使用 replace 适用于您的案例的模块:
You can use the replace module for your case:
--- - hosts: slurm remote_user: root tasks: - name: Comment out pipeline archive in fstab replace: dest: /etc/fstab regexp: '^//archive/pipeline' replace: '#//archive/pipeline' tags: update-fstab它将替换所有与regexp匹配的字符串.
It will replace all occurrences of the string that matches regexp.
lineinfile仅在一行上起作用(即使在文件中找到多个匹配项).这样可以确保特定行不存在或存在已定义的内容.
lineinfile on the other hand, works only on one line (even if multiple matching are find in a file). It ensures a particular line is absent or present with a defined content.
更多推荐
使用Ansible lineinfile模块注释掉一行
发布评论