94
OpenStack Vancouver Summit Learn you some Ansible for Great Good! Juergen Brendel (@brendelconsult) , David Lapsley (@devlaps) May 21, 2015

Learn you some Ansible for great good!

Embed Size (px)

Citation preview

Page 1: Learn you some Ansible for great good!

OpenStack Vancouver Summit

Learn you some Ansible for Great Good!

Juergen Brendel (@brendelconsult) , David Lapsley (@devlaps)

May 21, 2015

Page 2: Learn you some Ansible for great good!

Unified test and deployment environments

Page 3: Learn you some Ansible for great good!

Dev, Test, Deploy

Page 4: Learn you some Ansible for great good!

Dev, Test, Deploy

Why didn't we

catch this bug

in testing?

Page 5: Learn you some Ansible for great good!

Dev, Test, Deploy

Why didn't we

catch this bug

in testing?

We can't

reproduce the

issues.

Page 6: Learn you some Ansible for great good!

Dev, Test, Deploy

Why didn't we

catch this bug

in testing?

We can't

reproduce the

issues.

I don't have

access to our

test server.

Page 7: Learn you some Ansible for great good!

Dev, Test, Deploy

Why didn't we

catch this bug

in testing?

How do I setup

my

development

environment?

We can't

reproduce the

issues.

I don't have

access to our

test server.

Page 8: Learn you some Ansible for great good!

Dev, Test, Deploy

Why didn't we

catch this bug

in testing?

How do I setup

my

development

environment?

We can't

reproduce the

issues.

I don't have

access to our

test server.

“It works for me...”

(shrug)

Page 9: Learn you some Ansible for great good!

Dev, Test, Deploy

Why didn't we

catch this bug

in testing?

How do I setup

my

development

environment?

We can't

reproduce the

issues.

I don't have

access to our

test server.

“It works for me...”

(shrug)Wouldn't this be nice instead?

Single command: Dev environment created

Single command: Test environment created

Page 10: Learn you some Ansible for great good!

Summary

Page 11: Learn you some Ansible for great good!

• Configuration management background

• Ansible introduction

• Rise of APIs

• Unified test and deployment environments

• Demonstration

Page 12: Learn you some Ansible for great good!

Configuration Management Tools

Overview

Page 13: Learn you some Ansible for great good!

Configuring servers

How do you configure a server?

Page 14: Learn you some Ansible for great good!

Arcane

magic

Configuring servers

How do you configure a server?

Page 15: Learn you some Ansible for great good!

Arcane

magic

Configuring servers

How do you configure a server?

Manual

instructions

Page 16: Learn you some Ansible for great good!

Arcane

magic

Configuring servers

How do you configure a server?

Manual

instructions

Scripts

Page 17: Learn you some Ansible for great good!

Arcane

magic

Configuring servers

How do you configure a server?

Manual

instructions

Scripts

CM tools

Page 18: Learn you some Ansible for great good!

Arcane

magic

Configuring servers

How do you configure a server?

Manual

instructions

Scripts

CM tools

Automation!

Page 19: Learn you some Ansible for great good!

CM Tools

Describe the desired state

Page 20: Learn you some Ansible for great good!

Ensure all

system packages

are updated.

CM Tools

Describe the desired state

Page 21: Learn you some Ansible for great good!

Ensure all

system packages

are updated.

CM Tools

Describe the desired state

Ensure package

“apache” is

installed.

Page 22: Learn you some Ansible for great good!

Ensure all

system packages

are updated.

CM Tools

Describe the desired state

Ensure that user

“xyz” exists.

Ensure package

“apache” is

installed.

Page 23: Learn you some Ansible for great good!

Ensure all

system packages

are updated.

CM Tools

Describe the desired state

Ensure that user

“xyz” exists.

Ensure package

“apache” is

installed.Have latest

sources: Clone

repo, update if it

exists already.

Page 24: Learn you some Ansible for great good!

Ensure all

system packages

are updated.

CM Tools

Describe the desired state

Ensure that user

“xyz” exists.

Ensure package

“apache” is

installed.Have latest

sources: Clone

repo, update if it

exists already.

Ensure package

“postgres” v9.1 is

installed.

Page 25: Learn you some Ansible for great good!

Ensure all

system packages

are updated.

CM Tools

Describe the desired state

Ensure that user

“xyz” exists.

Ensure package

“apache” is

installed.Have latest

sources: Clone

repo, update if it

exists already.

Ensure package

“postgres” v9.1 is

installed. Ensure DB

“app_data” exists

with password

“****”.

Page 26: Learn you some Ansible for great good!

Configuration Management Tools

Varieties

Page 27: Learn you some Ansible for great good!

Puppet (2005)“powerful, feature-rich, enterprise-y”

Chef (2009)

CM Tools variety

Page 28: Learn you some Ansible for great good!

Puppet (2005)“powerful, feature-rich, enterprise-y”

Chef (2009)

Salt (2011)“simple, fast, good for most things”

Ansible (2012)

CM Tools variety

Page 29: Learn you some Ansible for great good!

Puppet (2005)“powerful, feature-rich, enterprise-y”

Chef (2009)

Salt (2011)“simple, fast, good for most things”

Ansible (2012)

Fabric (2011)“not really CMS tools”

Scripts

CM Tools variety

Page 30: Learn you some Ansible for great good!

Ansible Overview

Page 31: Learn you some Ansible for great good!

• “Orchestration engine” for CM and deployment

• Written in Python

• Uses YAML

• “Playbooks”

• Config specs or explicit commands

Ansible overview

Page 32: Learn you some Ansible for great good!

• Key Points:• No central configuration server

• No key management

• No agent to install on target machine

• Explicit order

Ansible simplicity

Page 33: Learn you some Ansible for great good!

• Key Points:• No central configuration server

• No key management

• No agent to install on target machine

• Explicit order

• Requirements:• Need SSH access (with key or password)

• Need Python installed on target machine

Ansible simplicity

Page 34: Learn you some Ansible for great good!

Ansible architecture

Server 1

Server 2Server 3

Server 4

Server 5

Your laptop

Page 35: Learn you some Ansible for great good!

Ansible Overview

Modules

Page 36: Learn you some Ansible for great good!

Modules

Hundreds of them. They know how to do stuff…

Command

Shell

Script

Page 37: Learn you some Ansible for great good!

Modules

Hundreds of them. They know how to do stuff…

Command

Shell

Script

Copy

Sync

Templates

Line ops

Page 38: Learn you some Ansible for great good!

Modules

Hundreds of them. They know how to do stuff…

Command

Shell

Script

Copy

Sync

Templates

Line ops

Install packages

Users and groups

Networking

Services

Page 39: Learn you some Ansible for great good!

Modules

Hundreds of them. They know how to do stuff…

Command

Shell

Script

Copy

Sync

Templates

Line ops

Install packages

Users and groups

Networking

ServicesRepositories

Message queues

Monitoring

Notification

Page 40: Learn you some Ansible for great good!

Modules

Hundreds of them. They know how to do stuff…

Command

Shell

Script

Copy

Sync

Templates

Line ops

Install packages

Users and groups

Networking

ServicesRepositories

Message queues

Monitoring

Notification

Web servers

Database servers

Cloud infra

Page 41: Learn you some Ansible for great good!

Ansible Overview

How does it

work?

Page 42: Learn you some Ansible for great good!

How does it work?

Laptop

Page 43: Learn you some Ansible for great good!

How does it work?

Python Module

“Install Apache”

Laptop

run playbook

Page 44: Learn you some Ansible for great good!

How does it work?

Python Module

“Install Apache”

Run ModuleLaptop

ssh

Page 45: Learn you some Ansible for great good!

How does it work?

Python Module

“Install Apache”

Run Module

(then delete)

Run ModuleLaptop

ssh

Page 46: Learn you some Ansible for great good!

How does it work?

Python Module

“Install Apache”

Run Module

(then delete)

Run ModuleLaptop

return results

Page 47: Learn you some Ansible for great good!

Ansible Overview

Details

Page 48: Learn you some Ansible for great good!

Inventory and groups

Define hosts, organized in groups

Page 49: Learn you some Ansible for great good!

Inventory and groups

Define hosts, organized in groups

by function

by location

by hosting provider

...

Page 50: Learn you some Ansible for great good!

[europe]

server1.somehoster.co.uk

server2.otherhoster.de

[north-america]

host-a.serverhost.com

host-b.serverhost.com

