17

If I run apt, I can update the package cache:

apt:
  name: postgresql
  state: present
  update_cache: yes

I'm now trying to use the generic package command, but I don't see a way to do this.

package:
  name: postgresql
  state: present

Do I have to run an explicit command to run apt-get update, or can I do this using the package module?

Baptiste Mille-Mathias
  • 2,144
  • 4
  • 31
  • 37
Justin Blank
  • 1,768
  • 1
  • 15
  • 32
  • What is the problem to solve? – techraf Mar 03 '18 at 17:47
  • Not really sure what you're asking. I'm trying to install a package. On ubuntu, in my particular environment, I have to call apt-get update first, or it fails. I'd like to use `package` to minimize the distro specific commands I use. – Justin Blank Mar 03 '18 at 18:37
  • If you want to use `package` then fork it and patch. – techraf Mar 03 '18 at 18:50

4 Answers4

14

This is not possible.

The module package as of writing is just capable to handle package presence, so you have to use directly the package module to refresh the cache.

Baptiste Mille-Mathias
  • 2,144
  • 4
  • 31
  • 37
  • ok, but can you provide an example playbook that *does* meet OP's needs? Maybe one where the first task uses the `apt` module to update the cache (using `when` so it only executes on apt-based systems), then uses the `package` module to install? – Michael Altfield May 17 '21 at 11:26
6

You can't with the package module, unfortunately, but you can do a two-step where you update the cache first before running the rest of your playbook(s).

- hosts: all
  become: yes
  tasks:
  - name: Update Package Cache (apt/Ubuntu)
    tags: always
    apt:
      update_cache: yes
    changed_when: false
    when: ansible_distribution == "Ubuntu"

  - name: Update Package Cache (dnf/CentOS)
    tags: always
    dnf:
      update_cache: yes
    changed_when: false
    when: ansible_distribution == "CentOS"

  - name: Update Package Cache (yum/Amazon)
    tags: always
    yum:
      update_cache: yes
    changed_when: false
    when: ansible_distribution == "Amazon"
  • why is changed_when set to false? I guess it's difficult to determine if that really changed something, but wouldn't `true` for could always have changed something be better as default? Where is the advantage in false? – jan Jan 17 '23 at 13:12
5

Unfortunately, Ansible does not yet offer a generic solution.

However, the variable ansible_pkg_mgr provides reliable information about the installed package manager. In turn, you can use this information to call the specific Ansible package modules. Please find attached an example for all common package managers.

- hosts: all
  become: yes
  tasks:
    - name: update apt cache
      ansible.builtin.apt:
        update_cache: yes
      when: ansible_pkg_mgr == "apt"
    
    - name: update yum cache
      ansible.builtin.yum:
        update_cache: yes
      when: ansible_pkg_mgr == "yum"
    
    - name: update apk cache
      community.general.apk:
        update_cache: yes
      when: ansible_pkg_mgr == "apk"
    
    - name: update dnf cache
      ansible.builtin.dnf:
        update_cache: yes
      when: ansible_pkg_mgr == "dnf"
    
    - name: update zypper cache
      community.general.zypper:
        name: zypper
        update_cache: yes
      when: ansible_pkg_mgr == "zypper"
    
    - name: update pacman cache
      community.general.pacman:
        update_cache: yes
      when: ansible_pkg_mgr == "pacman"
Philipp Waller
  • 463
  • 1
  • 7
  • 13
3

Yes.

Simply include update_cache: yes in your call to the ansible.builtin.package module:

package:
  name: postgresql
  state: present
  update_cache: yes

This works because as of 2020, Ansible passes any additional arguments to the underlying package manager:

While all arguments will be passed to the underlying module, not all modules support the same arguments. This documentation only covers the minimum intersection of module arguments that all packaging modules support.

Source: Package Module Docs