ubuntu – 是否可以将Ansible authorized_key与多个密钥一起使用?

我使用Ansible相当新,并且一直在阅读 here和谷歌,但还没有找到答案.

我的情况是我在服务器上有1个用户,但需要在其authorized_keys文件中放入2-3个不同的pub键.

我可以成功删除所有密钥,或使用此脚本添加所有密钥:

---
  - 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

使用当前标志,它会读取并添加所有键.使用缺席标志,它将删除列出的所有键.

问题是我有一个仅在服务器上的旧密钥,我想删除/覆盖它,并且为将来的部署覆盖可能在服务器上而不是在我的剧本中的任何未授权密钥.

使用独占标志,它只需要最后一个键并添加它.如果它循环并重复添加所有键,这将是太棒了.如果有一种方法可以在Ansible中执行此操作,我还没有找到它.

有没有办法循环pub文件并同时使用独占选项?

有没有办法循环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.

因此,您需要加入所有密钥并立即发送所有密钥.
像这样的东西:

- name: update SSH keys
  authorized_key:
    user: <user>
    key: "{{ lookup('pipe','cat ../files/pub_keys/*.pub') }}"
    state: present
    exclusive: yes

在生产中运行之前检查此代码

相关文章

目录前言一、创建Hadoop用户二、更新apt和安装Vim编辑器三、...
原文连接:https://www.cnblogs.com/yasmi/p/5192694.html ...
电脑重启后,打开VirtualBox,发现一直用的虚拟机莫名的消失...
参见:https://blog.csdn.net/weixin_38883338/article/deta...
Ubuntu 18.04 LTS 已切换到 Netplan 来配置网络接口。Netpla...
介绍每个 Web 服务都可以通过特定的 URL 在 Internet 上访问...