Yum Package Management with Ansible
Ansible provides a powerful and flexible way to manage package installations and updates across your infrastructure. In this section, we'll explore how to use Ansible for Yum package management on CentOS/RHEL-based systems.
Yum Module in Ansible
Ansible's built-in yum
module allows you to perform various package management tasks, such as installing, updating, or removing packages. Here's an example playbook that installs the httpd
package:
---
- hosts: webservers
tasks:
- name: Install Apache
yum:
name: httpd
state: present
In this playbook, the yum
module is used to ensure that the httpd
package is installed on all hosts in the webservers
group.
Updating Packages
To update a package, you can use the state: latest
option in the yum
module:
---
- hosts: all
tasks:
- name: Update all packages
yum:
name: "*"
state: latest
This playbook will update all installed packages on the managed nodes to their latest versions.
Removing Packages
To remove a package, you can use the state: absent
option in the yum
module:
---
- hosts: webservers
tasks:
- name: Remove Apache
yum:
name: httpd
state: absent
This playbook will remove the httpd
package from all hosts in the webservers
group.
Managing Package Groups
Ansible also allows you to manage package groups using the yum
module. Here's an example of installing the "Development Tools" package group:
---
- hosts: all
tasks:
- name: Install Development Tools
yum:
name: "@Development Tools"
state: present
In this playbook, the @
symbol is used to specify that the package name refers to a package group.
Handling Package Dependencies
Ansible's yum
module automatically handles package dependencies, ensuring that all required dependencies are installed when installing a package.
---
- hosts: webservers
tasks:
- name: Install WordPress
yum:
name:
- httpd
- php
- mysql
state: present
This playbook will install the httpd
, php
, and mysql
packages, along with their dependencies, on all hosts in the webservers
group.