On Github msgre / ansible-talk
Tkalci na webuMichal Valoušek — @msgrehttps://github.com/msgre/ansible-talk
$ vagrant up control
$ vagrant up remote1
$ vagrant ssh control
vagrant@control:~$ ssh vagrant@remote1.vbox
$ vagrant ssh control
vagrant@control:~$ sudo apt-get install software-properties-common
vagrant@control:~$ sudo apt-add-repository -y ppa:ansible/ansible
vagrant@control:~$ sudo apt-get update
vagrant@control:~$ sudo apt-get install -y ansible
$ vagrant ssh control
vagrant@control:~$ sudo rm /etc/ansible/hosts
vagrant@control:~$ sudo ln -s /vagrant/hosts /etc/ansible/
vagrant@control:~$ ping remote1.vbox
vagrant@control:~$ ansible remote1.vbox -m ping
vagrant@control:~$ hostname
vagrant@control:~$ ansible remote1.vbox -m shell -a "hostname"
ansible <pattern> -m <module> -a <arguments>
ansible remote1.vbox -m ping
ansible remote1.vbox -m shell -a "hostname"
ansible prague -m copy -a "src=/etc/hosts dest=/tmp/hosts"
ansible prague -m file -a "dest=/path/to/c mode=755 state=directory"
ansible webservers -m apt -a "name=nginx state=present"
ansible all -m user -a "name=karel password=<crypted password>"
ansible all -m user -a "name=toncin state=absent"
ansible webservers -m service -a "name=httpd state=restarted"
ansible all -m setup
ansible remote1.vbox -m apt -a "name=nginx state=present"
# prihlasi se na masinu jako uzivatel "username"
-u username
# prikazy na nodu bude provadet pres sudo
--sudo
# prikazy na nodu bude provadet pres sudo jako uzivatel "bohous"
-U bohous
# kdyz se neco kazi
-v, -vvv, -vvvv
ansible <pattern> -m <module> -a <arguments>
vagrant@control:~$ sudo rm /etc/ansible/hosts
vagrant@control:~$ sudo ln -s /vagrant/hosts /etc/ansible/
remote1.vbox ansible_ssh_host=172.16.1.21
remote2.vbox ansible_ssh_host=172.16.1.22
remote3.vbox ansible_ssh_host=172.16.1.23
mail.example.com
[webservers]
foo.example.com
bar.example.com
[dbservers]
one.example.com
two.example.com
three.example.com
[atlanta]
host1
host2
[atlanta:vars]
ntp_server=ntp.atlanta.example.com
proxy=proxy.atlanta.example.com
[mezric]
host1
host2
[vsetin]
host2
host3
[valachy:children]
mezric
vsetin
# zacileni vsech stroju
ansible all -m ping
# zacileni stroju ve skupine A nebo B (OR, sjednoceni)
ansible webservers:dbservers -m ping
# zacileni webservers ale bez phoenix (NOT)
ansible webservers:!phoenix -m ping
# zacileni webservers a zaroven staging (AND, prunik)
ansible webservers:&staging -m ping
# brutus kombinace
ansible webservers:dbservers:&staging:!phoenix -m ping
ansible <pattern> -m <module> -a <arguments>
(pokud už nebyla)
---
- hosts: remote1.vbox
sudo: true
vars:
static_dir: /var/static/
tasks:
- name: Install Nginx
apt: pkg=nginx state=present
- name: Prepare directory for static content
file: path={{static_dir}} state=directory
# ...
Zápis v YAML formátu
---
- hosts: remote1.vbox
sudo: true
tasks:
- name: Install Nginx
apt: pkg=nginx state=present
- hosts: remote2.vbox
sudo: true
tasks:
- name: Install Vim
apt: pkg=vim state=present
vagrant@control:~$ ansible-playbook nginx1.yml
Šablona template/foo.cfg.j2 (syntaxe Jinja2):
My amp goes to {{ max_amp_value }}
Vygenerování šablony v playi:
template: src=foo.cfg.j2 dest=/etc/kangaroo/foo.cfg
- name: "shutdown Debian flavored systems"
command: /sbin/shutdown -t now
when: ansible_os_family == "Debian"
- name: add several users
user: name={{ item }} state=present groups=wheel
with_items:
- testuser1
- testuser2
tasks:
- name: ensure apache is at the latest version
yum: pkg=httpd state=latest
- name: write the apache config file
template: src=/srv/httpd.j2 dest=/etc/httpd.conf
notify:
- restart apache
# ...
handlers:
- name: restart apache
service: name=httpd state=restarted
tasks:
- include: tasks/foo.yml
- include: tasks/bar.yml var1=1 var2=3
tasks:
- yum: name={{item}} state=installed
with_items:
- httpd
- memcached
tags:
- packages
$ ansible-playbook example.yml --tags "configuration,packages"
---
- name: Init provisioning infrastructure
hosts: 127.0.0.1
connection: local
gather_facts: no
- name: Wait for port 8000
wait_for: port=8000 delay=10
production # inventory file for production servers
stage # inventory file for stage environment
group_vars/
group1 # here we assign variables to particular groups
host_vars/
hostname1 # if systems need specific variables, put them here
site.yml # master playbook
roles/
common/ # this hierarchy represents a "role"
tasks/ #
handlers/ #
templates/ # files for use with the template resource
files/ #
vars/ #
defaults/ #
meta/ #
webtier/ # same kind of structure as "common" was above, done for the webtier role
monitoring/ # ""
---
- hosts: webservers
roles:
- common
- webservers