---
- name: Get container facts
  become: true
  kolla_container_facts:
    action: get_containers
    container_engine: "{{ kolla_container_engine }}"
    name:
      - letsencrypt_webserver
  register: container_facts

- name: Checking free port for LetsEncrypt server
  vars:
    service: "{{ letsencrypt_services['letsencrypt-webserver'] }}"
  wait_for:
    host: "{{ api_interface_address }}"
    port: "{{ letsencrypt_webserver_port }}"
    connect_timeout: 1
    timeout: 1
    state: stopped
  when:
    - container_facts.containers['letsencrypt_webserver'] is not defined
    - service | service_enabled_and_mapped_to_host

- name: Validating letsencrypt email variable
  run_once: true
  vars:
    replace: "valid"
  assert:
    that: letsencrypt_email | regex_replace('.*@.*$', replace) == "valid"
    fail_msg: "Letsencrypt contact email value didn't pass validation."
  when:
    - enable_letsencrypt | bool
    - kolla_enable_tls_external | bool

- name: Validating letsencrypt EAB variables
  run_once: true
  assert:
    that:
      - letsencrypt_eab_key_id != ""
      - letsencrypt_eab_hmac != ""
    fail_msg: "Both letsencrypt_eab_key_id and letsencrypt_eab_hmac must be set when External account binding is turned on."
  when:
    - enable_letsencrypt | bool
    - letsencrypt_external_account_binding | bool
