# This is needed for os_keypair as it uses public key
- name: Retrieve public key from private key
  command: "ssh-keygen -y -f {{ provision.key.file }}"
  register: public_key_content
  when: not provision.key.name

- name: Add keypair
  os_keypair:
      cloud: "{{ provision.cloud | default(omit) }}"
      name: "{{ prefix }}{{ provision.key.file | basename }}"
      public_key: "{{ public_key_content.stdout }}"
      state: present
  register: key
  when: not public_key_content|skipped

- name: register keypair
  vars:
      input:
          keypairs:
              # use default to avoid warnings on skip
              - "{{ key.key|default('') }}"
  include: register_resources_file.yml
  when: not public_key_content|skipped

# This is needed for os_keypair as it uses public key
# By loading the .pub file we allow use of crypted private keys.
- name: Retrieve public key from private key
  shell: |
    if [[ -f {{ provision.key.file }}.pub ]]; then
       cat {{ provision.key.file }}.pub
    else
       ssh-keygen -y -f {{ provision.key.file }}
    fi
  register: public_key_content
  when: not provision.key.name
  changed_when: false

- name: Add keypair
  os_keypair:
      cloud: "{{ provision.cloud | default(omit) }}"
      name: "{{ prefix }}{{ provision.key.file | basename }}"
      public_key: "{{ public_key_content.stdout }}"
      state: present
  register: key
  when: not public_key_content|skipped

- name: register keypair
  vars:
      input:
          keypairs:
              # use default to avoid warnings on skip
              - "{{ key.key|default('') }}"
  include: register_resources_file.yml
  when: not public_key_content|skipped

Use pub key when available
Use pub key when available

Allows use of crypted keys by reusing
existing pub keys.

Change-Id: Ie6d8447d6030f3a63fd3d4baff4f2817cd1cf006
