Answers: <blockquote>
    <p>To append id_rsa.pub of a specific user from a remote ssh ServerA (no the controller machine ) to ServerB.</p>
  </blockquote>
  
  <p>1) Fetch the public keys from ServerA</p>
  
  <pre><code>- 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 }}"
  </code></pre>
  
  <p>2) Configure authorized keys at ServerB</p>
  
  <pre><code>- 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"
  </code></pre>
  
  <p>(not tested)</p>
  
Answers: CC BY-SA 4.0
Answers: 2019-04-05T11:29:01.620Z
Answers: 2019-04-05T11:29:01.620Z
Answers: Vladimir Botka
Answers: <pre><code>---
  # tasks file for passwordless
  - name: Play to setup Passowrdless
    remote_user: "{{ ssh_user }}"
    vars_prompt:
      - name: "ssh_user"
        prompt: "Please specify ssh user name"
        private: no
    hosts: all
    tasks:
    - name: Get pub key value from edgenode
      #hosts: edgenode
      command:
        cat $HOME/.ssh/id_rsa.pub
      register: pubkey
      changed_when: false
      when: "'group_edgenode' in  group_names"
  
    - name: Print copyout value
      debug:
        msg: "{{ pubkey.stdout }}"
      when: "'group_edgenode' in  group_names"
  
    - name: Append pub key value to datanodes
      #hosts: datanode
      lineinfile:
        path: $HOME/.ssh/authorized_keys
        #line: "{{ pubkey.stdout }}"
        #line: "{{ hostvars['192.168.0.103']['pubkey']['stdout'] }}"
        line: "{{ hostvars[item]['pubkey']['stdout'] }}"
        #line: "{{ groups['group_datanode'] | map('extract',hostvars, stdout) }}"
        state: present
        owner: "{{ ssh_user }}"
        #group: domain
        mode: 0600
        backup: yes
        insertafter: EOF
      #when: inventory_hostname in groups['datanode']
      with_items: "{{ groups['group_edgenode'] }}"
      when: "'group_datanode' in  group_names"
  </code></pre>
  
  <p>In this solution edgenodes are ServerA while datanodes are ServerB.
  Its tested and is working fine.</p>
  
Answers: CC BY-SA 4.0
Answers: 2019-04-05T12:03:54.993Z
Answers: 2019-04-10T07:25:35.303Z
Answers: 2019-04-10T07:25:35.303Z
Answers: AVS
Body: <p>How to use ansible authorized_key to authorize a ServerA (not the controller machine) to access Server B.</p>
  
  <p>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.</p>
  
  <p>In our case the ServerA count is 20 while ServerB count is 200.
  What i have tried so far and failed.</p>
  
  <pre><code>---
  # 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 }}"
  </code></pre>
  
Tags: <ansible><ansible-inventory><ansible-galaxy>
Title: Ansible authorized copy from remote source to remote destination
UserDisplayName: AVS
ThreadText: # Ansible authorized copy from remote source to remote destination
  
  (Asked by: username_0 on 2019-04-05)
  
  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 }}"
  
  ```
  
  **Tags:** ansible, ansible-inventory, ansible-galaxy
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2019-04-05. Score: 1
  
  > 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: <PUB_KEYS_DIR>
      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: <PUB_KEYS_DIR>
      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)
  
  
  ---
  
  ### Answer by username_0 on 2019-04-05. Score: 0
  
  ```
  ---
  # tasks file for passwordless
  - name: Play to setup Passowrdless
    remote_user: "{{ ssh_user }}"
    vars_prompt:
      - name: "ssh_user"
        prompt: "Please specify ssh user name"
        private: no
    hosts: all
    tasks:
    - name: Get pub key value from edgenode
      #hosts: edgenode
      command:
        cat $HOME/.ssh/id_rsa.pub
      register: pubkey
      changed_when: false
      when: "'group_edgenode' in  group_names"
  
    - name: Print copyout value
      debug:
        msg: "{{ pubkey.stdout }}"
      when: "'group_edgenode' in  group_names"
  
    - name: Append pub key value to datanodes
      #hosts: datanode
      lineinfile:
        path: $HOME/.ssh/authorized_keys
        #line: "{{ pubkey.stdout }}"
        #line: "{{ hostvars['192.168.0.103']['pubkey']['stdout'] }}"
        line: "{{ hostvars[item]['pubkey']['stdout'] }}"
        #line: "{{ groups['group_datanode'] | map('extract',hostvars, stdout) }}"
        state: present
        owner: "{{ ssh_user }}"
        #group: domain
        mode: 0600
        backup: yes
        insertafter: EOF
      #when: inventory_hostname in groups['datanode']
      with_items: "{{ groups['group_edgenode'] }}"
      when: "'group_datanode' in  group_names"
  
  ```
  
  In this solution edgenodes are ServerA while datanodes are ServerB.
  Its tested and is working fine.
  
  
  ---
  
  