- name: Disable firewalld before LDAP and Subsystem installation
  systemd: state=stopped name=firewalld
  when: ansible_distribution == "RedHat"

- name : Set hostname for machines Bydefault we choose pki1 for master and pki2 for clones.
  hostname: name=pki1.example.com
  tags: platform-ci
  when: ansible_connection != "docker"

- name: Install a required package for modify hostname task below
  dnf: pkg={{item}} state=latest
  with_items:
    - python3-libselinux
  when:
    - ansible_connection != "docker"
    - ansible_distribution == "Fedora"

- name : Modify hostname for master in  /etc/hosts
  lineinfile: dest=/etc/hosts regexp='.*{{ inventory_hostname }}$' create=yes insertafter=EOF line="{{ inventory_hostname }} {{ansible_fqdn}}" state=present
  tags: platform-ci
  when: ansible_connection != "docker"

- name: Modify hostname for clone in /etc/hosts
  lineinfile:
    path: /etc/hosts
    line: "{{groups['clone'][0]}} pki2.example.com"
    insertafter: EOF
  when: (clone == "True") or (clone == "true" and groups.clone is defined)

- name: Add 127.0.0.1 mapping to master hostname in /etc/hosts for 389ds
  lineinfile: dest=/etc/hosts create=yes insertafter=ROF line="127.0.0.1 {{ansible_fqdn}}" state=present
  when: (ansible_connection != "docker") and (clone != "true")

- name: check the hostname/ip for the remote instance
  set_fact: inventory_ip={{ inventory_hostname }}
  when: ansible_connection != "docker"

- name: Check for private ip if exist for openstack instance
  shell: hostname -I
  register: private_addr
  when: ansible_connection != "docker"

- name: Set the private ip in a variable
  set_fact: private_ip={{ private_addr.stdout }}
  when: ansible_connection != "docker"

- name: Change in /etc/hosts to set right private/public ip for instance to be reachable
  replace: path=/etc/hosts regexp={{ inventory_ip }} replace={{ private_ip }}
  when:
    - ansible_connection != "docker"
    - inventory_ip not in private_ip

- name: pip install pexpect
  shell: pip3 install pexpect

- name : fetch file in  master in  /etc/hosts
  fetch: src=/etc/hosts dest=/etc/ flat=yes validate_checksum=no
  tags: platform-ci
  when: ansible_connection != "docker"

- name: Install list of packages for CS Master for Redhat
  yum : pkg={{item}} state=latest
  with_items:
    - redhat-pki
    - redhat-pki-console-theme
    - redhat-pki-server-theme
    - pki-console
    - 389-ds-base
    - pki-ca
    - pki-kra
    - pki-ocsp
    - pki-tks
    - pki-tps
    - policycoreutils-python
    - expect
    - libselinux-python
  when: ansible_distribution == "RedHat"
  tags: platform-ci

- name: Install list of packages for CS Master for Fedora
  dnf : pkg={{item}} state=latest
  with_items:
    - 389-ds-base
    - dogtag-pki
    - dogtag-pki-console-theme
    - dogtag-pki-server-theme
    - policycoreutils-python-utils
    - expect
  when: ansible_distribution == "Fedora"
  tags: platform-ci

- name: Check for Removed dependency from mod_revocator and mod_nss.If failes refer BZ 1295276 
  command: rpm -q {{item}}
  with_items:
    - mod_revocator
    - mod_nss
  register: rpm_check
  failed_when: "rpm_check.rc == 0"

- name: Check for Removed dependency of perl from pki-server.If fails, refer BZ 1305769
  command: rpm -qR pki-server | grep perl
  register: rpm_check
  failed_when: "rpm_check.rc == 0"

- name: Copying templates to /tmp folder
  copy : src=/tmp/test_dir  dest=/tmp/
  tags: platform-ci

- name: Making constants.py file compatable for including as vars.
  shell: sed -e "s/ =/:/g;s/'//g" /tmp/test_dir/constants.py > /tmp/test_dir/constants.yml

- name: Fetch the file
  fetch: src=/tmp/test_dir/constants.yml dest=/tmp/test_dir/ flat=yes validate_checksum=no

- name: Fips mode setup
  block:
    - name: Install fips-mode-setup to enable fips mode in Fedora
      dnf:
        name: fips-mode-setup
        state: latest

    - name: Enable FIPS
      command: "fips-mode-setup --enable"

    - name: restart machine
      shell: 'sleep 2 && shutdown -r now "Ansible updates triggered"'
      async: 1
      poll: 0
      ignore_errors: true

    - name: show inv hostname
      debug:
        msg: "connecting to {{inventory_hostname }}"

    - name: waiting for server to come back
      local_action: wait_for
        host="{{ inventory_hostname }}"
        port=22
        delay=10
        timeout=600

    - name: checking Server is up
      command: "fips-mode-setup --check"
      register: command_output

    - debug:
        var: command_output
  when: fips_enable == "true" or fips_enable == "True"

- name: Install podman packages for Redhat
  dnf: pkg={{item}} state=latest
  with_items:
     - podman
  when: topology == "podman_acme" and deploy == "true"
  tags: platform-ci

- name: Install certbot for acme
  command: pip-3 install {{item}}
  with_items:
    - cryptography --upgrade
    - certbot
  tags: platform-ci
  when: (topology == "acme") or (topology == "podman_acme" and deploy == "true")
