Simple Ansible playbook for installing packages

ComputerCarriage > Posts > Ansible > Simple Ansible playbook for installing packages
ansible_pkg

For our repeated tasks we can create a playbook, using the playbook we can run multiple tasks. in this section we are going to see how to write a playbook for the package installation

In ansible module we are going to use is yum. Make sure you have added the inventory file  and ansible.cfg is configured.
In this section we are going to install a httpd package and startup the service at OS boot and start the httpd service. also configure the firewall rules for the http port to allow to browse over the network.

1. Create a playbook file httpd.yml

The in playbook we are going to install the package on the server1 node.

name: Install and Configure HTTPD Package
hosts: server1
tasks:
- name: Install the HTTPD Package
yum: name=httpd state=present
- name: Enable the http port in the firewalld
firewalld: service=http immediate=true permanent=true state=enabled
- name: Start the httpd service
service: name=httpd state=started enabled=yes
when: ansible_os_family == 'RedHat'

Note: Indentation is very much important while writing the yaml files.

The – name: Is similar to the echo command, we could see when the tasks are being executed.
Modules Used: yum, firewalld
name, state, service, state, enables are arguments.

Outcome of the playbook will be

  1. Playbook will install the package
  2. Enable the http port permanently with immediate effect
  3. Enable the service at startup and start the httpd service
  4. Used when condition for a particular OS family to install the package, We are going to install only the. OS family Redhat distribution like RHEL, CentOS, Fedora, for Debian we can use ansible_os_family == Debian and for packages use apt module

2. Syntax Check in ansible

Check the syntax before running the playbook, this will help us to debug the playbook before executing

[devops@control ansible]$ ansible-playbook --syntax-check httpd.yml
playbook: httpd.yml
[devops@control ansible]$

3. Dry run of the playbook

Before the final step of the execution of the playbook, we can do a dry run, this will help to see what will be the exact result

[devops@control ansible]$ ansible-playbook -C httpd.yml
PLAY [Install and Configure HTTPD Package] **
TASK [Gathering Facts]
ok: [server1]
TASK [Install the HTTPD Package]
changed: [server1]
TASK [Enable the http port in the firewalld]
changed: [server1]
TASK [Start the httpd service]
changed: [server1]
PLAY RECAP
server1 : ok=4 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[devops@control ansible]$

At the last we could see the changed=3, since we have three tasks in the playbook, now execute the playbook

4. Final Execution

If the dry-run is success we can go ahead and run the playbook

[devops@control ansible]$ ansible-playbook httpd.yml
PLAY [Install and Configure HTTPD Package] **
TASK [Gathering Facts]
ok: [server1]
TASK [Install the HTTPD Package]
changed: [server1]
TASK [Enable the http port in the firewalld]
changed: [server1]
TASK [Start the httpd service]
changed: [server1]
PLAY RECAP
server1 : ok=4 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[devops@control ansible]$

Now the package has been successfully installed on the node,

First task will gather facts of the server, we will discuss later on the Gathering facts, the actual first task from our playbook is Install the HTTPD Package, second task is to Enable the http port in the firewalld, final task is to start and enable the service at OS boot, all the package has been installed in the server1 node.

5. Validate the installation

Validate the httpd package has been installed by the following command

[devops@control ansible]$ curl http://server1
This is Test page by ansible automation , this is my server1.computercarriage.com on 192.168.43.28
[devops@control ansible]$

Hope you have understand the how to write playbook for the package installation, you can also use it for other package installations well

Fore more info check https://docs.ansible.com

Also please refer Ansible Installation and Configuration https://computercarriage.com/2020/06/07/install-ansible-command-line/

2 thoughts on “Simple Ansible playbook for installing packages

Leave a Reply