- name: create directories
  file:
    dest:  '{{ home_path }}/{{ item }}'
    mode:  0700
    state: directory
  with_items:
    - '.ssh'
    - '.ssh/config.d'
    - '.ssh/keys'

- name: copy common config
  copy:
    src:  '{{ item }}'
    dest: '{{ home_path }}/.ssh/config.d/'
    mode: 0600
  with_fileglob:
    - '_*'

- name: assemble config.d
  assemble:
    src:           '{{ home_path }}/.ssh/config.d'
    dest:          '{{ home_path }}/.ssh/config'
    mode:          0600
    ignore_hidden: yes
    delimiter:     "\n"

- name: create directories
  file:
    dest:  '{{ home_path }}/{{ item }}'
    mode:  0700
    state: directory
  with_items:
    - '.ssh'
    - '.ssh/config.d'
    - '.ssh/keys'

- name: copy common config
  copy:
    src:  '{{ item }}'
    dest: '{{ home_path }}/.ssh/config.d/'
    mode: 0600
  with_fileglob:
    - '_*'

- name: assemble config.d
  assemble:
    src:           '{{ home_path }}/.ssh/config.d'
    dest:          '{{ home_path }}/.ssh/config'
    mode:          0600
    ignore_hidden: yes
    delimiter:     "\n"

- name: check id_rsa
  stat:
    path: '{{ home_path }}/.ssh/id_rsa'
  register: id_rsa_stat

- block:
    - name: create id_rsa
      command:
        ssh-keygen \
          -q \
          -b 2048 \
          -t rsa \
          -N "" \
          -C "" \
          -f {{ home_path | quote }}/.ssh/id_rsa

    - name: set mode for id_rsa
      file:
        dest: '{{ home_path }}/.ssh/id_rsa'
        mode: 0600

    - name: create id_rsa
      command:
        ssh-add {{ home_path | quote }}/.ssh/id_rsa

  when: not id_rsa_stat.stat.exists

Create id_rsa if not exists
Create id_rsa if not exists
