【问题标题】:ansible playbook public keys issueansible playbook公钥问题
【发布时间】:2016-04-27 17:58:05
【问题描述】:

我有这个基本的剧本,它将位于 public_keys 文件夹中的所有公钥附加到 .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"}

公钥文件完全有效,因为我目前正在使用它并且它运行良好。里面没有cmets,我把它贴在这里给大家看看:

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC4e+RLnQAqo3azuFzbynD9n6L7Qc2NjEwNLQRqKOd17532rHAhGOxz9ZV7ca5J6y9Z8QyV2EP9oXXpXd7I9oG1ybiU2cOmMQ7mIMFnMgy90dgVmF4X4Rj3fPch271TIQhvBH36L1eagk98Tlj32zepHNmC7ECFiAUihxXsuGAcFK4l9Y3s0HZe913E1ewUxXjUZAaqmzEQwW621hWDDTU1zUCnPPqEe6DFy6PUP8YL8mLbbKuSL2W6bD7rzm1axZANvoYeD5egvzwSMeZ8f+XF3MbuyhiJhGEFjwDfDkibP4bwQqZm5IdI1c0Ot2X67OHFsHx04gbs6ZzBkD39Z6Jr trax@M.local

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

【问题讨论】:

    标签: ssh key ansible ansible-playbook


    【解决方案1】:

    假设keyfiles是控制机器本地的,很容易使用file lookup来获取key内容,例如:

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

    【讨论】:

      【解决方案2】:

      key 参数的参数需要是 key(不是文件路径,而是实际的 contents)或 url。来自文档:

      key SSH 公钥,作为字符串或(从 1.9 开始)url (https://github.com/username.keys)

      所以你可以添加一个任务,将密钥读入一个注册的变量,然后循环安装密钥:

      - hosts: all
        tasks:
          - name: read keys
      
            # This needs to run on localhost, because that's where
            # the keys are stored.
            delegate_to: localhost
      
            command: cat {{item}}
      
            # Register the results of this task in a variable called
            # "keys"
            register: keys
      
            with_fileglob:
              - "public-keys/*.pub"
      
          - name: show what was stored in the keys variable
            debug:
              var: keys
      
          - authorized_key:
              user: fedora
              key: "{{item.stdout}}"
            with_items: "{{keys.results}}"
      

      Ansible documentation on using register with loops 了解详情。

      【讨论】:

        【解决方案3】:

        由于其中大部分都是旧的,我有一个适合我的更新版本。

              - name: Set authorized key taken from file
                authorized_key:
                  user: yourtargetusername
                  state: present
                  key: "{{ lookup('file', 'yourtargetkey.pub') }}"
        

        【讨论】:

          猜你喜欢
          • 2018-02-07
          • 2021-08-22
          • 2020-12-04
          • 1970-01-01
          • 2020-01-15
          • 2019-11-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多