- name: sshd configuration
lineinfile:
path: /etc/ssh/sshd_config
regexp: "{{ item.regexp }}"
line: "{{ item.line }}"
loop:
- { regexp: '^[\s#]*AllowTcpForwarding', line: "AllowTcpForwarding yes" }
notify: reload sshd
最开始 目标文件中有两行匹配 regexp ^[\s#]*AllowTcpForwarding
, 但两行都匹配 line, 所以只有第二行被替换,如果要确保多余的行 AllowTcpForwarding no
被删除,需要使用
- name: sshd configuration
lineinfile:
path: /etc/ssh/sshd_config
regexp: "{{ item.regexp }}"
line: "{{ item.line | default(omit) }}"
state: "{{ item.state | default('present') }}"
loop:
- { regexp: '^\s*AllowTcpForwarding\s+no', state: "absent" }
- { regexp: '^[\s#]*AllowTcpForwarding', line: "AllowTcpForwarding yes" }
notify: reload sshd
if state is absent, ansible will delete all lines match the regexp.
Top comments (0)