Ansible替换模块不起作用

时间:2018-05-23 04:14:12

标签: ansible

使用ansible replace模块在AIX /etc/inetd.conf文件中注释以下行“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,它就不会对这两行进行注释。

= unclear =>想知道具有相同行内容< =的两个文件,但是在/etc/inetd.conf上不起作用并且在测试文件上工作。请帮助。

1 个答案:

答案 0 :(得分:0)

保持简单。所有你关心的是如果它以某个值开始替换该行,是吗?其余部分并不特别重要(除非确实如此......)

如果这是对的,那么试试这个:

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