我刚开始使用 Ansible,一直在阅读 here和谷歌,还没有找到答案。
我的情况是,我在一台服务器上有 1 个用户,但有 2-3 个不同的公钥需要放入它的 authorized_keys 文件中。
我可以成功删除所有 key ,或使用此脚本添加所有 key :
---
- hosts: all
tasks:
- name: update SSH keys
authorized_key:
user: <user>
key: "{{ lookup('file', item) }}"
state: present
#exclusive: yes
with_fileglob:
- ../files/pub_keys/*.pub
使用 present
标志,它读取并添加所有键。使用 absent
标志,它会删除列出的所有键。
问题是我有一个只在服务器上的旧 key ,我想删除/覆盖它,并为将来的部署覆盖任何可能在服务器上但不在我的剧本中的未经授权的 key 。
使用 exclusive
标志,它只获取最后一个键并添加它。如果它能循环并递归地添加所有键,那就太棒了。如果有办法在 Ansible 中执行此操作,我还没有找到。
有什么方法可以循环遍历 pub 文件并同时使用 exclusive
选项吗?
最佳答案
有什么方法可以遍历 pub 文件并同时使用独占选项吗?
没有。 docs 中有关于循环和独占的说明:
exclusive: Whether to remove all other non-specified keys from the authorized_keys file. Multiple keys can be specified in a single key string value by separating them by newlines. This option is not loop aware, so if you use with_ , it will be exclusive per iteration of the loop, if you want multiple keys in the file you need to pass them all to key in a single batch as mentioned above.
因此,您需要加入所有 key 并一次发送所有 key 。
像这样:
- name: update SSH keys
authorized_key:
user: <user>
key: "{{ lookup('pipe','cat ../files/pub_keys/*.pub') }}"
state: present
exclusive: yes
在生产运行前检查此代码!
关于ubuntu - 是否可以将 Ansible authorized_key exclusive 与多个 key 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38879266/