Search code examples
ansibleaptdeb

Skip package download if already installed


I am using ansible to install a deb package and I do not want to download the remote file if the package is already installed. Currently I am doing like this:

- name: Check if elasticsearch installed
  become: true
  stat: path=/etc/elasticsearch/elasticsearch.yml
  register: st

- name: Install elasticsearch
  become: yes
  apt:
    deb: https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.6.12.deb
    update_cache: true
    state: present
  when: not st.stat.exists

Is there a better way to skip the download of the deb package if it is already installed?


Solution

  • You'll want package_facts or, of course, to just cheat and shell out something like command: dpkg --search elasticsearch

    - name: gather installed packages
      package_facts:
    - name: Install elasticsearch
      when: elasticsearch not in ansible_facts.packages
    

    Unless your question is about how to do that when elasticsearch might have been installed by hand, and not through dpkg, in which case your stat: and register: approach is a sensible one. You may even want to use a with_items: to check a few places the file might have been installed, depending on your circumstance