ansibleansible-facts

Override Ansible default package manager


Prior to Ansible 2.5, I was able to set override the default package manager used by Ansible's package module to use a different Ansible module. In a playbook this looked like:

tasks:
  - set_fact:
      ansible_pkg_mgr: pacaur

After upgrading to Ansible 2.5.3 setting the above no longer changes the package manager and package still tries to use the pacman module, the default for my OS.

How can I override the package manager fact?


Solution

  • When using the package module, you could use the use directive to specify which package manager you want to use. For example

    - package:
        name: spotify
        use: pacaur
    

    It is not recommended to override facts gathered by ansible, as it would lead to unexpected behaviour.

    To answer your question:

    From what I see this should not work, as ansible is doing a lookup to see if the specified module in the use directive (apt/yum/pacman) or the value from ansible_pkg_mgr exists or not.

    package is an action plugin which returns the os package manager based on the distribution / operating system and it requires the package manager to be implemented as an Ansible module. The implementation of the package plugin has been slightly changed between version 2.4 abd 2.5. You can refer to the code here. If the module is not available ansible would fail immediately.

    Update

    So, the reason why the override is not working anymore is because, with the 2.5 release all ansible facts were moved to ansible_facts.* namespace. The ansible_* namespace is still available but will be deprecated in a future release.

    When you look at the file linked above, you notice the following change

    In version 2.4

    module = self._templar.template('{{ansible_pkg_mgr}}')
    

    and in 2.5

    module = self._templar.template('{{ansible_facts.pkg_mgr}}')
    

    So, in version 2.5 the package plugin is getting the value of the fact (pkg_mgr) from ansible_facts.pkg_mgr so you need to set that fact to pacaur and not ansible_pkg_mgr. So your set fact, should be as below and your playbook would work as before.

    - set_fact:
        ansible_facts:
          pkg_mgr: pacaur