---
- name: Create group conjur
  group:
    name: conjur
    state: present

- block:
  - name: Install "ca-certificates"
    package:
      name: ca-certificates
    retries: 10
    delay: 2

  - name: Place Conjur public SSL certificate
    copy:
      dest: "{{ conjur_ssl_certificate_path }}"
      content: "{{ conjur_ssl_certificate }}"
      mode: 0644

  - name: Symlink Conjur public SSL certificate into /etc/ssl/certs
    file:
      src: "{{ conjur_ssl_certificate_path }}"
      dest: /etc/ssl/certs/conjur.crt
      state: link
    register: cert_symlink

  - name: Install openssl-perl Package
    yum:
      name: openssl-perl
    when:
      ansible_os_family == 'RedHat'
    retries: 10
    delay: 2

  - name: Rehash certs
    command: 'c_rehash'
    when: cert_symlink.changed
  when: ssl_configuration

- name: Render /etc/conjur.conf
  template:
    src: templates/conjur.conf.j2
    dest: /etc/conjur.conf
    mode: 0644

- block:
  - name: Request identity from Conjur
    uri:
      url: "{{ conjur_appliance_url }}/host_factories/hosts"
      method: POST
      body: "id={{ conjur_host_name }}"
      headers:
        Authorization: Token token="{{ conjur_host_factory_token }}"
        Content-Type: "application/x-www-form-urlencoded"
      status_code: 201
      validate_certs: "{{ conjur_validate_certs }}"
    register: host_factory_response
    retries: 3
    delay: 10
    until: host_factory_response.status == 201

  - name: Place identity file /etc/conjur.identity
    template:
      src: templates/conjur.identity.j2
      dest: /etc/conjur.identity
      mode: 0640
      group: conjur
  when: not conjurized
