Master Ansible: Using yum_repository, yum, and systemd Modules for Efficient Automation
This article explores three frequently used Ansible modules—yum_repository, yum, and systemd—detailing their parameters, usage examples, and practical commands to streamline package management and service control, helping DevOps engineers boost automation efficiency in cloud and container environments.
With the rise of cloud computing and container technologies, automated operations have become essential for IT infrastructure management. Ansible, a lightweight agentless automation tool, excels in deployment and configuration management.
yum_repository Module
The yum_repository module configures YUM repositories in Ansible, offering many options to add or remove repositories.
name (required) : The repository name part in the repo file.
file : Filename (without .repo) to store the repository; defaults to the name.
description : Human‑readable description, maps to the name attribute in the repo file.
baseurl : URL to download packages.
enabled : Whether to use the repository, default yes.
state : Repository file state, default present.
gpgcheck : Whether to perform GPG signature check (yes/no).
gpgkey : URL of the ASCII‑armored GPG key file.
Example to create a repo:
<code>ansible -i hosts -m yum_repository -a "name=epel file=test-epel description='Extra Packages for Enterprise Linux 7' baseurl=https://mirrors.aliyun.com/epel/7/x86_64/ state=present enabled=yes gpgcheck=no"</code>Example to delete the repo:
<code>ansible -i hosts -m yum_repository -a "name=epel file=test-epel state=absent"</code>yum Module
The yum module manages packages on RHEL/CentOS systems, similar to apt, supporting install, update, and removal.
name : Package name, optionally with version, path, or URL.
state : absent, installed, latest, present, removed (default none).
enablerepo : Repo ID to enable for the operation.
disablerepo : Repo ID to disable.
download_only : Download without installing, default no.
download_dir : Directory to store downloaded packages.
Examples:
<code># Install vim
ansible -i hosts -m yum -a "name=vim state=present"
# Download jq without installing
ansible -i hosts -m yum -a "name=jq-1.6-2.el7 state=present download_only=yes download_dir=/tmp/test"
# Install jq from epel repository
ansible -i hosts -m yum -a "name=jq-1.6-2.el7 state=present enablerepo=epel disablerepo=*"</code>systemd Module
The systemd module manages services on remote hosts, allowing start, stop, restart, or enable at boot.
name : Service name.
state : reloaded, restarted, started, stopped.
enabled : Whether the service should start on boot.
daemon_reload : Reload systemd manager configuration.
Examples:
<code># Reload systemd configuration
ansible -i hosts -m systemd -a "daemon_reload=yes"
# Start kubelet and enable at boot
ansible -i hosts -m systemd -a "name=kubelet state=started enabled=yes"</code>By mastering these modules, readers can enhance automation efficiency in daily operations.
Linux Ops Smart Journey
The operations journey never stops—pursuing excellence endlessly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.