Larry cai <larry.caiyu@gmail.com>
Agenda 
 Ansible Introduction 
 Exercise 1: Setup environment using docker 
 Exercise 2: Inventory and ad-hoc command 
 Exercise 3: Playbooks - install apache 
 Exercise 4: Playbooks – variables 
 Exercise 5: Playbooks – Template using Jinja2 
 Summary 
Code: 
https://github.com/larrycai/codingwithme-ansible 
Learn Ansible 2 in Docker in 90 minutes 09/28/14
Environment (docker/fig) 
 Boot2docker Installer (127M) http://boot2docker.io/ 
 Contains latest docker already, fast 
 Container persistence via disk automount on /var/lib/docker 
 Add proxy /var/lib/boot2docker/profile if needed 
 $ sudo vi /var/lib/boot2docker/profile 
 export http_proxy=<your proxy> 
 $ sudo /etc/init.d/docker restart 
 $ docker -v 
 User/Passwd: docker/tcuser 
 (Optional) replace with boot2docker.iso 
(fig/share folder support) 
https://github.com/larrycai/boot2docker-vbga-fig/releases 
Learn Ansible 3 in Docker in 90 minutes 09/28/14
Environment use online service 
 Create docker VM using CoreOS image, and assign public 
IP to access 
 http://ustack.com or 
https://cloud.digitalocean.com 
 Clone code & Start them 
$ git clone https://github.com/larrycai/codingwithme-ansible.git 
$ cd codingwithme-ansible 
$ bash start.sh 
# ./update.sh 
# ansible all –a “uname –a” 
Learn Ansible 4 in Docker in 90 minutes 09/28/14
What is Ansible 
 Ansible is a radically simple IT orchestration engine that 
automates configuration management, application 
deployment, and many other IT needs. 
 Similar to Cfengine/Puppet/Chef/Saltstack 
 Features: 
 Agentless with ssh 
 Very simple language (YAML). 
 Lots of modules to execute task. 
 Python 
Image source: page21 
from http://www.slideshare.net/NETWAYS/jp-mensansible 
Learn Ansible 5 in Docker in 90 minutes 09/28/14
Exercise 1: 
Setup environment using docker 
 Clone code from 
https://github.com/larrycai/codingwithme-ansible 
 $ fig run ansible bash # or ./start.sh 
(ansible) # ./update.sh & cd exercise 
(ansible) # ansible all –a “uname –a” 
AAnsnisbilbel ee nevnivrioronmnmenetnt 
HHaparporoxyxy 
wwebe1b1 
wwebe2b2 
DDataatbaabsaese 
DDoockckeer rE Enngigninee S eServrever r( V(VMM) ) 
80 1080 
80 80 
wwebe2b2 
hahparporoxyxy 
wwebe1b1 
Learn Ansible 6 in Docker in 90 minutes 09/28/14
Inventory & ad-hoc command 
 hosts: Inventory is host list 
 ansible.cfg: define 
 An ad-hoc command is something that you might type in 
to do something really quick, but don’t want to save for 
later. 
$ ansible <host patterns> [options] 
$ ansible web –m command –a “uname –a” 
 -m module name, default is command 
 -I inventory name, defaults is set in ansible.cfg or /etc/ansible/hosts 
 -a module args See http://docs.ansible.com/intro_adhoc.html 
Learn Ansible 7 in Docker in 90 minutes 09/28/14
Module 
 Ansible ships with a number of modules 
(called the ‘module library’) that can be 
executed directly on remote hosts 
 Modules can control system resources, 
like services, packages, or files (anything 
really), or handle executing system 
commands. 
 All modules technically return JSON 
format data 
See http://docs.ansible.com/modules.html 
Learn Ansible 8 in Docker in 90 minutes 09/28/14
Exercise 2: ad-hoc command 
 Check free memory in `all` hosts `-a “free –m”` 
 Check all facts in `web` host pattern using module setup 
 Create `/ansible` directory is created in web 
 Using file module http://docs.ansible.com/file_module.html 
 -m file -a “path=/ansible state=<?>” 
 Run command again (check changed) 
 ssh to remote web1 to remove `/ansible` and do it again 
–i /ansible/id_rsa root@web1 
 Take a look at module /usr/share/ansible/files/file 
Learn Ansible 9 in Docker in 90 minutes 09/28/14
Idempotency 
 Idempotence is the ability to run an operation which 
