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 ModuleFor 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: yesInstalling 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: yesUpdating 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: 3600Installing 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: presentRemoving Packages
Set
state: absent to uninstall:- name: Remove unwanted package
apt:
name: telnet
state: absentUsing 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: presentTwo Minute Drill
- Use the
packagemodule 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
