Ansible使用ssh公钥从一个帐户复制到另一个帐户

时间:2017-12-26 10:44:03

标签: ansible

我遇到了在远程服务器上的两个帐户之间复制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

2 个答案:

答案 0 :(得分: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 }}"

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

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

答案 1 :(得分:0)

如果你从中挑选它 /root/.ssh/authorized 而不是取代家 /charlie/.ssh/id_rsa.pub from /root/.ssh/authorized_keys

用sudo做。使用become: true  你的任务中的论据。