12

I am a newbie in Ansible world. I have already created some playbook and I am getting more and more familiar with this technology by the day. In my playbooks I have always used the command yum to install and manage new packages, but recently I found out about an another command package that claims to be OS independent.

Thus my question: What is the difference between them?

In particular, if I create a role and a playbook that I know that will be executed in RHEL environment (where yum is the default package manager), which advantage do I get from using the command package rather than yum?

Thanks in advance for your help.

2 Answers 2

10

Ansible package module autodetect your OS default package manager (e.g yum, apt) from existing facts.

The fact environment variable which stores is "ansible_pkg_mgr".

Here is a command for same. ansible localhost -m setup | grep ansible_pkg_mgr.

If you are using multiple OS in your environment, then instead of specifying package manager you should use package over yum or apt.

1
  • 3
    is there any downside to package over a specific module (apt, yum, ...), or, in other words, "should you go for yum/dnf/apt if you are not in a multi-OS environment? If so, why exactly @deepak ?
    – vrms
    Oct 13, 2021 at 12:29
9

Ansible package module is more general but looks like you still have to handle differences in package names. From package module

# This uses a variable as this changes per distribution.
- name: remove the apache package
  package:
    name: "{{ apache }}"
    state: absent

In this case package name for:

  • RHEL - httpd
  • Debian/Ubuntu - apache2

so {{ apache }} variable must be set according to the OS.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.