#
# (c) Copyright 2018 SUSE LLC
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
---

- name: Copy SSH keys
  blockinfile:
    path: "{{ ansible_env.HOME }}/.ssh/authorized_keys"
    create: yes
    block: |
      {% for dict_item in ssh_pub_keys %}
      {% for key in dict_item['keys'] %}
      {{ key }}
      {% endfor %}
      {% endfor %}

- block:
    - name: Create local SSH key on deployer
      shell: ssh-keygen -t rsa -f "{{ ansible_env.HOME }}/.ssh/id_rsa" -N ""
      delegate_to: localhost
      args:
        creates: "{{ ansible_env.HOME }}/.ssh/id_rsa"

    - name: Copy local SSH key to deployer
      lineinfile:
        path: "{{ ansible_env.HOME }}/.ssh/authorized_keys"
        create: yes
        line: "{{ lookup('file', ansible_env.HOME + '/.ssh/id_rsa.pub') }}"
  when: local_ssh_key

#
# (c) Copyright 2018 SUSE LLC
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
---

- name: Copy SSH keys
  blockinfile:
    path: "{{ ansible_env.HOME }}/.ssh/authorized_keys"
    create: yes
    block: |
      {% for dict_item in ssh_pub_keys %}
      {% for key in dict_item['keys'] %}
      {{ key }}
      {% endfor %}
      {% endfor %}

- block:
    - name: Create local SSH key
      shell: ssh-keygen -t rsa -f "{{ lookup('env', 'HOME') }}/.ssh/id_rsa" -N ""
      delegate_to: localhost
      args:
        creates: "{{ lookup('env', 'HOME') }}/.ssh/id_rsa"

    - name: Copy local SSH key to deployer
      lineinfile:
        path: "{{ ansible_env.HOME }}/.ssh/authorized_keys"
        create: yes
        line: "{{ lookup('file', lookup('env', 'HOME') + '/.ssh/id_rsa.pub') }}"
  when: local_ssh_key

Create the local ssh key in the right directory
Create the local ssh key in the right directory

The local ssh key needs to be added to the home directory of the user
running the Ansible playbook.

Signed-off-by: Nicolas Bock <4ad6fd604400c7892c7a2cb53bf674987bcaa405@suse.com>
