go faster with ansible

34
Go Faster with Ansible Richard Donkin @ rdonkin

Upload: richard-donkin

Post on 18-Jan-2017

110 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Go Faster with Ansible

Go Faster with AnsibleRichard Donkin

@rdonkin

Page 2: Go Faster with Ansible

$ whoami• Richard Donkin• DevOps engineer / contractor• Ansible, Vagrant, Docker, Linux,

AWS, databases, …

• Experience of sys admin, DevOps, backend dev, architecture, startups, …

@rdonkin linkedin.com/in/rdonkin

Page 3: Go Faster with Ansible

Goal: Faster Correct Setup

ServersAppsDevelopers

• Correct configuration• No snowflake servers• Dev, Test, Production

Page 4: Go Faster with Ansible

Infrastructure As

Code

Page 5: Go Faster with Ansible

Infrastructure as Code

Software processes for server configs:• Code review• Version control• Automated tests• Automated push to servers

Page 6: Go Faster with Ansible

What is Configuration Management?

Code that Controls Config

Page 7: Go Faster with Ansible

What Ansible DoesConfiguration Management

App Deployment• Basic, "Atomic", zero downtime, ...

Orchestration• Sequence operations on servers,

APIs, etc.• Harder with Configuration

Management

Page 8: Go Faster with Ansible

Why Ansible?• Agentless & serverless• Simple• Sequential• Declarative tasks• “Ensure nginx installed” • Enables idempotence

• Easy to learn• Scales to complex cloud

orchestration

Page 9: Go Faster with Ansible

Quick InstallMac: brew install ansible

Debian/Ubuntu:sudo apt-add-repository ppa:ansible/ansiblesudo apt-get install -y ansible

RHEL/CentOS: use EPEL, then: sudo yum –y install ansible

Any Linux (latest Ansible, requires Python):sudo easy_install pipsudo pip install ansible

Page 10: Go Faster with Ansible

Hello PHPMailerPlaybooks

TasksIdempotence

Page 11: Go Faster with Ansible

Key ConceptsPlaybook = series of tasks• Targets one server or thousands• Servers defined by Inventory

Task = "ensure X is done" action

Play = set of tasks in playbook

Page 12: Go Faster with Ansible

Running a Playbook (1)

Page 13: Go Faster with Ansible

Running a Playbook (2)

Tasks will "skip" if state already OK

(Idempotence)

Page 14: Go Faster with Ansible

Writing a PlaybookPlay – hosts to process, become == sudoTasks - descriptive name- invoke module (apt) with parameters Play

Task

Page 15: Go Faster with Ansible

The Secret Life of Tasks

Each task runs SSH commands that • Upload a Module (e.g. apt)• Run module with task's

parameters• Return JSON output

Page 16: Go Faster with Ansible

Inventory and Variables

Group your servers and assign key parameters ("variables")

[web]10.0.1.5110.0.1.52

[db]10.0.1.61

[web:vars]ansible_port=2222

$ ansible-playbook -i prod apache.yml --limit web

Run different Ansible code per groupRecommended: • Inventory file per environment (or dynamic

inventory)• Put vars in group_vars/mygroup/vars.yml

Page 17: Go Faster with Ansible

Apache Playbook (1)

Vars = parameters for this playbookCan be in separate include filesOr attach to hosts or host-groups in Inventory - e.g. Listening IP address should be in inventory

Page 18: Go Faster with Ansible

Apache Playbook (2)

template task runs Jinja2 on local file and copies to servernotify sends event to Handler- Each Handler runs just once, at end of whole

playbook- Restart a service, notify Slack, ...

Page 19: Go Faster with Ansible

Apache Playbook (3)

service task uses systemctl to enable start on boot- {{ apache_service }} instantiates var with Jinja2Handler restarts apache at end if any task does a notify

Page 20: Go Faster with Ansible

Apache PlaybookPlaybooksVariablesHandlers

Page 21: Go Faster with Ansible

Modules (1)Over 840 modules "in the box"- Git, yum, apt, compose, pear, pip, …- Copy files, template files- Edit files- Permissions, ownership, SELinux- Services – systemd, sysvinit, ...- Crontabs- MySQL, PostgreSQL, MongoDB, ...

Page 22: Go Faster with Ansible

Modules (2)More modules (AWS alone has 87)- Firewalls, routers, switches, ...- AWS, Google, Digital Ocean, ...- Docker, VMware, …- Fallback to shell, upload script, …

Runs best on Linux/Unix including MacWindows as a target only

Page 23: Go Faster with Ansible

Roles

"Modularised playbooks"- Split playbook into folder per type of

content- defaults folder for "parameter vars"- vars folder for "role vars" – hard to

override- meta folder for role dependencies

Vars

Tasks

Handlers

Apache + PHPplaybook

Apache role

PHP role

Page 24: Go Faster with Ansible

RolesUse Roles for everything!Skinny playbooks + modular roles Ideal playbook only calls roles, not tasks

Typical roles:• mysql• apache• php, php-fpm• deploy-app

"Wrapper roles" to invoke third party roles

Page 25: Go Faster with Ansible

Ansible GalaxyHub for 1000s of roles: galaxy.ansible.com

Discovery: Galaxy, GitHub, blogs, …

Assess quality carefully Install the roles needed by project: ansible-galaxy install –r requirements.yml

Pin the role to a version or Git commit

Page 26: Go Faster with Ansible

Testing Infra CodeBasic testing:- Separate test playbook using Vagrant VM

- Travis CI popular for open source- Smoke test at end of playbook:

Test frameworks:- Test-Kitchen, ServerSpec, InSpec, testinfra - Run whole series of tests - easier diagnosis

Page 27: Go Faster with Ansible

One LinersAd hoc command on single host, or group from inventory

Page 28: Go Faster with Ansible

Drupal VM

Create a VM with one command: vagrant upAnsible: 37 roles, 630 tasks, 7,200 lines of code

Page 29: Go Faster with Ansible

Drupal VMMultiple Roles

Page 30: Go Faster with Ansible

Trellis: Modern WordPress

Near Twelve Factor WordPress• Dev to Prod• PHP 7.1, A+ SSL, HTTP/2, WP-CLI, …• Example: rightsinfo.org

Related roots.io projects: • Bedrock (WP boilerplate)• Sage (starter theme)• Some commercial add-ons

Example project (blog post):• Install node, gulp, bower, Vagrant

plugins• vagrant up• Some fixes required for Ansible 2.2

Page 31: Go Faster with Ansible

ResourcesBook: Ansible for DevOps by Jeff Geerling – regular updates

Help: Stack Overflow, Ansible IRC + email lists

Roles:• Geerlingguy roles – wide range – pragmatic & well

maintained• Ansistrano: Deploying PHP apps demo (atomic model)

Projects:• Drupal-VM – http://drupalvm.com• Trellis - https://roots.io/trellis/ - very complete WordPress

setup• Use example project – requires node, bower & gulp

Best practices: Ansible.com, blogs by Leucos and Nylas

Podcasts: Arrested DevOps – general DevOps and Infra as Code

Page 32: Go Faster with Ansible

Thank YouRichard Donkin

@rdonkin

linkedin.com/in/rdonkin

Page 33: Go Faster with Ansible

Traction – Google Trends, 5 years

Page 34: Go Faster with Ansible

Advanced: Write a Module

Much more common to write a roleRequired for major new features:• New API• New package tool• New container format

Most modules written in PythonAny language works: PHP, C, Go, Perl, …Writing a module using PHP