Introduction to ANSIBLE

by Xavi Soler

@xavi_xsb

WHO AM I ?

Automation!

Use Ansible for:

 

· Configuration Management

· Orchestration

· Resource Provisioning

· Application Deployment

It's Simple

You can understand all the basics

in hours, not days

It's Easy

So you can make something

in minutes, not hours

I'm not going to say that

Ansible is better than:

 

Puppet, Chef, CFengine

Salt, NixOps, Shell Scripts

Why do I like Ansible then?

Because it's so fucking simple

even you can understand

how it works

Agent-less

Push changes to servers

although it has a pull-based mode

Ansible Dependencies

SSH

Python

Modules

Control system resources, services, packages, files

or execute system commands

Modules

Write your own modules

in any language

Cloud: amazon, openstack, digitalocean, rackspace, docker
Database: mysql, postgres, redis, mongodb
Files: copy, template, file, lineinfile, replace
Monitoring: nagios, rollbar, zabbix, librato, pingdom, pagerduty, new relic
Notifications: irc, jabber, slack, hipchat, mail
Packaging: apt, rpm, pip, npm, gem, bower, compose, maven
Source Control: git, hg, svn
System: user, service, cron, modprobe, mount
Utilities: wait_for, pause, debug

Some modules that come by default

From the command line

From a Playbook

$ ansible web -i hosts -m apt -a "name=nginx state=present" -s
- hosts: web
  sudo: True
  tasks:
    - name: install nginx
      apt: name=nginx state=present

Playbooks

Playbooks are the Ansible language,

use them to configure, deploy, etc

Playbooks

Conditionals

- name: "shutdown Debian flavored systems"
  command: /sbin/shutdown -t now
  when: ansible_os_family == "Debian"

Playbooks

Loops

- name: add users
  user: name={{ item }} state=present groups=speakers
  with_items:
     - xavi
     - ignasi
     - josep

Playbooks

Variables

- hosts: web
  vars:
    owner: root
    group: root
    mode: 0644
  tasks:
    - copy: src=file.txt dest=/file.txt
            owner={{ owner }}
            group={{ group }}
            mode={{ mode }}

Roles

Roles allow us to organize:

 

tasks, variables, files,

templates, handlers

Roles

roles/loadbalancer/
├── files
│   └── haproxy
├── handlers
│   └── main.yml
├── tasks
│   └── main.yml
└── templates
    └── haproxy.cfg.j2

Inventory

[web]
web1.example.com
web2.example.com

[db]
db.example.com

[smtp]
mailer.example.com

Static inventory/hosts file

Inventory

Dynamic inventory is

generated on demand by a script.

 

Available: AWS EC2, Openstack Nova,

GCE, Digital Ocean, BSD Jails, Cobbler

Live Example

Configure 3 servers:

· 2 static webservers (nginx)

· 1 load balancer (haproxy)

All web servers should serve

an HTML with its hostname

The load balancer should do

round robin with all the web servers

ansible/
├── hosts
├── roles
│   ├── loadbalancer
│   │   ├── files
│   │   │   └── haproxy
│   │   ├── handlers
│   │   │   └── main.yml
│   │   ├── tasks
│   │   │   └── main.yml
│   │   └── templates
│   │       └── haproxy.cfg.j2
│   └── web
│       ├── tasks
│       │   └── main.yml
│       └── templates
│           └── index.html.j2
└── site.yml

Introduction to Ansible

By xsb

Introduction to Ansible

  • 1,863