Ansible Playbooks, Ansible’in orkestrasyon dilidir. Tanımladığımız Playbooklarda Ansible’in yapmasını istediğimiz adımları tanımlayacağız. Tanımladığımız bir dizi talimat sonrasında Ansible sihrini bizlere gösterecektir.
Playbookların hepsi yaml formatında yazılmaktadır. playbook.yml Playbook aktivitelerimizi belirler. Bir grev tek bir eylemdir. Hedef bilgisayarda bir paket yükleme, yeniden başlatma, kapatma gibi işlemleri gerçekleştirebiliriz.
Örnek bir Playbook içeriği oluşturup aşağıdaki adımları gerçekleştirelim.
- Komut çalıştır,
- Script çalıştır,
- Paket yükle/çalıştır
- Makineyi kapat ya da yeniden başlat
-
name: Play 1
hosts: localhost
tasks:
- name: Execute command 'date'
command: date
- name: Execute script on server
command: test_script.sh
- name: Install httpd service
yum:
name: httpd
state: present
- name: Start web server
service:
name: httpd
state: started
YAML yazımızda kullandığımız yöntemleri düşünerek bu playbook’u biraz değiştirelim. Dictionary ol
playbook.yml
-
name: Play 1
hosts: localhost
tasks:
- name: Execute command 'date'
command: date
- name: Execute script on server
command: test_script.sh
-
name: Play 2
hosts: localhost
tasks:
- name: Install httpd service
yum:
name: httpd
state: present
- name: Start web server
service:
name: httpd
state: started
Peki hazırladığımız ansible playbooku nasıl çalıştıracağız.
ansible-playbook <playbook file name>
Biraz örneklere dalalım.
Ekrana tarihi yazdıracak ve adı localhostun ekranına tarih yazdırılacağı bilgisini veren ve localhost isimli makinede çalışacak bir playbook hazırlayalım.
-
name: 'Execute a date command on localhost'
hosts: localhost
tasks:
-
name: 'Execute a date command'
command: date
/etc/hosts içerisinde bulunan hostların ekrana yazdırılmasını sağlayan bir playbook oluşturalım,
-
name: 'Execute a command to display hosts file on localhost'
hosts: localhost
tasks:
-
name: 'Execute a command to display hosts file'
command: 'cat /etc/hosts'
Ekrana tarih ve hostları yazdıran bir Playbook oluştluralım,
-
name: 'Execute two commands on localhost'
hosts: localhost
tasks:
-
name: 'Execute a date command'
command: date
-
name: 'Execute a command to display hosts file'
command: 'cat /etc/hosts'
Yukarıda gerçekleştirdiğimiz adımları web_node1 grubunda bulunan cihazlara gerçekleştirelim,
-
name: 'Execute two commands on web_node1'
hosts: web_node1
tasks:
-
name: 'Execute a date command'
command: date
-
name: 'Execute a command to display hosts file'
command: 'cat /etc/hosts'
Boston node larına uygulayalım,
-
name: 'Execute two commands on web_node1'
hosts: boston_nodes
tasks:
-
name: 'Execute a date command'
command: date
-
name: 'Execute a command to display hosts file'
command: 'cat /etc/hosts'
web_node1 makinelere ekrana tarih, web_node2 makinelerinde ise hosts dizinini ekrana gösterelim,
-
name: 'Execute command to display date on web_node1'
hosts: web_node1
tasks:
-
name: 'Execute a date command'
command: date
-
name: 'Execute a command to display hosts file contents on web_node2'
hosts: web_node2
tasks:
-
name: 'Execute a command to display hosts file'
command: 'cat /etc/hosts'
Son olarakta aşağıdaki adımları gerçekleştirelim,
- web sunucularında web servislerini durduralım, / service httpd stop
- db sunucusunda db servisini durduralım / service mysql stop
- web ve db sunucusunu restart edelim / /sbin/shutdown -r
- db sunucularında db servisini restart edelim / service mysql start
- web sunucularında web servislerini restart edelim. / service httpd start
-
name: 'Stop the web services on web server nodes'
hosts: web_nodes
tasks:
-
name: 'Stop the web services on web server nodes'
command: 'service httpd stop'
-
name: 'Shutdown the database services on db server nodes'
hosts: db_nodes
tasks:
-
name: 'Shutdown the database services on db server nodes'
command: 'service mysql stop'
-
name: 'Restart all servers (web and db) at once'
hosts: all_nodes
tasks:
-
name: 'Restart all servers (web and db) at once'
command: '/sbin/shutdown -r'
-
name: 'Start the database services on db server nodes'
hosts: db_nodes
tasks:
-
name: 'Start the database services on db server nodes'
command: 'service mysql start'
-
name: 'Start the web services on web server nodes'
hosts: web_nodes
tasks:
-
name: 'Start the web services on web server nodes'
command: 'service httpd start'