ubuntu - 是否可以将 Ansible authorized_key exclusive 与多个 key 一起使用?

标签 ubuntu ansible authorized-keys

我刚开始使用 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/

相关文章:

Erlang、SSH 和authorized_keys

python - 无法在 ubuntu 13.04 上安装 pyhdf

ubuntu - 我想通过 apt-get 在 ubuntu 12.04 中安装 php 5.3.9 - 我应该添加什么存储库?

ansible 2.3.0 - 没有可用的身份验证方法

jenkins - 已从 Google Cloud VM 中删除 authorized_keys

ubuntu - 将 PuTTYgen 中生成的另一个 key 附加到authorized_keys 文件

javascript - 如何使用 nodejs 发送 udp 包?

linux中的pythonnet无法导入ssl

Ansible 在服务器中挂载多个磁盘

ansible - 如何在执行结束时总结 Ansible Tower 中失败的断言?