我有一个基本的剧本,它将位于public_keys文件夹中的所有公共(public)密钥附加到.ssh / authorized_keys中用户的文件夹中:

- hosts: default

vars:
  user: user1

tasks:
- name: Set up authorized_keys for the user
  authorized_key: user={{ user }} key="{{ item }}"
  with_fileglob:
  - public_keys/*.pub

当我在ansible上运行它时,它给了我这个错误,而我对此非常执着:
TASK [Set up authorized_keys for the user] ************************
failed: [default] => (item=/Users/trax/Git/ansible-keys/public_keys/test.pub) => {"failed": true, "item": "/Users/trax/Git/ansible-keys/public_keys/test.pub", "msg": "invalid key specified: /Users/trax/Git/ansible-keys/public_keys/test.pub"}

公用密钥文件在我当前正在使用时是完全有效的,并且可以正常运行。它没有任何评论,我实际上将其粘贴在这里,以便您可以看到它:
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC4e+RLnQAqo3azuFzbynD9n6L7Qc2NjEwNLQRqKOd17532rHAhGOxz9ZV7ca5J6y9Z8QyV2EP9oXXpXd7I9oG1ybiU2cOmMQ7mIMFnMgy90dgVmF4X4Rj3fPch271TIQhvBH36L1eagk98Tlj32zepHNmC7ECFiAUihxXsuGAcFK4l9Y3s0HZe913E1ewUxXjUZAaqmzEQwW621hWDDTU1zUCnPPqEe6DFy6PUP8YL8mLbbKuSL2W6bD7rzm1axZANvoYeD5egvzwSMeZ8f+XF3MbuyhiJhGEFjwDfDkibP4bwQqZm5IdI1c0Ot2X67OHFsHx04gbs6ZzBkD39Z6Jr [email protected]

有什么建议吗?提前非常感谢...

最佳答案

假设密钥文件是控制机器本地的,则可以使用file lookup轻松获取密钥内容,例如:

- hosts: default
  tasks:
  - authorized_key:
      user: '{{ user }}'
      key: '{{ lookup('file', item) }}'
    with_fileglob: public_keys/*.pub

09-30 15:23