---
####################################################################
# WARNING: These are designed specifically for Ansible tests       #
# and should not be used as examples of how to write Ansible roles #
####################################################################

- name: Register system environment
  command: "{{ ansible_python.executable }} -c 'import os; print(dict(os.environ))'"
  register: sys_environment

- debug: var=sys_environment

- name: Default value for OpenSSL binary path
  set_fact:
    openssl_binary: openssl

- name: Include OS-specific variables
  include_vars: '{{ ansible_os_family }}.yml'
  when: not ansible_os_family == "Darwin"

- name: Install OpenSSL
  become: true
  package:
    name: '{{ openssl_package_name }}'
  when: not ansible_os_family == 'Darwin'

- name: Register openssl version (full)
  shell: "{{ openssl_binary }} version"
  register: openssl_version_full

- name: Show openssl version (full)
  debug:
    var: openssl_version_full.stdout_lines

- when: ansible_os_family == "Darwin" and "LibreSSL" in openssl_version_full.stdout
  # In case LibreSSL is installed on macOS, we need to install a more modern OpenSSL
  block:
    - name: MACOS | Find brew binary
      command: which brew
      register: brew_which

    - name: MACOS | Get owner of brew binary
      stat:
        path: "{{ brew_which.stdout }}"
      register: brew_stat

    - name: MACOS | Install openssl
      homebrew:
        name: openssl
        state: present
      become: yes
      become_user: "{{ brew_stat.stat.pw_name }}"

    - name: MACOS | Locale openssl binary
      command: brew --prefix openssl
      register: brew_openssl_prefix

    - name: MACOS | Point to OpenSSL binary
      set_fact:
        openssl_binary: "{{ brew_openssl_prefix.stdout }}/bin/openssl"

    - name: MACOS | Register openssl version (full)
      shell: "{{ openssl_binary }} version"
      register: openssl_version_full_again
      # We must use a different variable to prevent the 'when' condition of the surrounding block to fail

    - name: MACOS | Show openssl version (full)
      debug:
        var: openssl_version_full_again.stdout_lines

- name: Register openssl version
  shell: "{{ openssl_binary }}  version | cut -d' ' -f2"
  register: openssl_version

- when: ansible_facts.distribution ~ ansible_facts.distribution_major_version not in  ['CentOS6', 'RedHat6']
  block:
  - name: Install cryptography (Python 3)
    become: true
    package:
      name: '{{ cryptography_package_name_python3 }}'
    when: not ansible_os_family == 'Darwin' and ansible_python_version is version('3.0', '>=')

  - name: Install cryptography (Python 2)
    become: true
    package:
      name: '{{ cryptography_package_name }}'
    when: not ansible_os_family == 'Darwin' and ansible_python_version is version('3.0', '<')

  - name: Install cryptography (Darwin)
    become: true
    pip:
      name: cryptography>=3.3
      extra_args: "-c {{ remote_constraints }}"
    when: ansible_os_family == 'Darwin'

- name: Register cryptography version
  command: "{{ ansible_python.executable }} -c 'import cryptography; print(cryptography.__version__)'"
  register: cryptography_version

- name: Print default key sizes
  debug:
    msg: "Default RSA key size: {{ default_rsa_key_size }} (for certificates: {{ default_rsa_key_size_certifiates }})"
