---

- block:
  - name: Configure updatedb
    copy:
      src: updatedb.conf
      dest: /etc/updatedb.conf
      owner: root
      group: root
      mode: "0644"

  - name: Configure mlocate cron job
    template:
      src: mlocate.j2
      dest: /etc/cron.daily/mlocate
      owner: root
      group: root
      mode: "0755"

  # If we're keeping mlocate enabled, just delete old/orphaned temp
  # dbs to clear up space.
  # If mlocate is disabled, delete all the dbs.
  - name: Find mlocate.db files
    find:
      paths: /var/lib/mlocate
      patterns: "{{ 'mlocate.db.*' if enable_mlocate else 'mlocate.db*' }}"
      age: "{{ '7d' if enable_mlocate else '0' }}"
    register: mlocate_dbs

  - name: Delete mlocate.db files
    file:
      path: "{{ item.path }}"
      state: absent
    with_items: "{{ mlocate_dbs.files }}"

  # This is in case there are any long running mlocate processes that are still
  # scanning ceph volumes.
  # This can be removed anytime after 5/1/2018.
  # It's unlikely to kill a 'good' mlocate process, and even if it does, it
  # will recover the next time the cron job runs.
  - name: Kill any currently running mlocate
    command: "pkill -f /usr/bin/updatedb.mlocate"
    register: kill_result
    failed_when: kill_result.rc > 1
    changed_when: kill_result.rc == 0

  become: yes
