Struktur
environment/
group_vars/
host_vars/
inventory
roles/
...
ansible.cfg
site.yml
Xservers.yml
Environment
z. B. production, staging, development
Group Vars
z.B. piwik-servers
- Gruppen spezifische Variablen
- Dateiname muss dem Namen im Inventar entsprechen
$ cat production/group_vars/piwik-servers
---
user: piwik
# Piwik download location
download_url: 'http://builds.piwik.org'
download_file: 'latest.zip'
Hosts Vars
z.B. ec2-54-72-255-255.eu-west-1.compute.amazonaws.com
-
Host spezifische Variablen
- Dateiname muss dem Hostnamen im Inventar entsprechen
$ cat production/host_vars/ec2-54-72-255-255.eu-west-1.compute.amazonaws.com
---
mysql_host: "pw.xxxxxxxxxxx.eu-west-1.rds.amazonaws.com"
Inventory
- Enthaelt alle Server des Environments
- Ordnet die Server in Gruppen
$ cat production/inventory
[piwik-servers]
ec2-54-72-255-255.eu-west-1.compute.amazonaws.com
ec2-54-186-255-255.us-west-2.compute.amazonaws.com
site.yml
- Master YAML-Datei
- Inkludiert alle weiteren Playbooks
---
- include: piwik-servers.yml
...
Xservers.yml
z.B. webservers.yml
- Beschreibt eine Gruppe von Servern
- Welche Rollen diese besitzen
- Welcher Benutzer verwendet wird
$ cat piwik-servers.yml
---
- hosts: piwik-servers
user: ansible
sudo: True
roles:
- dns-client
- mysql-client
- nginx
- php
- piwik
Role
role/
files/
known_hosts
handlers/
main.yml
(meta/)
tasks/
main.yml
templates/
config.py.j2
(vars/)
Role - Tasks
- Zentrale Datei einer Rolle
- Enthaelt alle Aufgaben diese Rolle
$ cat roles/piwik/tasks/main.yml
---
- name: Install requirements
apt: pkg={{ item }} update_cache=yes state=latest
tags: piwik
with_items:
- php5-geoip
- unzip
- name: Fetch latest Piwik release
get_url: url={{ download_url }}/{{ download_file }} dest=/tmp mode=0440 owner={{ user }}
tags: piwik
Role - Handlers
- Zur Steuerung von Prozessen
- Koennen in den Tasks verwendet werden
$ cat roles/nginx/handlers/main.yml
---
- name: restart nginx
service: name=nginx state=restarted
$ cat roles/nginx/tasks/main.yml
---
- name: write nginx.conf
copy: src=nginx.conf dest=/etc/nginx/nginx.conf mode=0644
tags: nginx
notify:
- restart nginx
Role - Templates
- Jinja2 Templates
- Werden durch Variablen aus `host_vars`,
`group_vars` oder `vars` gefuellt
$ cat roles/piwik/templates/php_piwik.conf.j2
[{{ user }}]
listen = /home/www/{{ user }}/php.sock
; Set permissions for unix socket, if one is used. In Linux, read/write
listen.owner = {{ user }}
listen.group = {{ user }}
listen.mode = 0666
; Unix user/group of processes
user = {{ user }}
group = {{ user }}
[...]