Playbook Example

  • In here I will examile a real use case example by creating and executing Ansible playbooks. I will install Nginx in all hosts and explore the different options for targeting OS variations with Ansible packages.

  • Use Ansible to install and configure project website, taking into account variances between the linux distributions of Nginx on both CentOS and Ubuntu.

  • Use Jinja2 templates to customise the website.

Let's first create our ansible config file ansible.cfg:

[defaults]
inventory = hosts
host_key_checking = False

Next our group vars for centos and ubuntu hosts:

group_vars/centos:

---
ansible_user: root
...

group_vars/ubuntu:

---
ansible_user: root
...

Next our host file hosts:

[control]
ubuntu-c

[centos]
centos[1:3]

[ubuntu]
ubuntu[1:3]

[linux:children]
centos
ubuntu

Next our playbook itself for installing EPEL on CentOS hosts:

Now we can execute the playbook:

Now let's install Nginx in all hosts by slightly modifying the playbook nginx_playbook.yaml:

Once we execute it, the output should be something similar to below:

or we can simply use Nginx module for that like below in nginx_playbook.yaml:

Next let's add a task to restart Nginx in our playbook:

Now let's had a handler to add a http health check to make sure Nginx is running:

Next let's create group vars for HTML location for Nginx in different hosts, and create a jinja template for HTML.

First let's nodify the playbook to deploy the HTML file inside Nginx:

Next our group vars for centos and ubuntu hosts:

group_vars/centos:

group_vars/ubuntu:

Now let's add the ansible_managed variable in the ansible.cfg:

Now we can use the ansible_managed variable inside any of our templates.

Now let's load out variables in the playbook.

Add unarchive task in the playbook to unarchive the code of our hidden game.

Last updated

Was this helpful?