Sobes.tech
Senior

How to specify different package managers for different OSes (Debian/apt and RedHat/yum) in Ansible?

sobes.tech AI

Answer from AI

In Ansible, you can use conditional constructs (when) to select the appropriate package manager depending on the OS. For example, you can specify different modules or parameters for Debian (apt) and RedHat (yum):

- name: Install package on Debian
  apt:
    name: nginx
    state: present
  when: ansible_facts['os_family'] == 'Debian'

- name: Install package on RedHat
  yum:
    name: nginx
    state: present
  when: ansible_facts['os_family'] == 'RedHat'

You can also use variables and dynamically select the package manager, but this approach with conditions is the most clear and common.

How to specify different package managers for… - sobes.tech