---
# file: roles/ssh-keygen-and-store/tasks/from_server_to_hashicorp_vault.yml

- name: "trying to patch hashicorp vault"
  delegate_to: localhost
  become: no
  command: 'vault kv patch talas-kv/{{ host_vars_location }}/{{ ansible_hostname }} ssh_key_ed25519_{{ item[0].item }}="{{ item[1].stdout }}" ssh_key_ed25519_{{ item[0].item }}_pub="{{ item[2].stdout }}"'
  register: result
  ignore_errors: True
  tags: ssh-keygen

- name: "patch failed because the entry doesn't exist, creating it instead"
  delegate_to: localhost
  become: no
  command: 'vault kv put talas-kv/{{ host_vars_location }}/{{ ansible_hostname }} ssh_key_ed25519_{{ item[0].item }}="{{ item[1].stdout }}" ssh_key_ed25519_{{ item[0].item }}_pub="{{ item[2].stdout }}"'
  when:
    - result.failed
    - '"No value found" in result.stderr'
  tags: ssh-keygen
