Ansible: DevOps Made Easy

The problem is quite old: A large number of servers need to be set up or homogenized, ideally with minimal effort. This problem has been solved several times. So-called orchestration tools execute specific commands on multiple systems based on predefined instructions.

Examples of such tools are Puppet, Chef, or CFEngine. However, these representatives have one major disadvantage for me: they require a client to be installed on the remote side for execution.

This is where Ansible comes in. Ansible uses only an SSH connection to execute scripts or playbooks on the clients.

The best way to demonstrate the simplicity of Ansible is with a small example in which a list of packages is installed via apt. First, we create the basic structure:

mkdir ansible
cd ansible

Next, we need to create an inventory, which is a list of servers and groups on which the playbooks will be executed.

# ansible/inventory
[group1]
server1
server2
server3

In this example, we have a group called group1 containing servers 1-3.

Next, we need to create a playbook containing the instructions for Ansible:

# ansible/site.yml
---
- hosts: all                 # select servers
  become: yes                # execute as sudo
  tasks:                     # list of tasks
    - name: Install packages # name for the task
      apt:                   # use the APT module
        name: "{{ items }}"  # name of the package
        state: present       # status of the package
      with_items:            # loop with a list. fills {{ items }}
        - program1
        - program2
        - program3

This playbook installs the programs program1-3 via apt.

To run this playbook, we just need to execute:

ansible-playbook site.yml

If you haven’t set up SSH key authentication, you need to append the -k option. To authenticate with a user other than the currently logged-in user, use the -u <username> option.

That’s it. Ansible should now connect to all servers via SSH and install the listed packages. If the packages are already installed, Ansible will simply skip them.

Ansible now has a huge number of modules to choose from, allowing you to use Ansible in a wide variety of use cases.