Skip to content

Ansible

Source: What is Ansible | Ansible Playbook explained | Ansible Tutorial for Beginners - YouTube

Tool to automate IT tasks

Open source GPL licensed

Save time, reduce errors, ensure consistency

  • Solve repetitive and distributed tasks:
    • Updates, backups, system reboots, user management
    • Manage multiple, distributed systemsm complex systems
  • For example, update Docker on 10 machines
  • Naive approach: Document through notes, then manually execute
  • 4 different ways Ansible helps:
    • Execute task from one machine
    • Config steps with YAML file(s)
    • Re-use same YAML files for different environments
    • Efficient, reliable and less errors

Ansible supports all infrastructure:

  • Operating systems, cloud, on-prem
    • Integrations vendor supported
  • Agentless, managed remotely from one machine - advantages:
    • No deployment or upgrade effort

Modules:

  • Small programs that do the work. They are sent to the target server. One specific tasks like:
    • Start docker container
    • Create/copy file
    • Start nginx service

There are modules to work with many systems, some examples:

  • Cloud
  • Commands
  • Database
  • Files
  • Network
  • Security
  • Source control
  • System

Simple to Understand becase of use of YAML

Section titled “Simple to Understand becase of use of YAML”

Ansible uses YAML, making it easier to understand and human readable.

  • Because modules do specific things, multiple modules will be required for complex tasks.

  • Sequential modules are grouped into tasks. For example:

    • Task: rename table, set owner of table, and truncase table.
    • Task: create directory, install nginx, start nginx
  1. Tasks Configuration

    • Hosts: where to run
    • Vars: variables
    • remoteuser: user to run as
    • Module name: what to run
    • Arguments: what to do

    Indentation is used in YAML to show hierarchy and is strict.

  2. Plays and Playbooks

    Play = combination of above: hosts, vars, remoteuser, tasks with modules and arguments. Should be named after the sequential tasks

    Playbook = 1 or more plays

    • How and in which order
    • At what time and where (which hosts)
    • What modules should be executed

    Example Playbook:

    hosts: webservers
    remote_user: root
    # Play for Webservers
    tasks:
    - name: create directory for nginx
    file:
    path: /etc/nginx
    state: directory
    - name: install nginx latest version
    apt:
    name: nginx
    state: latest
    - name: start nginx
    service:
    name: nginx
    state: started
    # Play for Database
    - hosts: databases
    remote_user: root
    tasks:
    - name: rename table
    mysql_db:
    name: old_table
    rename: new_table
    - name: set owner of table
    mysql_user:
    name: new_table
    priv: '*.*:ALL'
    - name: truncate table
    mysql_db:
    name: new_table
    state: import
    target: /tmp/new_table.sql
  3. Host Definition

    Set in Hosts File or Inventory = all machines involed in task executions

    • Includes IPs or hostnames
    • Can be grouped like webservers, databases
    • Can be any type of server (cloud, virtual, bare metal)

    For example:

    10.24.0.100
    [webservers]
    10.24.0.1
    web1.myserver.com
    [databases]
    10.24.1.4
    db1.myserver.com

Ansible with Docker and Other Environments

Section titled “Ansible with Docker and Other Environments”
  • Dockerfile to prepare application environment, for example config file, set log directory, env varaibles, app.jar, start script

Using Ansible, use the same configuration for the Docker container, but use it also on:

  • Docker / Vagrant container
  • Cloud instance
  • Bare metal

Ansible allows to reproduce the application in any environment

Ansible can also manage the container on the host level, for example with storage and network.

  • UI Dashboard from Redhat to centrally store and manage tasks, collaborate with other, manage inventories

Alternatives to Ansible like Puppet and Chef

Section titled “Alternatives to Ansible like Puppet and Chef”
AnsiblePuppet and ChefComment
YAMLRubyYAML is easier to learn
AgentlessAgent based, installationLess deployment required