{"id":6084,"date":"2025-02-11T04:14:45","date_gmt":"2025-02-11T04:14:45","guid":{"rendered":"https:\/\/linuxbuz.com\/?p=6084"},"modified":"2025-07-11T04:42:32","modified_gmt":"2025-07-11T04:42:32","slug":"ansible-yum-module-examples","status":"publish","type":"post","link":"https:\/\/linuxbuz.com\/devops\/ansible-yum-module-examples","title":{"rendered":"Ansible yum module: Install RHEL\/CentOS Packages"},"content":{"rendered":"<p style=\"text-align: justify;\">The <a href=\"https:\/\/docs.ansible.com\/ansible\/9\/collections\/ansible\/builtin\/yum_module.html\" target=\"_blank\" rel=\"noopener\"><strong>Ansible yum module<\/strong><\/a> manages packages on systems that use the YUM package manager, such as CentOS, RHEL, RockyLinux, and Fedora. This module allows administrators to install, update, remove, or verify packages on target systems. Using the yum module, you can ensure specific packages and their versions are installed or removed from your systems. You can also manage package groups and automatically handle package dependencies.<\/p>\n<p style=\"text-align: justify;\"><i>This guide explains installing and managing packages on RPM-based Linux distributions using the Ansible yum module.<\/i><\/p>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#001a66;border-radius:3px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#004d99;color:#FFFFFF;border-top-left-radius:1px;border-top-right-radius:1px\">\ud83d\udca1 Recommended Reading<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:1px;border-bottom-right-radius:1px\">\n\ud83d\udc49 <a href=\"https:\/\/linuxbuz.com\/linuxhowto\/how-to-get-free-cloud-vps-server-step-by-step-guide\" target=\"_blank\" rel=\"noopener\">How to Get a Free Cloud Server for Testing<\/a><br \/>\n<\/div><\/div>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/linuxbuz.com\/devops\/ansible-yum-module-examples\/#Basic_Syntax\" >Basic Syntax<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/linuxbuz.com\/devops\/ansible-yum-module-examples\/#Installing_a_Package\" >Installing a Package<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/linuxbuz.com\/devops\/ansible-yum-module-examples\/#Removing_a_Package\" >Removing a Package<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/linuxbuz.com\/devops\/ansible-yum-module-examples\/#Updating_a_Package\" >Updating a Package<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/linuxbuz.com\/devops\/ansible-yum-module-examples\/#Installing_Multiple_Packages\" >Installing Multiple Packages<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/linuxbuz.com\/devops\/ansible-yum-module-examples\/#Working_with_Package_Groups\" >Working with Package Groups<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/linuxbuz.com\/devops\/ansible-yum-module-examples\/#Using_Ansible_yum_repository_Module\" >Using Ansible yum_repository Module<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/linuxbuz.com\/devops\/ansible-yum-module-examples\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/linuxbuz.com\/devops\/ansible-yum-module-examples\/#FAQs\" >FAQs<\/a><\/li><\/ul><\/nav><\/div>\n\n<h2><span class=\"ez-toc-section\" id=\"Basic_Syntax\"><\/span>Basic Syntax<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">The basic syntax of the <strong>yum module<\/strong> is simple.<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto;\"><code>- name: Description of the task\r\n  yum:\r\n    name: Package-Name\r\n    state: State<\/code><\/pre>\n<p><strong>Explanation<\/strong>:<\/p>\n<ul>\n<li style=\"text-align: justify;\"><strong>name:<\/strong> This parameter specifies the name of the package to be managed.<\/li>\n<li style=\"text-align: justify;\"><strong>state:<\/strong> This parameter defines the desired state of the package (present, absent, latest, installed, etc.).<\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Installing_a_Package\"><\/span>Installing a Package<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">To install a package using the <strong>yum module,<\/strong> set the <strong>state<\/strong> parameter to <strong>present<\/strong> or <strong>installed.<\/strong><\/p>\n<p><strong>Example 1: Basic Package Installation<\/strong><\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto;\"><code>- name: Install httpd package\r\n  hosts: all\r\n  become: yes\r\n  tasks:\r\n    - name: Install httpd package\r\n      yum:\r\n        name: httpd\r\n        state: present<\/code><\/pre>\n<p>This playbook installs the <strong>httpd<\/strong> package on all hosts in the <a href=\"https:\/\/linuxbuz.com\/devops\/ansible-inventory-examples\" target=\"_blank\" rel=\"noopener\"><strong>inventory<\/strong><\/a>.<\/p>\n<p><strong>Example 2: Verify Package Installation<\/strong><\/p>\n<p>You can verify the package installation by checking its version or status.<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto;\"><code>- name: Verify httpd package installation\r\n  hosts: all\r\n  become: yes\r\n  tasks:\r\n    - name: Install httpd package\r\n      yum:\r\n        name: httpd\r\n        state: present\r\n\r\n    - name: Check httpd version\r\n      command: httpd -v\r\n      register: httpd_version\r\n\r\n    - name: Display httpd version\r\n      debug:\r\n        msg: \"Installed httpd version: {{ httpd_version.stdout }}\"<\/code><\/pre>\n<p style=\"text-align: justify;\">This playbook installs the <strong>httpd<\/strong> package if it&#8217;s not already installed, checks the installed version, and then displays the version information using a <a href=\"https:\/\/linuxbuz.com\/devops\/ansible-debug-module-examples\" target=\"_blank\" rel=\"noopener\"><strong>debug task<\/strong><\/a>.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Removing_a_Package\"><\/span>Removing a Package<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>To remove a package, set the <strong>state<\/strong> parameter to <strong>absent.<\/strong><\/p>\n<p><strong>Example 1: Basic Package Removal<\/strong><\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto;\"><code>- name: Remove httpd package\r\n  hosts: all\r\n  become: yes\r\n  tasks:\r\n    - name: Remove httpd package\r\n      yum:\r\n        name: httpd\r\n        state: absent<\/code><\/pre>\n<p>This playbook removes the <strong>httpd<\/strong> package from all hosts in the inventory.<\/p>\n<p><strong>Example 2: Ensure the Package is Removed<\/strong><\/p>\n<p>You can ensure a package is removed and verify it.<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto;\"><code>- name: Ensure httpd package is removed\r\n  hosts: all\r\n  become: yes\r\n  tasks:\r\n    - name: Remove httpd package\r\n      yum:\r\n        name: httpd\r\n        state: absent\r\n\r\n    - name: Verify httpd removal\r\n      command: rpm -q httpd\r\n      register: httpd_check\r\n      ignore_errors: yes\r\n\r\n    - name: Display httpd removal status\r\n      debug:\r\n        msg: \"httpd package removal status: {{ httpd_check.stdout }}\"<\/code><\/pre>\n<p style=\"text-align: justify;\">This playbook removes the <strong>httpd<\/strong> package from all hosts, verifies whether the package has been removed, and displays the removal status.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Updating_a_Package\"><\/span>Updating a Package<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>To update a package to the latest version, set the state parameter to <strong>latest.<\/strong><\/p>\n<p><strong>Example 1: Basic Package Update<\/strong><\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto;\"><code>- name: Update httpd package to the latest version\r\n  hosts: all\r\n  become: yes\r\n  tasks:\r\n    - name: Update httpd package to the latest version\r\n      yum:\r\n        name: httpd\r\n        state: latest<\/code><\/pre>\n<p style=\"text-align: justify;\">This playbook updates the <strong>httpd<\/strong> package to the latest version on all hosts.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Installing_Multiple_Packages\"><\/span>Installing Multiple Packages<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">You can manage multiple packages in a single task by listing them in the name parameter. This is useful when you need to ensure multiple packages are installed, removed, or updated simultaneously.<\/p>\n<p><strong>Example 1: Install Multiple Packages<\/strong><\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto;\"><code>- name: Install multiple packages\r\n  hosts: all\r\n  become: yes\r\n  tasks:\r\n    - name: Install multiple packages\r\n      yum:\r\n        name:\r\n          - httpd\r\n          - vim\r\n          - git\r\n        state: present<\/code><\/pre>\n<p>This playbook installs the <strong>httpd, vim,<\/strong> and <strong>git<\/strong> packages on all hosts.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Working_with_Package_Groups\"><\/span>Working with Package Groups<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">The <strong>yum module<\/strong> can also handle package groups, which are collections of packages grouped together for easier management. To install a package group, use the name parameter with the <strong>@<\/strong> prefix.<\/p>\n<p><strong>Example 1: Install a Package Group<\/strong><\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto;\"><code>- name: Install 'Development Tools' package group\r\n  hosts: all\r\n  become: yes\r\n  tasks:\r\n    - name: Install 'Development Tools' package group\r\n      yum:\r\n        name: \"@Development Tools\"\r\n        state: present<\/code><\/pre>\n<p>This playbook installs the <strong>Development Tools<\/strong> package group on all hosts.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Using_Ansible_yum_repository_Module\"><\/span>Using Ansible yum_repository Module<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">The Ansible <strong>yum_repository module<\/strong> manages YUM repository configurations on RPM-based systems. It allows you to define and manage custom repositories, such as adding new ones or configuring existing ones.<\/p>\n<p style=\"text-align: justify;\">Below is an <a href=\"https:\/\/linuxbuz.com\/devops\/ansible-playbooks-examples\" target=\"_blank\" rel=\"noopener\"><strong>example playbook<\/strong><\/a> that demonstrates how to add the Elasticsearch repository, import its GPG key, and install Elasticsearch on all target hosts.<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto;\"><code>---\r\n- name: Install Elasticsearch\r\n  hosts: all\r\n  become: yes\r\n  tasks:\r\n    - name: Add Elasticsearch repository\r\n      ansible.builtin.yum_repository:\r\n        name: elasticsearch\r\n        description: \"Elasticsearch repository\"\r\n        baseurl: \"https:\/\/artifacts.elastic.co\/packages\/7.x\/yum\"\r\n        gpgkey: \"https:\/\/artifacts.elastic.co\/GPG-KEY-elasticsearch\"\r\n        gpgcheck: yes\r\n        enabled: yes\r\n        repo_gpgcheck: yes\r\n\r\n    - name: Install Elasticsearch package\r\n      ansible.builtin.yum:\r\n        name: elasticsearch\r\n        state: present\r\n\r\n    - name: Start and enable Elasticsearch service\r\n      ansible.builtin.systemd:\r\n        name: elasticsearch\r\n        state: started\r\n        enabled: yes<\/code><\/pre>\n<p style=\"text-align: justify;\">This playbook configures all target hosts to use the Elasticsearch YUM repository by adding its URL and GPG key. It then installs the Elasticsearch package from this repository if it&#8217;s not already present on the system. Finally, it starts the Elasticsearch service and enables it to start automatically on system boot.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">The yum module in Ansible is a versatile tool for managing packages on RHEL, CentOS, and Rocky Linux systems. You can now effectively manage software packages and ensure your systems are up-to-date and secure.<\/p>\n<p>You can also <a href=\"https:\/\/linuxbuz.com\/devops\/ansible-yum-localinstall\" target=\"_blank\" rel=\"noopener\"><strong>install RPM packages from local files<\/strong><\/a>.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"FAQs\"><\/span>FAQs<span class=\"ez-toc-section-end\"><\/span><\/h2>\n        <section class=\"sc_fs_faq sc_card \">\n            <div>\n\t\t\t\t<h6>1. How do I handle failed installations with the yum module?<\/h6>                <div>\n\t\t\t\t\t                    <p>\n\t\t\t\t\t\tYou can use the <strong>ignore_errors: yes<\/strong> parameter in your task to continue playbook execution even if the yum module fails for a package installation.                    <\/p>\n                <\/div>\n            <\/div>\n        <\/section>\n\t\t        <section class=\"sc_fs_faq sc_card \">\n            <div>\n\t\t\t\t<h6>2. Can I disable a repository when using the yum module?<\/h6>                <div>\n\t\t\t\t\t                    <p>\n\t\t\t\t\t\tYes, you can use the <strong>disablerepo<\/strong> parameter to disable a repository during package installation.                    <\/p>\n                <\/div>\n            <\/div>\n        <\/section>\n\t\t        <section class=\"sc_fs_faq sc_card \">\n            <div>\n\t\t\t\t<h6>3. Is there a way to use yum with custom GPG keys?<\/h6>                <div>\n\t\t\t\t\t                    <p>\n\t\t\t\t\t\tYes, use the <strong>disable_gpg_check<\/strong> parameter to skip GPG checks.                    <\/p>\n                <\/div>\n            <\/div>\n        <\/section>\n\t\t\n<script type=\"application\/ld+json\">\n    {\n\t\t\"@context\": \"https:\/\/schema.org\",\n\t\t\"@type\": \"FAQPage\",\n\t\t\"mainEntity\": [\n\t\t\t\t{\n\t\t\t\t\"@type\": \"Question\",\n\t\t\t\t\"name\": \"1. How do I handle failed installations with the yum module?\",\n\t\t\t\t\"acceptedAnswer\": {\n\t\t\t\t\t\"@type\": \"Answer\",\n\t\t\t\t\t\"text\": \"You can use the <strong>ignore_errors: yes<\/strong> parameter in your task to continue playbook execution even if the yum module fails for a package installation.\"\n\t\t\t\t\t\t\t\t\t}\n\t\t\t}\n\t\t\t,\t\t\t\t{\n\t\t\t\t\"@type\": \"Question\",\n\t\t\t\t\"name\": \"2. Can I disable a repository when using the yum module?\",\n\t\t\t\t\"acceptedAnswer\": {\n\t\t\t\t\t\"@type\": \"Answer\",\n\t\t\t\t\t\"text\": \"Yes, you can use the <strong>disablerepo<\/strong> parameter to disable a repository during package installation.\"\n\t\t\t\t\t\t\t\t\t}\n\t\t\t}\n\t\t\t,\t\t\t\t{\n\t\t\t\t\"@type\": \"Question\",\n\t\t\t\t\"name\": \"3. Is there a way to use yum with custom GPG keys?\",\n\t\t\t\t\"acceptedAnswer\": {\n\t\t\t\t\t\"@type\": \"Answer\",\n\t\t\t\t\t\"text\": \"Yes, use the <strong>disable_gpg_check<\/strong> parameter to skip GPG checks.\"\n\t\t\t\t\t\t\t\t\t}\n\t\t\t}\n\t\t\t\t    ]\n}\n<\/script>\n\n","protected":false},"excerpt":{"rendered":"<p>The Ansible yum module manages packages on systems that use the YUM package manager, such as CentOS, RHEL, RockyLinux, and Fedora. This module allows administrators to install, update, remove, or &hellip; <\/p>\n","protected":false},"author":1,"featured_media":6085,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"two_page_speed":[],"footnotes":""},"categories":[828],"tags":[],"class_list":["post-6084","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/posts\/6084","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/comments?post=6084"}],"version-history":[{"count":3,"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/posts\/6084\/revisions"}],"predecessor-version":[{"id":6652,"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/posts\/6084\/revisions\/6652"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/media\/6085"}],"wp:attachment":[{"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/media?parent=6084"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/categories?post=6084"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/tags?post=6084"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}