Getting started
Hosts
$ sudo mkdir /etc/ansible
$ sudo vim /etc/ansible/hosts
[example]
192.0.2.101
192.0.2.102
Running a playbook
$ ansible-playbook playbook.yml
Tasks
- hosts: all
user: root
sudo: no
vars:
aaa: bbb
tasks:
- ...
handlers:
- ...
Includes
tasks:
- include: db.yml
handlers:
- include: db.yml user=timmy
Handlers
handlers:
- name: start apache2
action: service name=apache2 state=started
tasks:
- name: install apache
action: apt pkg=apache2 state=latest
notify:
- start apache2
Vars
- host: lol
vars_files:
- vars.yml
vars:
project_root: /etc/xyz
tasks:
- name: Create the SSH directory.
file: state=directory path=${project_root}/home/.ssh/
only_if: "$vm == 0"
Roles
- host: xxx
roles:
- db
- { role:ruby, sudo_user:$user }
- web
# Uses:
# roles/db/tasks/*.yml
# roles/db/handlers/*.yml
Task: Failures
- name: my task
command: ...
register: result
failed_when: "'FAILED' in result.stderr"
ignore_errors: yes
changed_when: "result.rc != 2"
Env vars
vars:
local_home: ""
Refereneces
Aptitude
- apt_key: id=AC40B2F7 url="http://..."
state=present
- apt: pkg=nodejs state=present
state=present # absent | latest
update_cache=yes
force=no
- apt_repository: repo='deb https://... raring main'
state=present
file
- file:
state=directory # file | link | hard | touch | absent
path=/etc/dir
owner=bin
group=wheel
mode=0644
recurse=yes # mkdir -p
force=yes # ln -nfs
- copy:
src=/app/config/nginx.conf
dest=/etc/nginx/nginx.conf
- template:
src=config/redis.j2
dest=/etc/redis.conf
git
- git: repo=git://github.com/
dest=/srv/checkout
version=master
depth=10
bare=yes
user
- user: state=present name=git
system=yes
shell=/bin/sh
comment="Git Version Control"
service
- service: name=nginx state=started [enabled=yes]
shell
- shell: apt-get install nginx -y
- script: /x/y/script.sh
Ansible modules cheatsheet
Aptitude
- apt_key: id=AC40B2F7 url="http://..."
state=present
- apt: pkg=nodejs state=present
state=present # absent | latest
update_cache=yes
force=no
- apt_repository: repo='deb https://... raring main'
state=present
file
- file:
state=directory # file | link | hard | touch | absent
path=/etc/dir
owner=bin
group=wheel
mode=0644
recurse=yes # mkdir -p
force=yes # ln -nfs
- copy:
src=/app/config/nginx.conf
dest=/etc/nginx/nginx.conf
- template:
src=config/redis.j2
dest=/etc/redis.conf
git
- git: repo=git://github.com/
dest=/srv/checkout
version=master
depth=10
bare=yes
user
- user: state=present name=git
system=yes
shell=/bin/sh
comment="Git Version Control"
service
- service: name=nginx state=started [enabled=yes]
shell
- shell: apt-get install nginx -y
- script: /x/y/script.sh
Install Ansible
$ brew install ansible # OSX
$ [sudo] pip install ansible # elsewhere
Start your project
~$ mkdir setup
~$ cd setup
Create an inventory file
This is a list of hosts you want to manage, grouped into groups. (Hint: try using 127.0.0.1 to deploy to your local machine)
; ~/setup/hosts
[sites]
127.0.0.1
192.168.0.1
192.168.0.2
192.168.0.3
Create your first Playbook
# ~/setup/playbook.yml
- hosts: 127.0.0.1
user: root
tasks:
- name: install nginx
apt: pkg=nginx state=present
- name: start nginx every bootup
service: name=nginx state=started enabled=yes
- name: do something in the shell
shell: echo hello > /tmp/abc.txt
- name: install bundler
gem: name=bundler state=latest
Run it
~/setup$ ls
hosts
playbook.yml
~/setup$ ansible-playbook -i hosts playbook.yml
PLAY [all] ********************************************************************
GATHERING FACTS ***************************************************************
ok: [127.0.0.1]
TASK: [install nginx] *********************************************************
ok: [127.0.0.1]
TASK: start nginx every bootup] ***********************************************
ok: [127.0.0.1]
...
Read more
Roles
Structure
roles/
common/
tasks/
handlers/
files/ # 'copy' will refer to this
templates/ # 'template' will refer to this
meta/ # Role dependencies here
vars/
defaults/main.yml
References