Ansible Talk
Ansible Talk
Ansible Talk
Whatisitandwhy
shouldIbeusingit?
2 / 36
What
is
it?
3 / 36
What
is
it?
Why
use
it?
4 / 36
Setupthecontrolmachine
5 / 36
Setup
the
control
machine
Work in a virtualenv
pip install virtualenv
virtualenv myproject
cd myproject
. bin/activate
pip install ansible
6 / 36
Yourfirstcommands
7 / 36
Your
first
command
8 / 36
Your
first
commands
Examine
the
command
line
Theinventoryfile
10 / 36
The
inventory
file
11 / 36
Modules
12 / 36
Modules
13 / 36
Playbooks
14 / 36
Playbooks
15 / 36
Playbook
Example
16 / 36
Test passed:
PLAY [test]
************************************************************
TASK: [ensure apache2 is installed]
******************************************
ok: [localhost] => {"changed": false}
PLAY RECAP
************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0
17 / 36
PlaybookRolesandIncludeActions
18 / 36
Playbook
Roles
and
Include
Actions
19 / 36
Playbook
Roles
and
Include
Actions
Role hierarchy:
If roles/x/tasks/main.yml exists, tasks listed therein
will be added to the play
If roles/x/handlers/main.yml exists, handlers listed
therein will be added to the play
If roles/x/vars/main.yml exists, variables listed therein
will be added to the play
If roles/x/meta/main.yml exists, any role
dependencies listed therein will be added to the list of
roles (1.3 and later)
Any copy tasks can reference files in roles/x/files/
without having to path them relatively or absolutely
Any script tasks can reference scripts in roles/x/files/
without having to path them relatively or absolutely
Any template tasks can reference files in
roles/x/templates/ without having to path them
relatively or absolutely
Any include tasks can reference files in roles/x/tasks/
without having to path them relatively or absolutely
20 / 36
Variables
21 / 36
Variables
hierarchy
22 / 36
AnsibleandAWS
23 / 36
Ansible
and
AWS
24 / 36
Ansible
and
AWS
25 / 36
Ansible
and
AWS
26 / 36
ManageyourAWS
nodeswithansible
27 / 36
Create
and
start
a
node
demo-create.yml
- name: Creates aws-nodes
hosts: all
connection: local
remote_user: root
vars:
image_id: image-id
instance: instance-type
key_name: ssh-key
region: region-name
tasks:
- name: Create and launch instance
ec2:
key_name: "{{ ssh-key }}"
instance_type: "{{ instance }}"
image: "{{ image_id }}"
region: "{{ region }}"
state: present
count: 1
wait: yes
ansible-playbook -i aws.ini demo-create.yml
28 / 36
Stop
a
node
demo-stop.yml
- name: Stop aws servers
connection: local
remote_user: root
vars:
- region: region_name
tasks:
- name: Stop instances
ec2:
region: "{{ region }}"
state: stopped
instance_ids: "{{ec2_id}}"
ansible-playbook -i plugins/inventory/ec2.py demo-stop.yml
29 / 36
Provision
nodes
30 / 36
Delete
nodes
demo-terminate.yml
- name: Delete aws servers
remote_user: root
vars:
- region: region_name
tasks:
- name: Delete hosts
ec2:
instance_ids: "{{ ec2_id }}"
region: "{{ region }}"
state: absent
wait: yes
ansible-playbook -i plugins/inventory/ec2.py demo-terminate.yml
31 / 36
Sometools
32 / 36
Some
tools
ansible-galaxy
It is the Ansibles official community hub for finding,
downloading, rating, and sharing Ansible roles.
ansible-galaxy install username.rolename
33 / 36
Some
tools
debops
Your Debian-based data center in a box. It is a framework.
It can be installed through ansible-galaxy. It is a collection
of Ansible playbooks, scalable from one container to an
entire data center.
ansible-galaxy install debops.apt
ansible all -s -m apt -a 'update_cache=yes upgrade=yes'
34 / 36
Some
tools
epdb
epdb or pdb? The reason to use epdb over pdb is epdb
contains a remote debugging feature that can sometimes
be useful for debugging processes where you dont have
console access.
In python module write:
import epdb
epdb.serve()
35 / 36
"Allpartsshouldgotogetherwithoutforcing.Youmust
rememberthatthepartsyouarereassemblingwere
disassembledbyyou.Therefore,ifyoucan'tgetthem
togetheragain,theremustbeareason.Byallmeans,donot
useahammer."
IBMmaintenancemanual,1925