Last updated on 5 Eylül 2021
Ansible altyapımızda bulunan bir veya birden fazla sistem ile aynı birlikte ve aynı zamanda çalışabilir. Birden fazla sistem ile çalışabilmesi için Ansible kurulması gerekmektedir. Bağlantı sağlayacağı sunuculara ise Linux sunucular için SSH, Windows makineler için Powershell Remoting kullanılarak yapılmakta.
Ansible’ı Agentless(ajansız) yapan da budur. Agentless, hedef makinelere ansible ile bağlanmak için ajana ihtiyacımız olmadığı anlamına gelir.
Basit bir SSH bağlantısı Ansible’ın ihtiyaçlarını karşılar.
Bağlanmak istediğimiz hedef sistemler bir envanter dosyasında saklanır. Eğer biz bir envanter dosyası oluşturmazsak Ansible default olarak /etc/ansible/hosts lokasyonunu kullanır.
Envanter dosyası INI benzeri bir formattadır. Bir dizi sunucu birbirinin ardına sıralanmaktadır.
server1.company.com server2.company.com
Ayrıca farklı sunucuları bir arada da gruplayabiliriz. Hatta aşağıdaki örnekte olduğu gibi birden fazla grubumuzu tek bir envanter içinde tanımlayabiliriz.
server1.company.com server2.company.com [mail] server3.company.com server4.company.com [db] server5.company.com server6.company.com [web] server7.company.com server8.company.com
Daha detaylı bakmak gerekirse, bu sunucular için alias (Takma isim tanımlayabiliriz).
web ansible_host=server1.company.com db ansible_host=server2.company.com mail ansible_host=server3.company.com web2 ansible_host=server4.company.com
Ansible_host sunucunun ip adresini ya da fqdn bilgisini tanımlamak için kullanılır. Sol tarafına alias takma ad bilgisi yazilir.
Başka bağlantı parametleride aşağıda sıralanmıştır.
ansible_connection - ssh/winrm/localhost ansible_port - 22/5986 ansible_user - root/administrator ansible_ssh_pass - Password
ansible_connection bağlantının hedef sunucuya nasıl gerçekleştirileceğini belirtir. Winrm windows sunucular için kullanıldığını tekrardan hatırlayalım.
web ansible_host=server1.company.com ansible_connection=ssh db ansible_host=server2.company.com ansible_connection=winrm mail ansible_host=server3.company.com ansible_connection=ssh web2 ansible_host=server4.company.com ansible_connection=winrm
ansible_port ise hedef bilgisayarda farklı bir port değeri kullanılıyorsa onu bizim tarafımızdan girmemiz gerektiği için kullanılmaktadır. ansible_user ise hedef bilgisayara bağlanırken kullanacağımız kullanıcı bilgisi, ansible_ssh_pass ise bağlantı sağlanırken kullanılacak parola bilgisinin yazıldığı alandır. Cleartext olarak parolanın buraya yazılması tabiki tavsiye edilmemektedir. Passwordless olacak şekilde ssh key tabanlı bağlantı kurmak en güvenli yolu olacaktır.
web ansible_host=server1.company.com ansible_connection=ssh ansible_user=root db ansible_host=server2.company.com ansible_connection=winrm ansible_user=admin mail ansible_host=server3.company.com ansible_connection=ssh ansible_ssh_pass=P@ssw0rd web2 ansible_host=server4.company.com ansible_connection=winrm ansible_user=admin
Biraz deneme yapalım. Aşağıda server1,2,3,4 olarak tanımlı basit bir envanter dosyamızın oluşmuş hali mevcut.
# Sample Inventory File server1.company.com server2.company.com server3.company.com server4.company.com
Envanter dosyamızda bulunan sunucuların aliaslarını tanımlayalım.
# Sample Inventory File web1 ansible_host=server1.company.com web2 ansible_host=server2.company.com web3 ansible_host=server3.company.com db1 ansible_host=server4.company.com
Web sunucularımız linux ancak db sunucumuz windows tabanlı. ansible_connection, ansible_user ve parolaları tanım çin tek tek eklememiz gerekmekte.
Alias | Host | Connection | User | Password |
---|---|---|---|---|
web1 | server1.company.com | SSH | root | Password123! |
web2 | server2.company.com | SSH | root | Password123! |
web3 | server3.company.com | SSH | root | Password123! |
db1 | server4.company.com | Windows | administrator | Password123! |
Not: linux bağlantısında parola bilgisi
ansible_ssh_pass
ve windows bağlantısında parola bilgisi bu şekilde belirtilir.ansible_password
.
# Sample Inventory File web1 ansible_host=server1.company.com ansible_connection=ssh ansible_user=root ansible_ssh_pass=Password123! web2 ansible_host=server2.company.com ansible_connection=ssh ansible_user=root ansible_ssh_pass=Password123! web3 ansible_host=server3.company.com ansible_connection=ssh ansible_user=root ansible_ssh_pass=Password123! db1 ansible_host=server4.company.com ansible_connection=winrm ansible_user=administrator ansible_password=Password123!
Şimdi ise webserver ile db sunucusunu gruplayalım envanterimiz içerisinde,
# Sample Inventory File # Web Servers web1 ansible_host=server1.company.com ansible_connection=ssh ansible_user=root ansible_ssh_pass=Password123! web2 ansible_host=server2.company.com ansible_connection=ssh ansible_user=root ansible_ssh_pass=Password123! web3 ansible_host=server3.company.com ansible_connection=ssh ansible_user=root ansible_ssh_pass=Password123! # Database Servers db1 ansible_host=server4.company.com ansible_connection=winrm ansible_user=administrator ansible_password=Password123! [web_servers] web1 web2 web3 [db_servers] db1
all_servers adında bir grup oluşturup, bir önceki grupları bu gruba üye olarak ekleyelim,
# Sample Inventory File # Web Servers web1 ansible_host=server1.company.com ansible_connection=ssh ansible_user=root ansible_ssh_pass=Password123! web2 ansible_host=server2.company.com ansible_connection=ssh ansible_user=root ansible_ssh_pass=Password123! web3 ansible_host=server3.company.com ansible_connection=ssh ansible_user=root ansible_ssh_pass=Password123! # Database Servers db1 ansible_host=server4.company.com ansible_connection=winrm ansible_user=administrator ansible_password=Password123! [web_servers] web1 web2 web3 [db_servers] db1 [all_servers:children] web_servers db_servers
Son olarakta aşağıda verilen tabloyu ansible inventory formatında oluşturalım,
Server Alias | Server Name | OS | User | Password |
---|---|---|---|---|
sql_db1 | sql01.xyz.com | Linux | root | Lin$Pass |
sql_db2 | sql02.xyz.com | Linux | root | Lin$Pass |
web_node1 | web01.xyz.com | Win | administrator | Win$Pass |
web_node2 | web02.xyz.com | Win | administrator | Win$Pass |
web_node3 | web03.xyz.com | Win | administrator | Win$Pass |
Gruplama da bu şekilde olacak,
Group | Members |
---|---|
db_nodes | sql_db1, sql_db2 |
web_nodes | web_node1, web_node2, web_node3 |
boston_nodes | sql_db1, web_node1 |
dallas_nodes | sql_db2, web_node2, web_node3 |
us_nodes | boston_nodes, dallas_nodes |
# Sample Inventory File # Web Servers web_node1 ansible_host=web01.xyz.com ansible_connection=winrm ansible_user=administrator ansible_password=Win$Pass web_node2 ansible_host=web02.xyz.com ansible_connection=winrm ansible_user=administrator ansible_password=Win$Pass web_node3 ansible_host=web03.xyz.com ansible_connection=winrm ansible_user=administrator ansible_password=Win$Pass # DB Servers sql_db1 ansible_host=sql01.xyz.com ansible_connection=ssh ansible_user=root ansible_ssh_pass=Lin$Pass sql_db2 ansible_host=sql02.xyz.com ansible_connection=ssh ansible_user=root ansible_ssh_pass=Lin$Pass # Groups [db_nodes] sql_db1 sql_db2 [web_nodes] web_node1 web_node2 web_node3 [boston_nodes] sql_db1 web_node1 [dallas_nodes] sql_db2 web_node2 web_node3 [us_nodes:children] boston_nodes dallas_nodes