可以使用以下Ansible任务来替换sshd_config中的值:
- name: Replace value in sshd_config
replace:
path: /etc/ssh/sshd_config
regexp: '^#?{{ item.key }}.*'
replace: '{{ item.key }} {{ item.value }}'
with_dict:
sshd_config:
Protocol: 2
PermitRootLogin: no
PasswordAuthentication: no
此任务使用replace模块,将sshd_config中的指定键值替换为给定值。其中,with_dict用来循环遍历sshd_config字典中的项,每个项对应一个键值对。此任务将协议版本设置为2,禁止根用户登录,禁止密码验证登录。你可以根据实际需要自行更改字典中的条目。