produces the same result whether run once or multiple 
times 
 Ansible has ability to ensure the same configuration is 
maintained whether you run it once or a thousand times. 
 In fact, almost every aspect of Ansible modules and 
commands is idempotent. 
 $ ansible web –m file –a “path=/ansible state=directory” 
 Declarative: Define what instead of how 
path=/ansible state=directory 
vs. 
mkdir /ansible 
Learn Ansible 10 in Docker in 90 minutes 09/28/14
Playbook 
 Playbooks are Ansible’s configuration, deployment, and 
orchestration language. They can describe a policy you 
want your remote systems to enforce, or a set of steps in 
a general IT process. 
 $ ansible-playbook site.yml 
 Each task is one module 
command 
 - file: path=/ansible state=directory 
or 
- name: make sure /ansible exist 
file: path=/ansible state=directory 
 YAML format 
key/value format 
http://docs.ansible.com/playbooks.html 
Learn Ansible 11 in Docker in 90 minutes 09/28/14
Exercise 3:Playbook – Install apache 
 Turn file command into playbook exer3.yml 
 Install apache2 and make them running into web hosts 
$ ansible-playbook exer3.yml 
 Use curl command to verify apache2 is running 
$ curl http://web1_1:80 
 Run ansible-playbook in debug mode using –vvvv 
notice the color for changed=true/false 
If work in firewall, run below command before exercise 
$ ansible-playbook proxy.xml –e “http_proxy=http://<company_proxy>” 
Learn Ansible 12 in Docker in 90 minutes 09/28/14 
wwebe2b2 
80
Variable 
 Variable is used to abstract data in ansible 
 Define variable and use it with “{{ }}” 
- host: web 
vars: 
http_port:80 
tasks: 
- debug: msg=“hello {{ http_port }}” 
 Default variables can be put under group_vars/all 
 Pass variable from command line –e “key=value” 
 Ansible provides a few variables for you automatically. 
‘hostvars’, ‘group_names’, and ‘groups’. 
 with_items for multi key/value 
- name: touch files with an optional mode 
file: dest={{ item.path }} state=touch 
with_items: 
- path: /tmp/foo 
- path: /tmp/bar 
Learn Ansible 13 in Docker in 90 minutes 09/28/14
Exercise 4: Variables 
 Install haproxy (understand) 
 check web ip (understand) 
 Print ip address (system variable “hostvars”) 
 Install extra packages (curl) using variables 
 Variable in yaml 
 In group_vars 
 Pass in command line 
 Install extra packages with_items (wget/socat) 
Learn Ansible 14 in Docker in 90 minutes 09/28/14 
wwebe2b2 
HHaparporoxyxy 
wwebe1b1 
80 80
File/Template 
 Template using Jinja2 (http://jinja.pocoo.org/), which is a 
modern and designer-friendly templating language for 
Python 
 Template module 
template: src=templates/haproxy.cfg.j2 dest=/etc/haproxy/haproxy.cfg 
Learn Ansible 15 in Docker in 90 minutes 09/28/14
Exercise 5: Template 
 See result 
 Add web1/web2 into haproxy backend using loop haproxy.cfg.j2 
 Add stats port 1080 in haproxy 
 Check it in haproxy server 
 docker ps to check haproxy’s port for 80/1080 
80 1080 
 http://192.168.59.103:49155 & http://192.168.59.103:49156 
 Update /var/www/html/index.html in each web for to its 
hostname 
Learn Ansible 16 in Docker in 90 minutes 09/28/14 
wwebe2b2 
hahparporoxyxy 
wwebe1b1 
80 80
Others not touched 
 Dynamic Inventory 
 Roles 
 Write own module 
 Ansible-Galaxy 
 Ansible-Tower 
Learn Ansible 17 in Docker in 90 minutes 09/28/14
Summary 
 Ansible is the orchestration engine to manage your 
infrastructure 
 Automate your own tasks using Ansible 
 Just do it ! 
Learn Ansible 18 in Docker in 90 minutes 09/28/14
Reference 
 http://docs.ansible.com/ 
 https://serversforhackers.com/editions/2014/08/26/getting 
-started-with-ansible/ 
 Practice online 
 http://ustack.com 
Learn Ansible 19 in Docker in 90 minutes 09/28/14