In this blog, we have explained how to write a playbook for the installation of epel-release, Nginx, and net-tools packages or Packages installation using Ansible playbook. There is a simple ansible structure for Ansible install epel-release, Nginx, net-tools packages.
There are a task folder and two files which are hosts and install-pkgs.yml files.
The hosts file is an inventory file and the install-pkgs.yml file is a playbook. In the install-pkgs.yml file, the package installation and the service enable, start code is written.

Topic covers in the Ansible packages installation using the Ansible playbook:

– Write the playbook for package installation
– Explain the modules and it’s parameters in the install-pkgs.yml
– Execute the playbook

Note:
Check the following link for Ansible First Playbook:
Ansible First Playbook
Check the following link to install ansible:
Install ansible
Check the following link for the Ansible basic command:
Ansible basic command
About YAML File
YAML File

Download the ansible-playbook from git.
# git clone https://github.com/smarttechfunda/ansible-playbook.git
Go to folder installation-packages which is under the ansible-playbook folder and run the following command.
# cd ansible-playbook/installation-packages
# ansible-playbook -i hosts install-pkgs.yml
OR
Follow the below manual method and understand how to write and run the ansible-playbook, which install the epel-release, Nginx, net-tools packages.

Write an ansible playbook and define the hosts and install-pkgs.yml files

Ansible Playbook Structure:

Run the following command to create the task folder:
# mkdir task
Run the following command to create the hosts and install-pkgs.yml (you can change the name of the install-pkgs.yml file to the main.yml file. In this example we are using the install-pkgs.yml name.) files in the task folder. Also, you can create a hosts file outside the task folder or inside the task folder.
# cd task
# touch hosts install-pkgs.yml

Copy and paste the code to hosts, install-pkgs.yml file.
OR
You can write the code of hosts and install-pkgs.yml file. It will help you to understand the structure of the host and install-pkgs.yml file.

task folder and hosts, install-pkgs.yml files:

task:
The task is the folder, in which hosts and install-pkgs.yml files created.
The task name denotes the task or steps to perform.

hosts:
The hosts file in an inventory of hosts group. There two hosts files. For the individual playbook hosts file and another is on path /etc/ansible/hosts file which is a global file for all playbook. In this example, we are using the individual playbook hosts file. The hosts file contains the IP, hostname, and different groups of hosts.
In this blog, we are installing the epel-release, nginx, and net-tools packages on the remote instance.
Check the following formate of hosts and install-pkgs.yml files.

hosts file:

Check the following code of the hosts file in the task folder. We used the remote IP and pkgs-host as the group name of the hosts. If you use the hostname, then it should be resolvable by DNS, OR if you use IP, then it should be reachable and configured passwordless ssh for hostname OR IP in both cases. For passwordless ssh check the automate-the-installation-of-ansible-on-centos-8 Step 6.

[pkgs-host]
192.168.0.204

install-pkgs.yml file:

main.yml or install-pkgs.yml:
The main.yml or install-pkgs.yml is a playbook, in which we can write the code or set of instructions to execute on any instance. This playbook installs epel-release, Nginx, and net-tools.
Check the following install-pkgs.yml file code in the task folder:

---
# This playbook install epel-release, nginx, net-tools Packages.
# Also, start and enable the nginx service.
# The epel-release package enable the third party 
# repository to install the nginx

