Ansible has a specific module for managing yum packages. You can install, remove, upgrade or downgrade versions, etc. using this module.
As with other package management modules in Ansible, the yum module also requires two parameters for the primary command.
- Name – The package you want to install.
- State – what should be the state of the package after the task is completed; present or absent. By default, the value is ‘present.’ So if you do not give any value for this parameter, the package will be installed.
Installing a package
In the below task, I am trying to install the git package using the yum module. I have set the name parameter to ‘git’ and the state parameter to present.
- hosts: all tasks: - name: Install a yum package in Ansible example yum: name: git state: present
If the package was not on the remote server, then the latest version will be installed.
Note: If you had seen the documentation, you might have noticed two parameters, ‘present’ and ‘installed’. Both of them has the same behavior.
Note: If the package was already installed on the server, then it won’t be updated to a new version. This is because the ‘state’ is already ‘present.’ If you run the task in debug mode, you would see a message like below.
git-1.8.3.1-12.el7_4.x86_64 providing git is already installed
Installing the latest version
If you want to install the newest version always then you can set the state parameter to ‘latest.’ This will install the latest package whether the package is present or not.
The below task will install the latest version of git in every run.
- hosts: all tasks: - name: Install latest yum package example. yum: name: git state: latest
You would get such a message if the task were run in debug mode.
...Package git.x86_64 0:1.8.3.1-6.el7 will be updated...
Installing a specific version
Sometimes you may want to install a particular version of the packages. It is usually due to some dependencies.
You can do this by appending the version also with the package name.
<packagename>-<package version>
eg: git-1.8.3.1-6.el7
The below task tries to install the git package with version and release, 1.8.3.1-6.el7 on the remote server.
- hosts: all tasks: - name: Install specific version of a package in Ansible. yum: name: git-1.8.3.1-6.el7 state: present output ------ [root@rpm ~]# yum info git Installed Packages Name : git Arch : x86_64 Version : 1.8.3.1 Release : 6.el7
Now there are three possible scenarios in such cases.
- The package is not already installed on the system.
- This is fine. The package will be installed correctly every time.
- The package is already present installed, but the version you gave is more recent.
- The updated version will be installed.
- The package is already present installed, but the version you gave is an older one.
- In this case, it won’t install the downgrade version. But it won’t throw an error either. If you run the task in debug mode, you can see the following message.
Package matching git-1.8.3.1-6.el7.x86_64 already installed. Checking for update.\nNothing to do\n"
But if you are okay with the version being downgraded, you need to mention it explicitly. You can set a parameter ‘allow_downgrade’ to true. Note that this is only available from ‘2.4’.
Also, note that doing downgrade will have issues about the associated and dependent packages. The ansible yum documentation page has some related info on this.
Installing multiple packages
If you need to install various packages you don’t have to give it in separate tasks. You can use the ‘with_items’ statement to loop through a list of packages.
For example, in the below task, I am trying to execute three yum packages; git, MySQL, and httpd.
- hosts: all tasks: - name: yum yum: name: "{{ item }}" state: present with_items: - git - httpd - mysql
Note: As per the documentation, the above code won’t 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 also update all the yum packages, like giving the command yum -y update.
You just need to use the wildcard ‘*’ in the name.
- hosts: all tasks: - name: Upgrade all yum packages ansible. yum: name: "*" state: latest
You can also use the exclude parameter so that some packages should not be upgraded. The following task will not update the ‘git’ package.
- hosts: all tasks: - name: Exclude some packages from upgrade in Ansible. yum: name: "*" state: latest exclude: git*