【发布时间】:2018-04-15 07:42:15
【问题描述】:
通过使用 Ansible,我尝试确保我们服务器的 .ssh/authorized_keys 文件仅包含一组给定的 ssh 密钥。不管安排。
- 如果缺少,请添加(没问题,
lineinfile) - 如果其他人偷偷输入了额外的密钥(不在“with_items”列表中),请将其删除并返回一些警告或其他内容。嗯...“更改”也可以接受,但最好以某种方式区分“缺失”和“潜入”行。
第一部分很简单:
- name: Ensure ssh authorized_keys contains the right users
lineinfile:
path: /root/.ssh/authorized_keys
owner: root
group: root
mode: 0600
state: present
line: '{{ item }}'
with_items:
- ssh-rsa AABBCC112233... root@someserver.com
- ssh-rsa DDEEFF112233... user@anothersomeserver.com
但第二部分看起来更棘手。至少要使用简短而优雅的代码来完成它。
有什么想法吗?
【问题讨论】:
-
lineinfile 不是你的朋友。
-
为什么不总是用源文件覆盖目标?
-
@NeilMcGuigan:这可行,但我不知道是否缺少某些键或是否有一些额外的键。
标签: security ansible ssh-keys system-administration