Master Ansible Playbooks: Deploy Webpages and Automate Server Patching

What are Playbooks and How to Write Them?

  • A playbook is a configuration file where scripts are written.

  • It is a straightforward YAML file that looks like this:

Let's consider a simple example to print the date. Create a file named show_date.yml.

-
  name: Date playbook
  hosts: servers
  tasks:
    - name: Show date
      command: date

A playbook always begins with a “-”.

Lets run this playbook:

ansible-playbook show_date.yml
  • The output will appear as follows:

  • To check the output run below command:
ansible-playbook -v show_date.yml

  • Using a playbook file, you can perform various checks on servers.
mv show_date.yml show_server_stats.yml

show_server_stats.yml
-
  name: Date playbook
  hosts: servers
  tasks:
    - name: Show date
      command: date
    - name: Show RAM
      command: free -h
    - name: Show DISK
      command: df -h
    - name: Show uptime
      command: uptime
  • Run the playbook:
ansible-playbook -v show_server_stats.yml

Install docker on multiple servers:

  • Let's create a script to install Docker “install_docker.yml“ on three different machines from the master server.
-
  name: Install Docker
  hosts: servers
  become: yes
  tasks:
    - name: install docker based on server distribution
      apt:            #apt built_module of ansible. Refer document and search apt in it, you will get the code.
        name: docker.io
        state: latest
      when: ansible_distribution == 'Debian' or ansible_distribution == 'Ubuntu'

    - name: install the latest version of docker based on distribution
      dnf:
        name: docker
        state: latest
      when: ansible_distribution == 'Amazon' or ansible_distribution == 'RedHat'

Here’s a simple explanation of the provided Ansible playbook:

  1. - name: Install Docker
    This is the name of the playbook, describing its purpose (to install Docker).

  2. hosts: servers
    Specifies that the playbook will run on the hosts group named "servers."

  3. become: yes
    Enables privilege escalation (sudo) to perform tasks as a superuser.

  4. tasks:
    Declares the list of tasks to be executed in this playbook.

  5. - name: install docker based on server distribution
    Names the first task, which installs Docker on Debian or Ubuntu systems.

  6. apt:
    Uses the apt module to manage packages on Debian-based distributions.

  7. name: docker.io
    Specifies that the package to be installed is docker.io.

  8. state: latest
    Ensures the latest version of Docker is installed.

  9. when: ansible_distribution == 'Debian' or ansible_distribution == 'Ubuntu'
    Runs this task only if the server's operating system is Debian or Ubuntu.

  10. - name: install the latest version of docker based on distribution
    Names the second task, which installs Docker on Amazon Linux or Red Hat systems.

  11. dnf:
    Uses the dnf module to manage packages on Red Hat-based distributions.

  12. name: docker
    Specifies that the package to be installed is docker.

  13. state: latest
    Ensures the latest version of Docker is installed.

  14. when: ansible_distribution == 'Amazon' or ansible_distribution == 'RedHat'
    Runs this task only if the server's operating system is Amazon Linux or Red Hat.

  • You can refer to the Ansible Module documentation to write any ansible code.

  • To list the available details for ansible_distribution on the system, execute the following command:

ansible servers -m setup | grep "ansible_distribution"
  • Now we will run below command to install docker
ansible-playbook -v install_docker.yml

Deploy Webpage with Ansible on Multiple Servers

To deploy a project using Ansible, follow these steps:

First, visit my GitHub repository. You have the option to fork the repository or copy the code for index.html.

Then, create a file named deploy_webpage.yml to facilitate the deployment of the webpage.

-
  name: Deploy Webpage to nginx/apache
  become: yes
  hosts: servers
  tasks:
    - name: install nginx/apache
      apt:
        name: nginx
        state: latest
      when: ansible_distribution == 'Ubuntu'

    - name: start nginx/apache
      service:
        name: nginx
        enabled: yes
      when: ansible_distribution == 'Ubuntu'

    - name: copy webpage to server destination
      copy:
        src: index.html
        dest: /var/www/html
      when: ansible_distribution == 'Ubuntu'

To view the webpage on server1, execute the deploy_webpage.yml file.

ansible-playbook -v deploy_webpage.yml

To view the deployed webpage, obtain the public IP address of server1 of Ubuntu from AWS. Then, enter the following URL in your browser: http://public_ip_server1.

You will now see that our webpage has been successfully deployed.

Similarly we can deploy Webpage on other two servers Amazon Linux 2 and RedHat.

Add below code into deploy_webpage.yml file:

-
  name: Deploy Webpage to nginx/apache
  become: yes
  hosts: servers
  tasks:
    - name: install nginx/apache
      apt:
        name: nginx
        state: latest
      when: ansible_distribution == 'Ubuntu'

    - name: start nginx/apache
      service:
        name: nginx
        enabled: yes
      when: ansible_distribution == 'Ubuntu'

    - name: install nginx/apache
      dnf:
        name: httpd
        state: latest
      when: ansible_distribution == 'Amazon' or ansible_distribution == 'Redhat'

    - name: start nginx/apache
      service:
        name: httpd
        state: started
        enabled: yes
      when: ansible_distribution == 'Amazon' or ansible_distribution == 'Redhat'

    - name: copy webpage to server destination
      copy:
        src: index.html
        dest: /var/www/html

Now, copy the public IP addresses of the Amazon and RedHat servers. You will be able to see that your webpage has been successfully deployed.


Server Patching Playbook

Refer this documentation for update_cache:

To create a server patching playbook, begin by creating a file named: patch_servers.yml.

-
 name: Apply Patching Activity
 hosts: servers
 become: yes
 tasks:
   - name: Upgrade the servers
     apt:
       update_cache: yes
       upgrade: dist
     when: ansible_distribution == 'Ubuntu'

   - name: Upgrade the RHEL CentOS Packages
     dnf:
       name: "*"
       state: latest
     when: ansible_distribution in ['Amazon', 'Redhat']

   - name: Reboot the servers
     reboot:
       msg: "Rebooting servers: Patching activity in progress"
       reboot_timeout: 600

To execute this script, follow these steps:

ansible-playbook -v patch_servers.yml

This guide explains how to patch your servers using an Ansible Playbook.

0
Subscribe to my newsletter

Read articles from Chetan Mohanrao Mohod directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Chetan Mohanrao Mohod
Chetan Mohanrao Mohod

DevOps Engineer focused on automating workflows, optimizing infrastructure, and building scalable efficient solutions.