Ansible has a specific module for managing the Yum packages. You can install, remove, upgrade or downgrade versions and many more by using this module.
The Yum module also requires two parameters for the primary command, like other package management modules in Ansible.
Installing a Package
Let's install the git package using the Yum module. Set the name parameter to "git" and the state parameter to "present".
If the package was not on the remote server, then the latest version will be installed.
And if the package was already installed on the remote server, then it will not be updated to a new version because the "state" is already "present".
NOTE: Both the "present" and "installed" parameters have the same behavior.
Installing the latest Version
If you want to install the newest version, then you can set the state parameter to "latest". It will install the newest package, whether the package is present or not.
Installing a Specific Version
Sometimes you want to install a particular version of the packages. You can do this by appending the version with the package name.
For example git-220.127.116.11-6.el7
Let's install the git package with version and release, 18.104.22.168-6.el7, on the remote server.
------ [root@rpm ~]# yum info git Installed Packages Name: git Arch: x86_64 Version: 22.214.171.124 Release: 6.el7
Installing Multiple Packages
If you want to install various packages, you can do this by using the "with_items" statement to loop through a list of the packages.
Let's execute three Yum packages such as MySQL, git, and httpd.
NOTE: The above code will not be executed as a single package installation in each loop instance. Instead, all the modules are installed in one go. This optimized form is the behavior since 1.9.2.
Update all Packages
You can update all the yum packages, like giving the command yum -y update. You can use the wildcard "*" in the name.
Also, you can use the exclude parameter so that some packages should not be upgraded. The following task will not update the git package.