Don't worry, each concept will be explained in more details further on.

Setting up the Environment

There are different ways to install Ansible. My preferred method is through PIP, the Python package manager. Other methods usually have outdated versions of Ansible or require complex installations.

If you don’t have PIP installed:

Install Ansible:

pip install ansible

Test it by running:

ansible --help

Inventory

By default, Ansible on Linux will look for the inventory file in: /etc/ansible/hosts

Next section will show how to change the location, so let's create it in the current directory:

[localhost]

127.0.0.1

Save it as: hosts

Like an INI file, [localhost] is a group and each line below is a host.

We are going to run our first playbook against localhost, as a test.

Config

To define a different location for your inventory file or pass special SSH parameters, there's a file for that: ansible.cfg

Ansible will look this file in the current directory, or in your home directory (named .ansible.cfg) and finally in /etc/ansible/ansible.cfg.

[defaults]

inventory = hosts

remote_user = ubuntu

host_key_checking = False

You can tell Ansible to look for the inventory file in any directory or in the current directory (as the example above).

Other settings:

remote_user = ubuntu

The default user that Ansible will use to SSH to your target hosts.

The default user that Ansible will use to SSH to your target hosts. host_key_checking = false

Avoids SSH asking for confirmation when logging in a host for the first time.

First Playbook

All setup and ready for your first playbook.

Let's take the example above and increment a little:

---

- hosts: all

connection: local

tasks:

- debug: msg="Hello World!"

Save it as: hello.yml

hosts: all

is telling Ansible to target all hosts in your inventory file. Another option would be hosts: localhost, since we have a group called localhost.

You cannot use hosts: 127.0.0.1, only groups.

is telling Ansible to target all hosts in your inventory file. Another option would be hosts: localhost, since we have a group called localhost. You use hosts: 127.0.0.1, only groups. connection: local

is just a hack to make Ansible run without SSH. Since the only host in our inventory is localhost, no SSH is needed in this point.

Format for tasks is:

- module_name: parameter1=value parameterX=value

The module used above (debug) is useful to print environment variables or simple messages during execution.

Running a Playbook

ansible-playbook hello.yml

Environmentally friendly green

This is the output of an Ansible playbook run.