[frontend]

server1.somehoster.co.uk

host-b.serverhost.com

[backend]

server2.otherhoster.de

host-a.serverhost.com

Page 51: Learn you some Ansible for great good!

Adhoc commands

Single commands, applied to groups

Page 52: Learn you some Ansible for great good!

Adhoc commands

Single commands, applied to groups

$ ansible -i hosts europe –a “uname -a”

$ ansible -i hosts frontend -a “/sbin/reboot” -f 3

Page 53: Learn you some Ansible for great good!

• Tell Ansible what to do

Playbooks

Page 54: Learn you some Ansible for great good!

---

- hosts: frontend

sudo: yes

tasks:

- name: Update the system

apt: pkg=nginx state=latest

- name: Create the user account

user: name=appuser shell=/bin/bash state=present

- name: Copy files to remote user's home

copy: >

src=files/names.txt dst=/home/appuser

owner=appuser mode=0644

Page 55: Learn you some Ansible for great good!

• Provide input to Ansible templates

Variables

Page 56: Learn you some Ansible for great good!

---

- hosts: all

sudo: yes

vars:

username: appuser

tasks:

- name: Create the user account

user: >

name={{ username }}

shell=/bin/bash

state=present

Page 57: Learn you some Ansible for great good!

• Simple layout for arranging Ansible playbooks, variables, templates, metadata, etc.

Simple Project Layout

Page 58: Learn you some Ansible for great good!

/

my_hosts

group_vars/

all

frontend

backend

europe

north-america

site.yml

Page 59: Learn you some Ansible for great good!

• Best practices layout for arranging Ansible playbooks, variables, templates, metadata, etc.

• Better suited for larger projects

• More extensible

Best Practice Project Layout

Page 60: Learn you some Ansible for great good!

/

ansible.cfg

deploy_hosts

staging_hosts

group_vars/

all

frontend

backend

europe

north-america

host_vars/

server1.somehoster.co.uk

host-b.serverhost.com

site.yml

roles/

common/

tasks/

main.yml

handlers/

main.yml

templates/

sshd_config.j2

files/

my_script.sh

vars/

main.yml

web/

...

db/

...

Page 61: Learn you some Ansible for great good!

Playbooks with roles

---

- hosts: frontend

sudo: yes

roles:

- common

- web

Page 62: Learn you some Ansible for great good!

The rise and rise of APIs

Page 63: Learn you some Ansible for great good!

The rise and rise of APIs

APIs

Page 64: Learn you some Ansible for great good!

The rise and rise of APIs

APIs

Local

Page 65: Learn you some Ansible for great good!

The rise and rise of APIs

APIs

Local Infrastructure

Page 66: Learn you some Ansible for great good!

The rise and rise of APIs

APIs

Local Infrastructure Services

Page 67: Learn you some Ansible for great good!

The rise and rise of APIs

APIs

Local Infrastructure Services

Page 68: Learn you some Ansible for great good!

Ansible 'cloud' modules

Public cloud

OpenStack

Amazon AWS

Google Compute

Azure

Digital Ocean

Linode

Private cloud

• OpenStack

• Eucalyptus

• Vsphere

• Docker

• libvirt

Page 69: Learn you some Ansible for great good!

Example: AWS Modules

EC2 / infrastructure

• Instances

• Images

• VPCs

• Load balancers

Services

• S3

• Route 53

• Databases

• Cache

Page 70: Learn you some Ansible for great good!

• Create instances via AWS and OpenStack

Example: Create instances

Page 71: Learn you some Ansible for great good!

- name: Booting EC2 guests

ec2:

key_name: my-key

group: my-security-group

instance_type: t2.micro

image: ami-120abc90

region: us-east-1

count: 1

register: ec2results

Page 72: Learn you some Ansible for great good!

- name: Booting OpenStack guests

nova_compute:

state: present

login_username: "{{ openstack_username }}"

login_password: "{{ openstack_password }}"

login_tenant_name: "{{ openstack_tenantname }}"

name: "{{ cluster_id }}-{{ item }}"

image_id: "{{ openstack_image_id }}"

key_name: "{{ openstack_keyname }}"

wait_for: 60

flavor_id: "{{ openstack_flavor_id }}"

nics:

- net-id: "{{ openstack_internal_net_id }}”

