Ansible 可更换模块的工作原理不一样

Ansible 可更换模块的工作原理不一样,ansible,Ansible,在AIX/etc/inetd.conf文件中,使用ansible replace模块注释下面的行“ftp和telnet” "ftp stream tcp6 nowait root /usr/sbin/ftpd ftpd -l -u 077 -t 180" "telnet stream tcp6 nowait root /usr/sbin/telnetd telnetd -a" Ansible playbook: --- - name:

在AIX/etc/inetd.conf文件中,使用ansible replace模块注释下面的行“ftp和telnet”

"ftp     stream  tcp6    nowait  root    /usr/sbin/ftpd  ftpd -l -u 077 -t 180"
"telnet  stream  tcp6    nowait  root    /usr/sbin/telnetd       telnetd -a"

Ansible playbook:

---
- name: comment the line
  hosts: server1
  vars:
    - line1: "ftp     stream  tcp6    nowait  root    /usr/sbin/ftpd  ftpd -l -u 077 -t 180"
    - line2: "telnet  stream  tcp6    nowait  root    /usr/sbin/telnetd       telnetd -a"
  tasks:
    - name: Take the file backup
      shell:
        cmd: "cp /etc/inetd.conf /etc/inetd.conf_`date +%F` "

    - name: comment the line
      replace:
        path: /etc/test
        regexp: "{{ item }}"
        replace: "#{{ item }}"
        backup: yes
        owner: root
        group: system
        mode: 0777
      with_items:
        - "{{ line1 }}"
        - "{{ line2 }}"
代码与文件内容配合得非常好

$ cat /etc/test
ftp     stream  tcp6    nowait  root    /usr/sbin/ftpd  ftpd -l -u 077 -t 180
telnet  stream  tcp6    nowait  root    /usr/sbin/telnetd       telnetd -a
但是如果我尝试使用/etc/inetd.conf,它不会对这两行都进行注释


=不清楚=>想知道两个文件的行内容相同保持简单。你所关心的就是替换以某个值开头的行,是吗?这条线的其余部分并不特别重要(除非它有…)

如果是这样,请尝试以下方法:

- name: comment ftp and telnet
  replace:
     path: "/etc/test"
     regexp: "^(ftp|telnet) "
     replace: "#\1 "
     backup: yes
     owner: root
     group: system
     mode: 0777

它甚至评论了一个吗?你把它复杂化了。这是一个简单的sed任务
s/^ftp/#ftp/s/^telnet/#telnet/
所有权和权限是什么?哪个用户正在执行该步骤?请澄清你最后的陈述。