Ansible Otomasyon Platformu
Ansible, agentless mimarisi ile Linux/Unix sistemleri otomatize etmeye yarayan güçlü bir araçtır. SSH üzerinden çalışır, hedef sistemlere agent kurmak gerekmez.
Ansible Kurulumu
# RHEL/CentOS
sudo dnf install ansible-core
Python pip ile
pip install ansible
Versiyon kontrolü
ansible --version
Inventory Yapılandırması
# /etc/ansible/hosts veya inventory.yml
[webservers]
web1.example.com ansible_host=192.168.1.10
web2.example.com ansible_host=192.168.1.11
[dbservers]
db1.example.com ansible_host=192.168.1.20
db2.example.com ansible_host=192.168.1.21
[all:vars]
ansible_user=ansible
ansible_ssh_private_key_file=~/.ssh/id_rsa
ansible_python_interpreter=/usr/bin/python3
Ad-hoc Komutlar
# Ping testi
ansible all -m ping
Komut çalıştırma
ansible webservers -m shell -a "uptime"
Paket kurulumu
ansible webservers -m dnf -a "name=httpd state=present" -b
Servis yönetimi
ansible webservers -m service -a "name=httpd state=started enabled=yes" -b
Dosya kopyalama
ansible webservers -m copy -a "src=/local/file dest=/remote/path"
Playbook Örneği
# webserver.yml
---
- name: Web Sunucu Yapılandırması
hosts: webservers
become: yes
vars:
http_port: 80
doc_root: /var/www/html
tasks:
- name: Nginx kurulumu
dnf:
name: nginx
state: present
- name: Nginx yapılandırması
template:
src: templates/nginx.conf.j2
dest: /etc/nginx/nginx.conf
validate: nginx -t -c %s
notify: Restart Nginx
- name: Web içeriği kopyala
copy:
src: files/index.html
dest: "{{ doc_root }}/index.html"
- name: Nginx servisini başlat
service:
name: nginx
state: started
enabled: yes
- name: Firewall kuralı
firewalld:
port: "{{ http_port }}/tcp"
permanent: yes
state: enabled
immediate: yes
handlers:
- name: Restart Nginx
service:
name: nginx
state: restarted
Playbook Çalıştırma
# Normal çalıştırma
ansible-playbook webserver.yml
Verbose mod
ansible-playbook webserver.yml -v
Dry-run (check mode)
ansible-playbook webserver.yml --check
Belirli tag'leri çalıştırma
ansible-playbook webserver.yml --tags "install,configure"
Limit ile çalıştırma
ansible-playbook webserver.yml --limit web1.example.com
Sonuç
Ansible, 100+ sunucuyu dakikalar içinde yapılandırabilir. Idempotent yapısı sayesinde güvenle tekrar çalıştırılabilir.