diff --git a/tasks/Debian/main.yaml b/tasks/Debian/main.yaml index 0f77e73..4273655 100644 --- a/tasks/Debian/main.yaml +++ b/tasks/Debian/main.yaml @@ -16,25 +16,41 @@ update_cache: yes loop: "{{ consul_apt_repository }}" -- name: Check if Consul is installed - ansible.builtin.shell: dpkg-query -l {{ consul_package_name }} 2>&1 | grep {{ consul_version }} - ignore_errors: True - register: is_consul - changed_when: is_consul.rc != 0 - failed_when: False +- name: Gather packages + package_facts: + manager: auto + +- name: Set fact about Consul package + set_fact: + consul_installed_package: "{{ ansible_facts.packages[consul_package_name][0]['name'] }}" + when: consul_package_name in ansible_facts.packages + +- debug: + msg: "{{ ansible_facts.packages[consul_package_name][0]['version'] }}" + when: consul_installed_package is defined - name: Mask Consul before install ansible.builtin.systemd: name: "{{ consul_package_name }}" masked: yes - when: is_consul.rc != 0 + when: consul_package_name not in ansible_facts.packages or ansible_facts.packages[consul_package_name][0]['version'] != (consul_version + "-" + consul_version_build) - name: "Install {{ consul_package_name }}-{{ consul_version }}-{{ consul_version_build }}" apt: name: "{{ consul_package }}-{{ consul_version_build }}" update_cache: yes register: consul_setup - when: is_consul.rc != 0 + when: consul_package_name not in ansible_facts.packages + +- name: "Update {{ consul_package_name }}-{{ consul_version }}-{{ consul_version_build }}" + apt: + name: "{{ consul_package }}-{{ consul_version_build }}" + update_cache: yes + register: consul_update + notify: Restart consul + when: + - consul_package_name in ansible_facts.packages + - ansible_facts.packages[consul_package_name][0]['version'] != (consul_version + "-" + consul_version_build) - name: Fix "/etc/consul.d/consul.env" copy: diff --git a/vars/Debian.yaml b/vars/Debian.yaml index 3f995d7..e471aa2 100644 --- a/vars/Debian.yaml +++ b/vars/Debian.yaml @@ -7,5 +7,6 @@ consul_apt_repository: - repo: deb [arch=amd64] https://apt.releases.hashicorp.com {{ ansible_distribution_release }} main filename: hashicorp consul_version_build: "1" +consul_package_name_regex: "{{ consul_package_name }}-{{ consul_version }}-{{ consul_version_build }}" consul_ssl_update_ca_command: "update-ca-certificates --fresh" consul_ssl_ca_trust_dir: "/usr/local/share/ca-certificates" diff --git a/vars/RedHat.yaml b/vars/RedHat.yaml index ddbfb2c..d6ff1cc 100644 --- a/vars/RedHat.yaml +++ b/vars/RedHat.yaml @@ -1,3 +1,4 @@ consul_package: "{{ consul_package_name }}-{{ consul_version }}" +consul_package_name_regex: "{{ consul_package }}" consul_ssl_update_ca_command: "update-ca-trust extract" consul_ssl_ca_trust_dir: "/etc/pki/ca-trust/source/anchors"