Ansible
Install
# apt-get install ansible
Kõisse masinaisse tuleb paigaldada SSH võti
# ssh-keygen -t rsa
Kliendid vaja defineerida
Kliendi defineerimine toimub failis /etc/ansible/hosts näiteks lisame ühe kliendi gruppi test
[test] 10.20.0.20
Neid gruppe võib teha terve hulga, nt kõik masinad ära jaotada opsüsteemide järgi
[debian] vm1 vm2 vm3 [fedora] netflix [centos] confluence vm-server1 gitlab [ubuntu] trusty-mirror media-centre nas
Käsurealt kasutamine
Kõikide nodede korrasoleku kontroll
# ansible all -m ping
Küsime nodedelt hostname käsku
# ansible -m shell -a "hostname" all
Samamoodi võib käsurealt otse teha ka jõhkramaid toiminguid, nt midagi installida
# ansible all -s -m shell -a 'apt-get install nginx'
Playbook
Playbookid on .yml failid, kus defineeritakse Ansible tegevused ja tegevuste järjekord. Näiteks lihtne playbook mis paigaldab ngxinxi apt moodulit kasutades ning paneb selle tööle.
---
- hosts: debian
tasks:
- name: Installs nginx web server
apt: pkg=nginx state=installed update_cache=true
notify:
- start nginx
handlers:
- name: start nginx
service: name=nginx state=started
Nende käivitamiseks
# ansible-playbook test.yml
Keerukam lahendus, mis juba lisaks paigaldab vaikimisi nginx konfi ja tõmbab alla ka gitist veebiserveri sisu
---
- hosts: debian
sudo: yes
tasks:
- name: paigaldame nginx
apt: name=nginx state=installed update_cache=yes
- name: loome nginxile konfiguratsiooni
template: src=templates/nginx.conf.j2 dest=/etc/nginx/nginx.conf
notify: restart nginx
- name: paigaldame veebiserverisse sisu
git: repo=https://github.com/jweissig/episode-47.git
dest=/usr/share/nginx/html/
version=release-0.01
- name: start
service: name=nginx state=started
handlers:
- name: restart nginx
service: name=nginx state=restarted
Kolmas näide
---
- hosts: linode
remote_user: yourusername
become: yes
become_method: sudo
tasks:
- name: "Install Apache, MySQL, and PHP5"
apt: name=Mall:Item state=present
with_items:
- apache2
- mysql-server
- python-mysqldb
- php5
- php-pear
- php5-mysql
- name: "Turn on Apache and MySQL and set them to run on boot"
service: name=Mall:Item state=started enabled=yes
with_items:
- apache2
- mysql
- name: Create a test database
mysql_db: name=testDb
state=present
- name: Create a new user for connections
mysql_user: name=webapp
password=mypassword
priv=*.*:ALL state=present
Ip aadressi muutujaks tegemine playbookis
- name: get IP of testmachine
shell: "dig +short testmachine.test.dom| awk '{ print ; exit }'"
register: ip_test
always_run: yes
changed_when: False
debug: msg="ip of testmachine is Mall:Ip test.stdout"
Template kasutamine
Esimesena tuleb luua templaatide kataloog
mkdir templates
Seejärel tekitame haproxy konfiguratsiooni
nano templates/haproxy.cfg
Konfi lisame järgnevad read
global log 127.0.0.1 local0 notice maxconn 2000 user haproxy group haproxy defaults log global mode http option httplog option dontlognull retries 3 option redispatch timeout connect 5000 timeout client 10000 timeout server 10000 listen Mall:Haproxy app name 0.0.0.0:80 mode Mall:Haproxy mode stats Mall:Haproxy enable stats {% if haproxy_enable_stats == 'enable' %} stats uri /haproxy?stats stats realm Strictly\ Private {% for user in haproxy_stats_users %} stats auth Mall:User.username:Mall:User.password {% endfor %} {% endif %} balance Mall:Haproxy algorithm option httpclose option forwardfor {% for server in haproxy_backend_servers %} server Mall:Server.name Mall:Server.ip:Mall:Server.port Mall:Server.paramstring {% endfor %}
Järgmisena defineerime konfitemplates olevad muutujad playbooki
vars:
haproxy_app_name: myapp
haproxy_mode: http
haproxy_enable_stats: enable
haproxy_algorithm: roundrobin
haproxy_backend_servers:
- {name: server1, ip: 10.133.181.247, port: 80, paramstring: cookie A check}
- {name: server2, ip: 10.133.186.46, port: 80, paramstring: cookie A check}
haproxy_stats_users:
- {username: joe, password: soap}
Ja kige viimasena seome konfi ja template kokku
- name: Update HAProxy config
template: src=templates/haproxy.cfg
dest=/etc/haproxy/haproxy.cfg
backup=yes
Moodulid'
Ansible sisaldab lisaks suurt hulka mooduleid praktiliselt kõige tegemiseks. Moodulite nimekirja näeb
# ansible-doc -l
Näiteks saab importida mooduli abil postgresi dump faili
---
- hosts: postgres-server
tasks:
- name: Restore db server
postgresql_db: name=example state=import target=/example.sql
https://github.com/randohinn/KnowHow/blob/master/docs/Vorgurakendused/ansible.rst
https://github.com/asjalik/ansible
http://docs.ansible.com/ansible/latest/proxmox_module.html
http://docs.ansible.com/ansible/latest/mysql_db_module.html
https://github.com/rhwlo/ansible-playbooks/blob/master/mailserver.yml