# The pkgs-host is hosts group from hosts invetory file.
- hosts: pkgs-host
  # tasks to execute the modules.
  tasks:
    # Install the epel-release package.
  - name: Install epel-release
    # yum module.
    yum: 
       # epel-release package name.
       name: epel-release
    # epel_status is a variable name.
    register: epel_status
    # Print installation status of epel-release.
  - name: epel-release installation status
    # debug module.
    debug:
       # Print the following message and epel_status       
       # variable value.
        msg: "epel-release status: {{epel_status}}"

    # Install the nginx service.
  - name: Install nginx and start service
    # yum module.
    yum:
       # Package name and it's state which install the package.
       name: nginx
       state: present
    # status_nginx is a variable name.
    register: status_nginx

   # Print the insallation status of nginx.
  - name: Print the nginx status
    # debug module.
    debug:
       # Print the message and status_nginx variable value.
        msg: "Nginx status: {{status_nginx}}"

    # Start and enable the nginx service.
  - name: Start and enable the nginx service
   # service module.
    service:
      # Package name to enable service after booting the instance 
      # and state restarted the service after installation.
       name: nginx
       enabled: yes
       state: restarted
    # nginx_service_status variable name.
    register: nginx_service_status

    # Print the nginx service status.
  - name: Print the nginx service status
    # debug module.
    debug:
       # Print the message and nginx_service_status variable value.
        msg: "nginx service status: {{nginx_service_status}}"

    # Install the net-tools package.
  - name: Install net-tools
    # yum module.
    yum:
        # Package name to install.
        name: net-tools
    # net_tools_status variable name.   
    register: net_tools_status

   # Print the net-tools installation status.
  - name: Print the net-tools installation status
    # debug module.
    debug:
        # Print the message and net_tools_status variable value.
        msg: "net-tools status: {{net_tools_status}}"

Explain the modules and it’s parameters in the hosts and install-pkgs.yml files:

hosts:
In the main.yml or install-pkgs.yml file, the hosts is a keyword and its value is IP or hostname or group of IPs. The hosts file is an inventory file of a group of hosts.

tasks:
The tasks which execute the modules like debug, yum, service, etc.

name:
Lable of the task.

debug module:
The debug is a module to print messages during the execution for debugging the tasks. This module has a msg parameter key, which displays the messages. Also, a print variable value is declared in the register module.
In this example, we declare the following variables.

Package NameVariable Name
epel-release{{epel_staus}}
nginx{{status_nginx}}
nginx service{{nginx_service_status}}
net-tools{{net_tools_status}}
Packages and it’s variable names

yum module:
The yum module installs the provided package. The package name should be matched with the OS package name. The following command searches and display package from the yum repository.

# yum search nginx

nginx: Package name to search in the yum repository.

yum module paramters:

name:
Provide the package name like Nginx, net-tools, etc to install on remote install.
state:
The state parameter key provides the kind of instructions like install or present or latest ( These options install the packages or install the latest packages.) and absent or remove (These options remove the packages.) packages of the name provided to the “name:” parameter key.

Note: Some packages do not require the state parameter key like the net-tools package because they do not have services.

service module:

The service module reload, start, restart and stop the service after installation of service package.

service module paramters:

name:
Service names like Nginx, DHCP, tomcat, etc.
enabled:
After installation service package needs to enable or not service after booting the instance. The values of the enabled parameter key are yes or no.
State:
After installation, the service package state can be reloaded or started or restarted or stopped.

register module:
The register is a module to store the variable value in the playbook. Use the debug module to print variable values stores in the variable.

Execute the playbook

Run the following command to execute the Ansible playbook:
# cd task
# ansible-playbook -i hosts install-pkgs.yml

Command explanation:
Go to the task folder and run the ansible-playbook command to execute the playbook.

ansible-playbook:
The ansible-playbook is an ansible command to run the playbook.
-i:
The -i option is for the inventory file. Specify the inventory file path which is you are using an individual playbook hosts file or /etc/ansible/hosts file.
hosts:
The hosts file is an inventory file of hosts group.
install-pkgs.yml:
The install-pkgs.yml file is an ansible-playbook in which an ansible set of instructions or code is written.
Check the following output of the command. Rest output is truncated:

Packages Installation
Packages Installation

After installation of packages using the ansible, login to the remote host using ssh, and run the following commands.
# rpm -q epel-release nginx net-tools
To check the -q flag of the rpm command, run the following command.
# man rpm

Check the following output of the above command.

Package Installation Status
Package Installation Status

Run the following command to use the net-tools package.
# netstat -ntpl
To check the “-nplt” flag run the following command.
# man netstat

Netstat Command
Netstat Command