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

Answered By — mdaniel

Answer Checked By — Senaida (FixIt Volunteer)

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Learn more Skip package download if already installed

Leave a Reply