使用[复制]将ssh公钥从一个帐户复制到另一个帐户

问题描述 投票:0回答:2

这个问题在这里已有答案:

我面临着在远程服务器上的两个帐户之间复制ssh密钥的问题。我有一个名为“rmt”的远程服务器,在rmt我有一个名为“clado”的帐户我想使用Ansible将/root/.ssh/authorized_keys(在rmt上)复制到/home/clado/.ssh/authorized_keys(在rmt上)。

我得到了这个示例代码:

- name: Set authorized key in alternate location
  authorized_key:
    user: charlie
    state: present
    key: "{{ lookup('file', '/home/charlie/.ssh/id_rsa.pub') }}"

但它正在使用当地的/home/charlie/.ssh/id_rsa.pub

ansible
2个回答
2
投票

但它使用的是本地('/ home / charlie / .ssh / id_rsa.pub')。

所有查找插件都在Ansible控制机器上本地工作。

您可以使用slurp module获取远程文件的内容,例如:

- name: Fetch authorized key from alternate location
  slurp:
    src: /home/other_user/.ssh/id_rsa.pub
  register: slurped_key_b64

- name: Ensure the fetched key is set for charlie
  authorized_key:
    user: charlie
    state: present
    key: "{{ slurped_key_b64.content | b64decode }}"

自定义详细信息,因为您的描述和代码不匹配。

但从系统管理的角度来看,这种流程通常没有多大意义。从控制机器分配密钥。


0
投票

如果你从/root/.ssh/authorized采摘它而不是取代家庭/charlie/.ssh/id_rsa.pub from /root/.ssh/authorized_keys

和sudo一起做。在任务中使用become: true参数。

© www.soinside.com 2019 - 2024. All rights reserved.