【问题标题】:Add Key pair to remote linux server将密钥对添加到远程 linux 服务器
【发布时间】:2019-07-06 13:30:56
【问题描述】:

我想使用 ansible 在 Linux 服务器“Ubuntu 18.04lts”上为“tuser”添加一个密钥对,以避免基于密码的登录。 所以我在 yml 剧本文件中成功地尝试了这种方式:

- name: Set authorized key for tuser
  become: yes
  authorized_key:
    user: tuser
    state: present
    key:  "{{ lookup('file', '/home/rogg/.ssh/id_rsa.pub') }}"

好吧,但是当我尝试在密钥中使用其他位置时:

- name: Set authorized key for tuser
  become: yes
  authorized_key:
    user: tuser
    state: present
    key: "{{ role_path }}/files/csbin_keys/id_rsa.pub"

我明白了:

"msg": "指定的密钥无效

我已经使用 {{ role_path }} 复制其他文件并且可以正常工作,但是在这个键中它没有

【问题讨论】:

    标签: ansible ssh-keys


    【解决方案1】:

    摘自 authorized_key 模块的文档:

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

    在您的第一个示例中,lookup('file', '/home/rogg/.ssh/id_rsa.pub') 读取文件 /home/rogg/.ssh/id_rsa.pub 并将其内容作为 key 值提供。

    在您的第二个示例中,您尝试将文件路径作为key 值提供。

    用查找替换它:

    lookup('file', role_path+'/files/csbin_keys/id_rsa.pub')
    

    【讨论】:

      【解决方案2】:

      您也可以使用with_file 选项,它读取文件的内容:

      - name: Ensure the public key is populated
          authorized_key:
            user: john
            state: present
            key: '{{ item }}'
          with_file:
            - /home/john/.ssh/id_rsa.pub
      

      【讨论】:

        猜你喜欢
        • 2013-06-28
        • 2013-09-12
        • 2018-05-19
        • 2019-08-24
        • 1970-01-01
        • 1970-01-01
        • 2012-11-26
        • 2014-06-28
        • 1970-01-01
        相关资源
        最近更新 更多