Ansible copying ssh public key from one account to

2019-03-04 01:57发布

This question already has an answer here:

I am facing a problem of copying ssh key between two accounts on a remote server. I have remote server called "rmt", on rmt I have one account called "clado" i want to copy the /root/.ssh/authorized_keys (on rmt) to /home/clado/.ssh/authorized_keys (on rmt) using Ansible.

I got this sample code:

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

But it is using the local /home/charlie/.ssh/id_rsa.pub.

标签: ansible
2条回答
在下西门庆
2楼-- · 2019-03-04 02:25

if you are picking it from /root/.ssh/authorized than replace home /charlie/.ssh/id_rsa.pub from /root/.ssh/authorized_keys

and do with it with sudo . Use become: true argument in your task.

查看更多
仙女界的扛把子
3楼-- · 2019-03-04 02:35

But it is using the local('/home/charlie/.ssh/id_rsa.pub').

All lookup plugins work locally on the Ansible control machine.

You can fetch the contents of a remote file with slurp module, for example:

- 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 }}"

Customise the details, because your description and your code don't match.

But generally this flow doesn't make much sense from system management point of view. Assign the key from the control machine.

查看更多
登录 后发表回答