可以使用 "lineinfile" 模块来避免 authorized_keys 文件被覆盖。下面是一个示例:
- name: Add Public Key
lineinfile:
path: /home/user/.ssh/authorized_keys
create: yes
line: "{{ lookup('file', '/home/user/.ssh/id_rsa.pub') }}"
state: present
这个示例将会在 /home/user/.ssh/authorized_keys 文件中添加一个公钥。如果文件不存在,它也会被创建。使用这个方法,即使对应的 "authorized_key" 模块再次运行,也不会覆盖原文件。