Content and code for the Ansible workshop I conducted at SANOG 2017 in Gurgaon.
This tutorial is an attempt to explain Ansible to beginners by using practical examples.
-
Setup
-
Directory Structure
-
Example 1 - Hello World with
debug
module- Understanding plays and playbooks
- Understanding the inventory file
- Understanding host grouping using inventory file
-
Understanding
roles
- Example 2 - installing Java
- Example 3 - installing Elasticsearch
- Example 4 - installing the entire ELK stack
-
Understanding variables and configuring roles
- Example 5 - configuring the ELK stack with Nginx
- Example 6 - using facts to configure JVM for Elasticsearch
-
Writing your custom role
- Example 7 - write a role for Logstash forwarder
Preferrably Mac OS or Linux.
Note: Windows users can also use this but will have to figure out the relevant changes by themselves).
- Python 2.7 (comes installed on almost all Linux distros and Mac OS)
Other Python dependencies:
-
virtualenv
- not a requirement but its good to have it so that you don't mess up your global Python environment. Its super useful. -
ansible
If you are using
virtualenv
, then activate yourvirtualenv
.Install
ansible
like so:pip install ansible
pip is the Python package manager.
You will usually keep your Ansible code (playbooks, variables, roles) in one place. Occassionally, you will want to create separate repos for roles or completely separate repos for a system. These things depend on what your requirements are. You don't need to worry about them right now. You will know it when you need to.
For now, lets get started by creating a new repository:
# Create a new directory at a path of your choosing
mkdir ansible-tutorial-by-examples
cd ansible-tutorial-by-examples
# Create an empty git repository so that you can commit your Ansible code
git init
Our directory structure is going to look like this:
ansible-tutorial-by-examples
|- host_vars/
|- group_vars/
|- roles/
|- installed_roles/
|- inventory
|- ansible.cfg
|- first.yml
|- second.yml
|- ...
host_vars
andgroup_vars
- contain files with data in the form of variables that will be used by our Ansible scripts/playbooks.roles
- logical units of setting up a system.installed_roles
- like roles but has roles installed using ansible-galaxy.inventory
- contains information about your hosts and group of hosts.ansible.cfg
- the main configuration file for Ansible.*.yml
files - Ansible playbooks in most of the cases.
We will encounter these one-by-one in our tutorial as we go ahead. So don't bother about figuring out everything at this point.