Yum module in Ansible




Yum module in Ansible | Normally to install ,upgrade or remove a package we use yum command  in linux operating system .Similarly when we are going to install a package using Ansible we use yum module. Through this module we can install, upgrade and remove our required packages.

How to update all packages in ansible

To update all packages at a time we use ‘*’ symbol in the name parameter in Ansible and you must mention the state to latest to update all packages.
- hosts: node1
  tasks:
  - name: Upgrade all yum packages 
    yum:
      name: "*"
      state: latest

If you want to update all packages except some package you must specify ‘exclude’ parameter which contains which packages you don’t want to update.

In the below example we want to update all packages except php package.

- hosts: node1
  tasks:
  - name: Exclude some packages for upgrade
    yum:
      name: "*"
      state: latest
      exclude: php*

How to install a package in Ansible

You can install a package in Ansible using yum module.Under  this yum module there are two arguments one is name and another is state.
Name: name of the package which is to be installed.
State: state of the packages. By default state of the package set to present. when we are going to upgrade a package the state will be latest. when we are going to remove  a package the state will be absent.

For an example we install httpd package.


---
- hosts: node1
  tasks:
  - name: Install  package
    yum:
      name: httpd
      state: present

Install Package in Ansible
Install Package in Ansible



 How to install a specific version of package

To install specific version of package we have to mention the version of the the package in the name argument along with package name.

- hosts: node1
  tasks:
  - name: Install specific version of a package 
    yum:
      name: httpd<package version>
      state: present

How to install the latest version of package in Ansible

When we are going to install latest version of package we have to set the state to latest and in the name argument set the package name.
Name: name of the package which is to be installed.
State: state of the packages. By default state of the package set to present. when we are going to upgrade a package the state will be latest. when we are going to remove  a package the state will be absent.
For an example we install httpd package in latest version.
- hosts: node1
  tasks:
  - name: Install latest yum package
    yum:
      name: httpd
      state: latest

How to install multiple packages in ansible

To install multiple package we use with_item module and state should be present.
You can set the state to installed instead of present.The functionality of the both is same.

In the below example we install lamp stack


- hosts: node1
  tasks:
  - name: yum
    yum:
      name: "{{ item }}"
      state: present
    with_items:
    - httpd
    - php
    - mysql

How to delete a  package in Ansible

You can remove a package in Ansible using yum module.Under  this yum module there are two arguments one is name and another is state.
Name: name of the package which is to be installed.
State: state of the packages. By default state of the package set to present. when we are going to upgrade a package the state will be latest. when we are going to remove  a package the state will be absent.

For an example we remove httpd package.
---
- hosts: node1
  tasks:
  - name: Install  package
    yum:
      name: httpd
      state: absent

If this article is helpful to know about  Yum module in Ansible please share this article.




SHARE

Admin

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment