Ansible: Writing your first playbook

Let’s try to automate something and write our Ansible playbook. For our first example playbook, we’ll configure a server to run a web server using Nginx.

Inventory

To begin with, we would like to create an inventory and specify the hosts that we want to configure as web servers. Inventory files are in the .ini file format. Next, edit your playbooks/hosts file. And then put a [webservers] line above the dev-server and prod-server. This change indicates that dev-server and prod-server are in the webservers group.

~/playbooks/hosts
[webservers]
  dev-server:
    ansible_host=192.168.1.1 ansible_port=2222
  prod-server
    ansible_host=192.168.1.2 ansible_port=2222

The group webservers here define the scope. We are using IP addresses here. But you can use hostnames too. You can also change the standard port for SSH to 2222.

Ansible playbook

Let’s create our playbook using a collection of tasks:

Start a playbook

Our next stop is the main YAML file which is a playbook. We will start with — sign at the top of the book followed by the play’s name. Next, we will use reference to the webservers group from our inventory file. Finally, our automation flow will need root privileges (aka sudo). Thus, we are going to use the statement “become: True”. So, our file is going to look like this:

---
- name: Configure NGINX on the webserver 
  hosts: webservers
  become: True

Playbook task to install Nginx

We will write several tasks in the correct order. Please note that Ansible executes the tasks sequentially.

Let’s declare the tasks section and start with installing the Nginx. If you were to do it manually, you would execute the following command on a Linux shell:

sudo apt update
sudo apt install nginx

In the Ansible world, it will look like this:

  tasks:
  - name: install nginx
    apt: name=nginx update_cache=yes

Playbook task to configure Nginx

As a next step, we need to configure basic Nginx settings. The settings include the server port, HTML/CSS content location, index, and so on (nginx.conf). If you were to do it manually, you would probably use a text editor (vim or nano) and change the config in place. But, the easiest way to do it is by copying the config file to the respective folder.

  - name: copy nginx config file
    copy: src=files/nginx.conf dest=/etc/nginx/sites-available/default

Playbook task to create a Symlink

Next, let’s enable the file by creating a link from it to the sites-enabled directory. Nginx reads from the site-enabled directory during startup. In Linux shell the command would look like this:

sudo ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/

The command mentioned above will look like the YAML below:

  - name: enable configuration 
    file: >
    dest=/etc/nginx/sites-enabled/default
    src=/etc/nginx/sites-available/default state=link

Playbook task to add a webpage

The most miniature version of a website is a single HTML page. Let’s copy our primitive index.html to the Nginx destination path. Take note that the mode here is used to set the correct permissions using octal notation. It is equivalent to the Linux chmod command.

  - name: copy index.html
    template:src=your_templates/index.html.j2 >
    dest=/usr/share/nginx/html/index.html mode=0644

Playbook task to restart Nginx

Finally, you must restart the webserver once you have configured all the settings. Again, the Ansible module service is meant to solve it for us, and it is just as easy as if you were to execute service restart from the Linux shell.

  - name: restart nginx
    service: name=nginx state=restarted

The final Anible playbook

Let’s compile all the bits and pieces into a single Ansible playbook called websrv-provisoning.yml:

---
- name: Configure NGINX on the webserver 
  hosts: webservers
  become: True
  tasks:
  - name: install nginx
    apt: name=nginx update_cache=yes
  - name: copy nginx config file
    copy: src=files/nginx.conf dest=/etc/nginx/sites-available/default
  - name: enable configuration 
    file: >
    dest=/etc/nginx/sites-enabled/default
    src=/etc/nginx/sites-available/default state=link
  - name: copy index.html
template:src=your_templates/index.html.j2 >
dest=/usr/share/nginx/html/index.html mode=0644
  - name: restart nginx
    service: name=nginx state=restarted

Running your Ansible playbook

All we need to do now is to run the playbook and set up our web server:

ansible-playbook websrv-provisoning.yml

The output is going to look like this everything is configured correctly:

PLAY [Configure webservers with nginx] *******************************

GATHERING FACTS ******************************************************
ok: [dev-server]
ok: [prod-server]

TASK: [install nginx] ************************************************
changed: [dev-server]
changed: [prod-server]

TASK: [copy nginx config file] ***************************************
changed: [dev-server]
changed: [prod-server]

TASK: [enable configuration] *****************************************
ok: [dev-server]
ok: [prod-server]

TASK: [copy index.html] ********************************************* changed: [dev-server]
changed: [prod-server]

TASK: [restart nginx] ************************************************
changed: [dev-server]
changed: [prod-server]

PLAY RECAP *************************8*********************************
dev-server           : ok=6    changed=4    unreachable=0    failed=0
prod-server           : ok=6    changed=4    unreachable=0    failed=

Wrapping up

We have written a highly scalable yet simple playbook that installs NGINX on a Linux server and performs basic configuration. As a result, we can add our entire fleet to the inventory and watch automation at scale.

You may also be interested in

YouTube player

About Anto Online

Anto, a seasoned technologist with over two decades of experience, has traversed the tech landscape from Desktop Support Engineer to enterprise application consultant, specializing in AWS serverless technologies. He guides clients in leveraging serverless solutions while passionately exploring cutting-edge cloud concepts beyond his daily work. Anto's dedication to continuous learning, experimentation, and collaboration makes him a true inspiration, igniting others' interest in the transformative power of cloud computing.

View all posts by Anto Online

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.