Ansible authorized copy from remote source to remote destination How to use ansible authorized_key to authorize a ServerA (not the controller machine) to access Server B.

Scenario: Need a playbook to execute from a ansible controller that should append id_rsa.pub of a specific user from a remote ssh ServerA (no the controller machine ) to ServerB.

In our case the ServerA count is 20 while ServerB count is 200.
What i have tried so far and failed.

---
# tasks file for passwordless
- name: Play to setup Passowrdless
  hosts: destnode
  remote_user: "{{ ssh_user }}"
  vars_prompt:
    - name: "ssh_user"
      prompt: "Please specify ssh user name"
      private: no

  tasks:
  - name: Copy pub key from SourceNode to Destnode
    authorized_key:
      user: "{{ ssh_user }}"
      state: present
      key: "{{ lookup('file', lookup('env','HOME') + '/.ssh/id_rsa.pub') }}"
    become: yes
    become_user: "{{ ssh_user }}"
    delegate_to: "{{ hostvars.sourcenode }}"
    when: ssh_user is defined
    register: copyout


  - name: Print copyout value
    debug:
      msg: "{{ copyout }}"
To append id_rsa.pub of a specific user from a remote ssh ServerA (no the controller machine ) to ServerB.


1) Fetch the public keys from ServerA

- host: ServerA
  vars:
    public_keys_dir: &lt;PUB_KEYS_DIR&gt;
    specific_user:
      - user1
      - user2
      - userN
  tasks:
    - name: Fetch pub keys
      fetch:
        src: "/home/{{ item }}/.ssh/id_rsa.pub"
        dest: "{{ public_keys_dir }}/{{ item }}-ServerA.id_rsa.pub"
        flat: yes
      loop: "{{ specific_user }}"


2) Configure authorized keys at ServerB

- host: ServerB
  vars:
    public_keys_dir: &lt;PUB_KEYS_DIR&gt;
    my_remote_user: admin
  tasks:
    - name: Set up authorized_keys
      authorized_key:
        user: "{{ my_remote_user }}"
        key: "{{ lookup('file', '{{ item }}')}}"
      with_fileglob:
        - "{{ public_keys_dir }}/*-ServerA.id_rsa.pub"


(not tested)