register: openstack_guests

Page 73: Learn you some Ansible for great good!

• Add hosts to inventory

Example: Create instances

Page 74: Learn you some Ansible for great good!

- local_action:

module: ec2

key_name: my-key

group: my-security-group

instance_type: t2.micro

image: ami-120abc90

region: us-east-1

count: 3

register: ec2results

- local_action:

module: add_host

hostname: {{ item.public_ip }}

groupname: my-server-group

with_items: ec2results.instances

Page 75: Learn you some Ansible for great good!

Unified test and deployment environments

Vagrant

Page 76: Learn you some Ansible for great good!

Use Vagrant to spin-up VMs local (VirtualBox, VMware, etc.)

cloud (EC2)

Use Ansible as 'provisioner'

Make an inventory file with just your VM

Point at same playbook as before

Vagrant

Page 77: Learn you some Ansible for great good!

• Tells vagrant which VMs to construct

• How to construct them:• RAM

• Virtual CPUs

• Network interfaces (public, private, static, nat’d)

Vagrant config: Vagrantfile

Page 78: Learn you some Ansible for great good!

Vagrant.configure(2) do |config|

config.vm.box = "saucy64"

config.vm.box_url = "http://cloud-

images.ubuntu.com/vagrant/..."

config.vm.host_name = "myapp-test"

config.vm.network "private_network", ip: "192.168.1.2”

config.vm.provision "ansible" do |ansible|

ansible.playbook = "site.yml"

#ansible.verbose = "vvvv"

ansible.inventory_path = "vagrant_hosts"

ansible.host_key_checking= false

end

end

Page 79: Learn you some Ansible for great good!

• Specifies which VMs/Groups Ansible should run against

Inventory: Vagrant Hosts

Page 80: Learn you some Ansible for great good!

[vagrant]

vagrant_host

ansible_ssh_host=192.168.1.2

[frontend-hosts]

vagrant_host

[applayer-hosts]

vagrant_host

[backend-hosts]

vagrant_host

[db-access:children]

applayer-hosts

backend-hosts

[appserver-access:children]

frontend-hosts

applayer-hosts

Page 81: Learn you some Ansible for great good!

Vars: group_vars/vagrant

Variables that only apply to Vagrant instances

---

ansible_ssh_user: vagrant

Page 82: Learn you some Ansible for great good!

Create and configure VMs

$ vagrant up

...

$ vagrant provision

Page 83: Learn you some Ansible for great good!

Unified test and deployment environments

Cattle, not

pets!

Page 84: Learn you some Ansible for great good!

Desired development/deployment workflow

- Local unit tests

- Local provisioning with Vagrant + Ansible

- Integration tests

Local dev

and testing

Page 85: Learn you some Ansible for great good!

Desired development/deployment workflow

- Create/update cloud

staging servers with

Ansible

- Provision servers with

Ansible

Local dev

and testing

Cloud

testing

Page 86: Learn you some Ansible for great good!

Desired development/deployment workflow

- Create/update cloud production

servers with Ansible

- Provision servers with Ansible

Local dev

and testing

Cloud

testing

Cloud deployment

Page 87: Learn you some Ansible for great good!

Demo

Page 88: Learn you some Ansible for great good!

Dev Environment

Cacher (apt/pip)

MCP MHV1 MHV2

Ansible

Git cache

Page 89: Learn you some Ansible for great good!

• Same Ansible playbooks can be used to provision application locally or in the cloud

Key Takeaways

Page 90: Learn you some Ansible for great good!

• Same Ansible playbooks can be used to provision application locally or in the cloud

• With cloud APIs and Ansible modules (OpenStack, AWS, Rackspace, …) playbooks can also be used to provision infrastructure

Key Takeaways

Page 91: Learn you some Ansible for great good!

References

Page 92: Learn you some Ansible for great good!

• Questions: [email protected], [email protected]

• Ansible playbooks: http://bit.ly/devstack-ansible

• Ansible docs: http://docs.ansible.com/

• Ansible source: https://github.com/ansible/ansible

• Vagrant: http://www.vagrantup.com/

• Example project: http://bit.ly/ansible-devstack

Page 93: Learn you some Ansible for great good!

@brendelconsult, @devlaps

Thank You

Page 94: Learn you some Ansible for great good!