My talk from DevOpsCon Berlin 2016.
Ansible is a radically simple and lightweight provisioning framework which makes your servers and applications easier to provision and deploy. By orchestrating your application deployments you gain benefits such as documentation as code, testability, continuous integration, version control, refactoring, automation and autonomy of your deployment routines, server and application configuration. Ansible uses a language that approaches plain English, uses SSH and has no agents to install on remote systems. It is the simplest way to automate and orchestrate application deployment, configuration management and continuous delivery.
In this tutorial you will be given an introduction to Ansible and learn how to provision Linux servers with a web-proxy, a database and some other packages. Furthermore we will automate zero downtime deployment of a Java application to a load balanced environment.
5. Describe what to do (imperative)
#!/bin/bash
if$(command-vvim>/dev/null2>&1);then
echo"vimisalreadyinstalled."
else
apt-getinstallvim
fi
if$(grep-Fxq"filetypeindentoff"/etc/vim/vimrc);then
echo"setfiletypeindentoffisalreadyin/etc/vim/vimrc."
else
echo"filetypeindentoff">>/etc/vim/vimrc
#TODO:Donotcontinueifthisfails.
fi
#TODO:Rollbackifsomethingfails.
7. Pros
In source control.
Self documenting (it's code!).
Refactoring.
Less differences between environments.
Deterministic.
Prevents manual steps.
Fast and easy to configure up a new environment.
Easier to test server setup.
8. Ansible
SSH-based
Client only (no server)
YAML configuration
Push (and pull)
Supports more than setup and provisioning:
Application deployment
Remote command execution
12. Facts
Ansible by default gathers “facts” about the machines under
management.
These facts can be accessed in Playbooks and in templates.
ansible-msetupapp1.local
13. The task
An app user 'devops', with:
Home directory: /home/devops
ssh-key
A PostgresSQL database.
Nginx as a reverse proxy.
An init script installed as a service.
Deploy an application that uses the provisioned infrastructure.
20. Variables
Use variables! → Infrastructure as data.
Where should variables be defined? Ansible has very many options.
Inventory (./hosts)
group_vars and host_vars
Playbook (site.yml)
Facts (local or server)
Command line (as arguments)
Access variables from playbooks: "{{ variable }}"
http://docs.ansible.com/playbooks_variables.html
21. Task2: Create an application user
Create roles/users/tasks/main.yml
Home directory: /home/devops
ssh-key
Use variables! (group_vars)
Help:
(createadirectory)
(.ssh/authorized_keys)
http://docs.ansible.com/group_module.html
http://docs.ansible.com/user_module.html
http://docs.ansible.com/file_module.html
http://docs.ansible.com/lineinfile_module.html
http://docs.ansible.com/playbooks_best_practices.html#group-and-host-variables
40. Progress
Installed software
Manipulated files
Created a user and set up a ssh-key
Installed and configured a database and a db user
Deployed an application to two appservers and enabled it as a
service
Migrated the database schema and fetched data from it through the
application
41. Task6: Set up proxy
roles/nginx
├──handlers
│ └──main.yml
├──tasks
│ ├──config_nginx.yml
│ ├──install_nginx.yml
│ └──main.yml
└──templates
└──devops.conf.j2
Help:
http://wsgiarea.pocoo.org/jinja/docs/loops.html
43. Browse to # refresh me many times
Run it!
ansible-playbooksite.yml--limitproxies--tagsnginx
Try it!
http://proxy.local/
44. Progress
Installed software
Manipulated files
Created a user and set up a ssh-key
Installed and configured a database and a db user
Deployed an application to two appservers and enabled it as a
service
Migrated the database schema and fetched data from it through the
application
Set up a reverse proxy for automatic failover between the two
appservers
52. Play time :-)
Suggestions:
Change database table name from HELLO to MESSAGES and
deploy a new version without downtime.
Implement automated rollback.