Loading

Quipoin Menu

Learn • Practice • Grow

ansible / Installing Packages
tutorial

Installing Packages

Installing software packages is a core automation task. Ansible provides package modules for all major systems: apt (Debian/Ubuntu), yum (RHEL/CentOS), dnf (Fedora), pacman (Arch), and homebrew (macOS).

Using the Generic package Module

For cross‑platform playbooks, use the package module. It automatically selects the correct package manager based on the OS.
- name: Install nginx on any Linux
package:
name: nginx
state: present
become: yes

Installing Multiple Packages

Use a loop or pass a list to the name parameter (list form is more efficient):
- name: Install common utilities
package:
name:
- git
- curl
- vim
- htop
state: present
become: yes

Updating Package Cache (apt)

For apt‑based systems, update the cache before installing:
- name: Update apt cache and install python3
apt:
name: python3
update_cache: yes
cache_valid_time: 3600

Installing a Specific Version

For apt, use name=package=version; for yum, use name=package-version.
- name: Install specific nginx version
apt:
name: nginx=1.18.0-0ubuntu1
state: present

Removing Packages

Set state: absent to uninstall:
- name: Remove unwanted package
apt:
name: telnet
state: absent

Using Conditionals for OS‑Specific Packages

When packages have different names across OS families:
- name: Install httpd on RedHat, nginx on Debian
package:
name: "{{ 'httpd' if ansible_os_family == 'RedHat' else 'nginx' }}"
state: present


Two Minute Drill
  • Use the package module for cross‑platform installations.
  • Install multiple packages using a list in name.
  • Update cache with update_cache: yes (apt).
  • Remove packages with state: absent.

Need more clarification?

Drop us an email at career@quipoinfotech.com