---
- name: Populate service facts
  service_facts:

- name: Mask alternative NTP clients to prevent conflicts
  vars:
    # NOTE(mgoddard): The service_facts module can return services that are not
    # present, possibly due to After/Before dependencies in other services.
    # These show up with a status of 'not-found'.
    service_exists: >-
      {{ item in ansible_facts.services and
         ansible_facts.services[item].status != 'not-found' }}
  systemd:
    name: "{{ item }}"
    enabled: "{{ 'false' if service_exists else omit }}"
    masked: true
    state: "{{ 'stopped' if service_exists else omit }}"
  become: true
  with_items: "{{ ntp_service_disable_list }}"

- name: Remove kolla-ansible installed chrony container
  shell:
    cmd: >
      if [[ -n $(docker ps -aq -f name=chrony) ]]; then
        docker rm -f -v chrony
      fi
    executable: "/bin/bash"
  register: result
  changed_when: result.stdout != ""
  become: true
  # NOTE(wszumski):  There is an ordering issue where on a fresh host, docker
  # will not have been configured, but if that is the case, the chrony container
  # can't possibly exist, but trying to execute this unconditionally will fail
  # with: No module named 'docker' as we have not yet added the docker package
  # to the kayobe target venv, so we use 'docker rm' command with force and
  # don't fail on error.
  when:
    - "'docker.service' in ansible_facts.services"
    - ansible_facts.services['docker.service'].status == 'running'
