DevopsSunucu

Mini Ansible Cheatsheet


# Envanterle ping at

ansible all -i inventory.ini -m ping

# Belirli gruba modül çalıştır

ansible web -i inventory.ini -m shell -a "uptime"

# Playbook çalıştır

ansible-playbook -i inventory.ini playbook.yml



En Sık Kullanılan Modüller:

Modülİşlev
pingErişim testi
fileDosya/dizin oluştur, sil, izin ata
copyYerel dosyayı hedefe kopyala
templateJinja2 şablonunu hedefe işleyip gönder
yum/aptPaket yükle/sil (RHEL/Debian)
serviceServis başlat/durdur/yükle
userKullanıcı oluştur/sil
lineinfileDosyaya satır ekle/sil
gitRepo klonla/pull yap
command / shellKomut çalıştır (ama dikkatli ol)




2. Günlük Hayatta Lazım Olacak 20 Görev Örneği

GörevModül + Kısa Açıklama
1. Ping testiansible all -m ping
2. /tmp/testfile oluşturfile: path=/tmp/testfile state=touch
3. htop yükleapt: name=htop state=present update_cache=yes
4. Servis başlatservice: name=nginx state=started
5. Kullanıcı ekleuser: name=deniz shell=/bin/bash
6. Dosya kopyalacopy: src=./dosya.txt dest=/tmp/
7. Satır eklelineinfile: path=/etc/sysctl.conf line=”vm.swappiness = 10″
8. Git repo klonlagit: repo=https://… dest=/opt/code/
9. Dosya izinlerini ayarlafile: path=/tmp/testfile mode=0644
10. Komut çalıştırshell: echo hello > /tmp/hello.txt
11. Paket kaldıryum: name=httpd state=absent
12. Dosya silfile: path=/tmp/oldfile state=absent
13. Cron job eklecron: name=”log temizle” minute=0 hour=1 job=”rm -f /tmp/*.log”
14. .env dosyası deploy ettemplate: src=env.j2 dest=/app/.env
15. SSH anahtarı dağıtauthorized_key modülü
16. Mount işlemi yapmount: path=/mnt state=mounted src=…
17. Firewall kuralıufw: rule=allow port=22
18. Sistem yeniden başlatreboot: modülü
19. Disk alanı kontrolücommand: df -h
20. Varsayılan gateway kontrolü`command: ip route



3. Playbook ve Envanter Örnekleri

[web]
web1 ansible_host=192.168.10.10 ansible_user=deniz ansible_ssh_pass=parola

[db]
db1 ansible_host=192.168.10.11 ansible_user=deniz ansible_ssh_pass=parola

 Playbook Örneği (playbook.yml):

---
- name: Nginx yükle ve başlat
  hosts: web
  become: true
  tasks:
    - name: Nginx paketi yüklensin
      apt:
        name: nginx
        state: present
        update_cache: yes

    - name: Servis başlatılsın
      service:
        name: nginx
        state: started




 Bonus: Hızlı test etmek için komut

ansible web -i inventory.ini -m shell -a "hostname && uptime"



Basit Web Sunucusu Otomasyonu (Ubuntu tabanlı)

Amaç:

Bir veya daha fazla sunucuda:

  1. Nginx kurulumu
  2. Varsayılan index.html dosyasını deploy et
  3. Servisi başlat
  4. Web sayfasının içeriği kontrol edilebilir mi, test et



Proje Yapısı:

web-setup/
├── inventory.ini
├── playbook.yml
├── files/
│   └── index.html

inventory.ini

[web]
web1 ansible_host=192.168.10.10 ansible_user=ubuntu ansible_ssh_pass=Par0la!

 

files/index.html(ornek icerik)

<!DOCTYPE html>
<html>
<head><title>Welcome</title></head>
<body>
  <h1>Welcome to Ansible Web Server!</h1>
</body>
</html>

playbook.yml






---
- name: Setup Nginx web server and deploy static site
  hosts: web
  become: true

  tasks:
    - name: Update apt cache
      apt:
        update_cache: yes

    - name: Install nginx
      apt:
        name: nginx
        state: present

    - name: Copy custom index.html
      copy:
        src: files/index.html
        dest: /var/www/html/index.html
        owner: www-data
        group: www-data
        mode: '0644'

    - name: Ensure nginx is started and enabled
      service:
        name: nginx
        state: started
        enabled: yes

    - name: Check if index page is served
      uri:
        url: http://localhost
        return_content: yes
      register: webpage

    - name: Debug response
      debug:
        msg: "{{ webpage.content | truncate(80) }}"



Çalıştırma

cd web-setup/
ansible-playbook -i inventory.ini playbook.yml

Ansible Galaxy

Ansible Galaxy Nedir?

Ansible Galaxy, tıpkı bir uygulama mağazası gibi düşünebileceğiniz, açık kaynak Ansible rol deposudur.

İçinde binlerce sistem yöneticisi, DevOps uzmanı ve topluluk geliştiricisi tarafından yazılmış, kullanıma hazır Ansible rolleri bulunur.

  • Web sunucusu kurmak
  • Docker ortamı hazırlamak
  • PostgreSQL mi, MongoDB

şeklinde görevleri otomatize eder.

Ubuntu tabanlı bir sunucuya nginx kurmak istiyoruz. Ama elle uğraşmak istemiyoruz.

Bunun yerine community tarafından yazılmış ve test edilmiş bir rolü kullanacağız.

 Rolü İndir: 

geerlingguy.nginx

ansible-galaxy install geerlingguy.nginx


Bu komut, ilgili rolü $HOME/.ansible/roles/ klasörüne indirir.



Proje Yapısı Oluştur

mkdir -p galaxy-demo && cd galaxy-demo
touch inventory.ini playbook.yml

inventory.ini

[web]
web1 ansible_host=192.168.10.10 ansible_user=ubuntu ansible_ssh_pass=Par0la!

SSH key kullanıyorsan ansible_ssh_private_key_file ile güncelle.



Playbook: Galaxy Rolünü Çağır

# playbook.yml
---
- name: Install NGINX using Ansible Galaxy role
  hosts: web
  become: true
  roles:
    - geerlingguy.nginx


Bu kadar basit. Hiçbir task yazmadık. Sadece rolü çağırdık.

ansible-playbook -i inventory.ini playbook.yml



Neden Galaxy Kullanmalısın?

AvantajAçıklama
🔁 Yeniden kullanılabilirHer projede tekrar yazmana gerek yok
⏱ Zaman kazancı20 task yerine 1 satır
✅ Test edilmiş yapıTopluluk ve yazarlar tarafından denenmiş
🧩 Modülerlikİster genişlet, ister özelleştir



Ansible AWX Kurulumu:

Ansible AWX, Red Hat Ansible Tower’ın açık kaynaklı versiyonudur.

Modern bir web arayüzü, REST API, rol tabanlı erişim kontrolü, zamanlanmış görevler ve daha fazlasını sağlar.

Kısaca:

Ansible’ı kurumsal seviyeye çıkaran yönetim panelidir.



Gereksinimler
AWX artık tamamen container tabanlı (Kubernetes, Docker Compose) kurulmaktadır.

Minimum:

Git, Python3, make, ansible komutları

2 vCPU / 4 GB RAM (demo için)

Docker (ya da Podman)

Docker Compose



Kurulum Adımları (Kubernetes üzerinde)

 Kubernetes Kümesi Kurulu Olmalı

  • On-Prem (RKE2, K3s, MicroK8s)
  • Cloud (EKS, AKS, GKE)

AWX Operator Kurulumu

git clone https://github.com/ansible/awx-operator.git
cd awx-operator
export NAMESPACE=awx
make deploy






AWX Custom Resource (CR) Tanımı

# awx.yml
apiVersion: awx.ansible.com/v1beta1
kind: AWX
metadata:
  name: awx
spec:
  service_type: LoadBalancer  # NodePort veya ClusterIP de olabilir
  ingress_type: none
  hostname: awx.example.com   # İsteğe bağlı, Ingress kullanıyorsan gerekir
  admin_user: admin
  admin_password_secret: awx-admin-password
  postgres_storage_class: nfs-client

Gerekli secret’ları oluştur:

kubectl create secret generic awx-admin-password \
  --from-literal=password='StrongPa$$w0rd!' -n awx



WX CR’yi Apply Et

kubectl apply -f awx.yml -n awx



 Erişim

kubectl get svc -n awx

Servis tipi LoadBalancer ise IP adresi üzerinden erişebilirsin.

Aksi halde Ingress tanımı oluşturman gerekir.



Kurulumdan Sonra Yapılacaklar
DNS yapılandırması (FQDN üzerinden erişim için)

SSL sertifikası ekleme (Ingress + cert-manager)

Vault/secrets entegrasyonu

Git repo bağlantıları (SCM credentials)

Monitoring (Prometheus/Grafana entegrasyonu opsiyonel)



High Availability Kurulumu

AWX Operator 2.x+ ile birlikte HA destekleniyor:

spec:
  replicas: 3
  task_replicas: 3
  web_replicas: 2


Bunlar ile hem web hem task pod’ları HA olarak ayağa kalkar.

İlgili Makaleler

Bir yanıt yazın

E-posta adresiniz yayınlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir

Başa dön tuşu