我一直在尝试很多lineinfile并替换示例,但是我无法使其正常工作。

我有以下几行:

deb-src http://httpredir.debian.org/debian buster main
deb http://security.debian.org/debian-security buster/updates main
deb-src http://security.debian.org/debian-security buster/updates main


我希望他们看起来像这样:

deb-src http://httpredir.debian.org/debian buster main contrib
deb http://security.debian.org/debian-security buster/updates main contrib
deb-src http://security.debian.org/debian-security buster/updates main contrib


我曾尝试用Ansible这样做;

- name: Add contrib repository
  lineinfile:
    dest: /etc/apt/sources.list
    backup: yes
    state: present
    regexp: '^(deb (.*)$)'
    backrefs: yes
    line: '\1 contrib'
  check_mode: no


哪种方法有效,但是在第二次运行后它显示为例如

deb http://security.debian.org/debian-security buster/updates main contrib contrib


因此,它会在每次运行时不断添加单词。

显然,并不是每个系统上的行都是如此,但是它们确实以deb或deb-src开头

谢谢!

最佳答案

- name: Add contrib repository
  replace:
    dest: /etc/apt/sources.list
    regexp: '^(deb.+)(?<! contrib)$'
    replace: '\1 contrib'

关于linux - 在Ansible行末添加单词,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58994046/

10-10 04:17