💻
Cheet Sheets
  • 🦁Başlarken
  • 🟧DevOps Pre-Requisite
    • ❤️Why Linux? | Linux Basics #1
    • 💛Vi Editor | Linux Basics #2
    • 💙Basics Commands | Linux Basics #3
    • 🧡Package Managers | Linux Basics #4
    • 💚Services | Linux Basics #5
    • 💛Networking Basics
    • 🤎DNS Basics
    • 🩶Applications Basics
    • 🟨Java introduction
    • 🟩NodeJS Introduction
    • 🟦Python Introduction
    • 🟪GIT Introduction
    • 🟧Apache Web Server Introduction
    • ⬛Apache Tomcat
    • 🟫Python Flask
    • 🟥Node.js Express App
    • 🟨Databases
    • 🟩MySQL
    • 🟪MongoDB
    • 🟨SSL & TLS
    • 🟦YAML & JSON - JSON Path
    • ⬛Labs Resources
  • 🍎Kubernetes
    • 🍏Kubernetes: Nedir ?
    • 🍒Bileşenler
    • 🍵Kubectl ve Versiyon
    • ❤️Pod
    • 🏷️Label ve Selector
    • 🔎Annotation
    • 📲Namespaces
    • 📦Deployments
    • 🔁ReplicaSet
    • 🔙Rollout & Rollback
    • 🌐Networking - 1
    • 🌏Service
    • 🛠️Liveness Probe & Readiness Probe
    • 🥐Resource Limits
    • 💰Environment Variables
    • 📃Ephemeral Volumes
    • 🔑Secrets
    • 🌂ConfigMap
    • 🖥️Node Affinity
    • 🔌Pod Affinity
    • ✍️Taint and Toleration
    • 🔦DaemonSet
    • 🧀PV/PVC
    • 🌜Storage Class
    • 🗿StatefulSet
    • 🕹️Job & Cronjob
    • 🔐Authentication
    • 📏Role-based access control (RBAC)
    • 🈷️Service Account
    • 📈Ingress
    • 📂ImagePullPolicy & ImageSecret
    • 📖Static Pods
    • 🌐Network Policy
    • 🫐Helm Nedir?
    • 📽️Prometheus Stack - Monitoring
    • 💼EFK Stack - Monitoring
    • 🥳CRD & Operator
  • 🧑‍⚕️GIT & GITHUB
    • 👉Girizgah
    • 🌴Branch
    • 🤝Merge
    • 🤔Conflict - Rebase
    • 🇸🇴Alias
    • 🛑Gitignore
    • 🥢Diff
    • ◀️Checkout
    • 🔦Stash
    • 👉Other
  • ☁️AWS
    • 🪣S3
    • 🚙EC2
    • ⚖️ELB
    • 🤝Auto Scaling
    • 🗄️EFS
    • 🔐VPC
    • 🎆CloudFront
    • ❤️Route53
    • 🦈RDS
    • 🏢ElastiCache
    • 🔭CloudWatch
    • 👀CloudTrail
    • 📃CloudFormation
    • 🔕SNS
    • 📬SQS
    • 🎇SWF
    • 📧SES
    • 📦Kinesis
    • 📐AWSConfig
    • 👩‍🏭OpsWork
    • 🚀Lambda - Api Gateway
    • 📌ECS - EKS
    • 🔑KMS
    • 📂Directory Service
    • 🏐Snowball
    • 💾Storage Gateway
    • 💽Volume Gateway
    • 📼Tape Gateway
    • 🏠Organizations
    • 🔙Backup-Transfer-CloudShell
    • 🆔IAM
    • 📀DataSync
    • 🗃️FSx
    • 🎒Aurora Serverless
    • 🌐Global Accelerator
    • 💪HPC
    • 🎰Outposts
    • 🗼Others
  • 👨‍🔬Ansible
    • 👉Girizhah
    • 📔YAML
    • ⚙️Komponentler
    • 🎒Inventory
    • 🏑ad-hoc
    • ▶️Playbook
  • 👨‍⚕️PROMETHEUS
    • 📈Terminoloji
    • 🦯Ubuntu 20.04 Prometheus Kurulum
    • 🗒️prometheus.yml dosyasına ilk bakış:
    • 🧭promQL up query
    • 📇Exporters
    • 🔦promQL Data Types
    • 🦯Selectors & Matchers
    • 🔢Binary Operators
    • 💀ignoring and on
    • ✍️Aggregation Operators
    • 🧠Functions
    • 🖊️Alıştırma
    • 💻Client Libraries
    • 🐍Examining the data of our Python application
    • 🐐Examining the data of our GO application
    • ⏺️Recording Rules
    • 💡rate functions
    • ⏰Alerting
    • ⌚Alert Routing
    • ⏰Slack integration with Prometheus
    • 🤯PagerDuty integration with Prometheus
    • ◼️BlackBox exporter
    • 📍Push Gateway
    • 🪒Service Discovery
    • 🧊kube cadvisor with external prometheus
    • 👉aws with prometheus
    • ☁️CloudWatch Exporter
    • 👨‍🚒mysql exporter
    • 🛃Custom exporter with Python
    • ⚙️Prometheus with HTTP API
    • 🤖Prometheus Federation For Kubernetes
    • 📺Grafana
    • ⁉️Prometheus: Ne zaman kullanılmalı? Ne zaman kullanılmamalıdır?
  • 🍪Sheets
    • 🛳️Docker Sheets
    • 🐐Kube Sheets
  • 🔢12 Factor APP
    • 🏗️Introduction
    • 1️⃣Codebase
    • 2️⃣Dependencies
    • 3️⃣Concurrency
    • 4️⃣Processes
    • 5️⃣Backing Services
    • 6️⃣Config
    • 7️⃣Build, release, run
    • 8️⃣Port binding
    • 9️⃣Disposability
    • 🔟Dev/prod parity
    • 🕚Logs
    • 🕛Admin processes
  • ☁️Azure 104
    • 👨‍👨‍👧‍👧Azure Active Directory ( Entra ID )
    • 💰Subscriptions
    • 🌎Virtual Network (VNET)
    • 💻Virtual Machines
    • 🧑‍🌾Load Balancing
    • 🥍Network Advanced
    • 🪡Automating Deployment and Configuration
    • 💂Securing Storage
    • 📓Administering Azure Blobs and Azure Files
    • 🔧Managing Storage
    • 🎁App Service
    • 🛳️Azure Container
    • 🥇Backup And Recovery
    • 🪐Network Watcher
    • ⏰Resource Monitoring And Alerts
  • ⛅AZURE 305
    • 🆔identity and access management
    • 💼Desing Azure AD (Entra ID)
    • 👨‍💼Desing for Azure B2B
    • 🛃Desing for Azure B2C
    • 💳Design for MFA and Conditional Access
    • ⛑️Design for Identity Protection
    • 🚶Access Reviews
    • 🚦Managed identity Demostration
    • 🔐Key Vault Demostration
    • 👑Governance hierarchy
    • 💠Design for Management Groups
    • 🔑Desing for Subscriptions
    • 🍇Desing for resource groups
    • 📟Design for resource tags
    • 🚷Azure Policy & RBAC
    • 🫐Desing For Blueprints
    • 🪡Desing for Virtual Networks
    • 🛫Design for on-premises connectivity to Azure
    • 🔽Design for network connectivity
    • 📦Design for application delivery
    • 🥞Design for network security and application protection
    • 📕Choose a compute solution
    • 🌊Design for virtual machines
    • 🔋Azure Batch Demostration
    • 🛰️Design for Azure App Service
    • ⛲Design for Azure Container Instances
    • 🎢Design for Azure Kubernetes Service
    • 📠Azure Functions Demostration
    • 💪Azure Logic Apps Demostration
    • 🧑‍💼Design for data storage
    • 🎞️Design for Azure storage accounts
    • 🌟Choose the storage replication
    • 📹Azure blob storage - Lifecycle & immutable demo
    • 🥌Azure Files Demostration
    • 🕸️Design Azure disks
    • 🦼Design for storage security
    • 🔮Azure Table Storage And Cosmos DB Demostration
    • 🟧Azure SQL Solutions
    • 🎡Azure SQL Database - Purchasing models
    • 🕯️Database availability
    • 📜Data security strategy
    • 🧮Azure SQL Edge
    • 🚲Azure Data Factory
    • 🔅Azure Data Lake Storage
    • 🧘‍♂️Azure Databricks
    • 🎒Azure Synapse Analytics
    • 🅰️Azure Stream Analytics
    • 📼Data flow strategy
    • 🍥Cloud Adoption Framework
    • ☣️Azure Migration Framework
    • 🦿Assessing workloads
    • 🪡Migration tools
    • 🤖Azure Database migration
    • 👥Storage migration
    • 👜Azure Backup
    • ⏲️Azure Blob Backup and Recovery
    • 💈Azure files backup and recovery
    • 🎞️Azure VM backup and recovery
    • 🧺Azure SQL backup and recovery
    • ⏰Azure Site Recovery
    • 📩Differentiate event and message
    • ✈️Azure messaging solutions
    • 🚜Event Hub
    • 🥍Application optimization solution
    • 🎁Application lifecycle
    • 📺Azure Monitor
    • 🅱️Log Analytics
    • 👥Azure workbooks and Insights
    • 🚌Azure Data Explorer
  • Github Actions
    • Github Actions Nedir?
    • Workflow & Schedule Triggers
    • Single and Multiple Events
    • Manuel Events
    • Webhook Events
    • Conditional Keywords For Steps
    • Expressions - 1
    • Expressions - 2
    • Runners
    • Workflow Commands
    • Workflow Context
    • Dependent Jobs
    • Encrypted Secrets
    • Configuration Variables
    • Default & Custom Env Varb
    • Set Env Varb with Workflow Commands
    • Github Token Secret
    • Add Script to workflow
    • Push Package #1
    • Push Package #2 Docker
    • Service Containers
    • Routing workflow to runner
    • CodeQL Step
    • Caching Package and Dependency Files
    • Remove workflow Artifact
    • Workflow Status Badge
    • Env Protection
    • Job Matrix Configuration
    • Disable & Delete Workflows
    • Actions type for Action
    • Inputs and Outputs for actions
    • Action Versions
    • Files and Directories for Actions
    • Exit Codes
    • Reusable Workflow & Reuse Templates for Actions and Workflows
    • Configure Self Hosted Runners for Enterprise
  • Loki
    • What is Loki?
    • Architecture of Loki
    • Install Loki For Ubuntu
    • Install Promtail For Ubuntu
    • Querying Logs
    • Loki in Kubernetes
    • Deploying Loki in Kubernetes
    • Connecting to Grafana
    • Viewing Kubernetes logs
    • Promtail Customize & Pipeline
  • Ansible
    • Ansible Introduction
    • Introduction to Ansible Configuration Files
    • Ansible Inventory
    • Inventory Formats
    • Ansible Variables
    • Variable Types
    • Registering Variables and Variable Precedence
    • Variable Scoping
    • Magic Variables
    • Ansible Facts
    • Ansible Playbooks
    • Verifying Playbooks
    • Ansible lint
    • Ansible Conditionals
    • Ansible Conditionals based on facts, variables, re-use
    • Ansible Loops
    • Ansible Modules
    • Introduction to Ansible Plugins
    • Modules and Plugins Index
    • Introduction to Handlers
    • Ansible Roles
    • Ansible Collections
    • Introduction to Templating
    • Jinja2 Templates for Dynamic Configs
Powered by GitBook
On this page
  • 1. Neden Loops Kullanıyoruz?
  • 2. Basit Bir Örnek: Düz Liste (“loop:”)
  • 3. Liste Elemanları Daha Karmaşık Olursa (Dictionaries)
  • 4. “with_items” vs “loop”
  • 5. Diğer “with_” Yönergeleri
  • 6. Özet
  • Lookup Plugin
  • 1. Lookup Plugin Nedir?
  • 2. Örnek: with_dict
  • 3. Sık Kullanılan Lookup Plugin’leri
  • 4. Özet

Was this helpful?

  1. Ansible

Ansible Loops

1. Neden Loops Kullanıyoruz?

Ansible’da bir görevi (örn. kullanıcı oluşturma, paket kurma gibi) birden fazla öğe (örn. birçok kullanıcı, birçok paket) için uygulamak istediğinizde, her öğe için ayrı bir “task” kopyalamak zorunda kalmazsınız. Loops kullanarak tekrarlayan görevlerden kurtulur, daha az ve okunaklı kod yazarsınız.

Örnek: Kullanıcı oluşturma işini tek satırda tanımlayıp, listede yer alan tüm kullanıcılar için otomatik tekrarlanmasını sağlayabilirsiniz.

2. Basit Bir Örnek: Düz Liste (“loop:”)

2.1 Tek Öğe İçin Görev

- name: Create a single user
  hosts: localhost
  tasks:
    - user:
        name: joe
        state: present

Bu şekilde tek bir kullanıcı “joe” oluşturuluyor.

2.2 Birden Çok Öğe Olursa (Kopyalama Sorunu)

Beş, on, yirmi kullanıcı oluşturmak istersek aynı görevi tekrar tekrar kopyalamak zorunda kalırız:

    - user:
        name: joe
        state: present
    - user:
        name: george
        state: present
    - user:
        name: ravi
        state: present
    ...

Bu uzun, tekrarlı ve bakımı zor.

2.3 loop: Kullanın

- name: Create multiple users
  hosts: localhost
  tasks:
    - user:
        name: "{{ item }}"
        state: present
      loop:
        - joe
        - george
        - ravi
        - mani
        - kiran
  • loop parametresine bir liste (array) veriyoruz: [joe, george, ravi, ...]

  • Ansible, bu listeyi tek tek döngüden geçirir, her yinelemede item adlı özel bir değişkende (örn. joe, sonra george, vb.) tutar.

  • name: "{{ item }}" → her yinelemede yeni kullanıcı adı.

Sonuç: Daha kısa ve temiz bir playbook!

3. Liste Elemanları Daha Karmaşık Olursa (Dictionaries)

Bazen yalnızca kullanıcı adı değil, ek bilgiler (örn. UID) de vermek isteyebilirsiniz. Bu durumda döngü öğeleri birer dictionary (sözlük) olabilir:

- name: Create users with specific UID
  hosts: localhost
  tasks:
    - user:
        name: "{{ item.name }}"
        uid: "{{ item.uid }}"
        state: present
      loop:
        - { name: "joe",    uid: 1010 }
        - { name: "george", uid: 1011 }
        - { name: "ravi",   uid: 1012 }
        - { name: "mani",   uid: 1013 }
  • Her döngü öğesi, { name: "...", uid: "..." } gibi iki alan içerir.

  • Ansible her yinelemede item değişkenini bu dictionary ile doldurur. item.name → “joe”, item.uid → 1010 vb.

Nasıl Çalıştığını Görselleştirmek

Döngü, bu playbook’u “ardışık” bir şekilde çalıştırır ve her seferinde item’a farklı değer atar:

  1. item = {name: "joe", uid: 1010}

  2. item = {name: "george", uid: 1011}

  3. item = {name: "ravi", uid: 1012}

  4. item = {name: "mani", uid: 1013} ... ve her seferinde tek bir “user” görevini uygular.

4. “with_items” vs “loop”

Eskiden Ansible’da döngü yapmak için with_items sıklıkla kullanılırdı:

- user:
    name: "{{ item }}"
    state: present
  with_items:
    - joe
    - george
    - ravi

Günümüzde loop: önerilen yaklaşım. Ancak eski playbook’larda with_items, with_file, with_url vb. görebilirsiniz. Mantık aynıdır, “her öğe için görevi tekrarla” fikrini uygular.

5. Diğer “with_” Yönergeleri

with_file, with_url, with_mongodb vb.

  • with_file: Birden çok dosya yolunu döngüyle işler.

  • with_url: Birden çok URL üzerinde işlem yapar.

  • with_mongodb: Birden çok MongoDB veritabanına bağlanmak için.

Bu “with_*” yöntemleri, Ansible’ın lookup plugin yapısına dayanır. Örneğin with_file ile bir dizi dosyayı tek tek okuyabilir, with_url ile bir dizi URL’yi ziyaret edebilirsiniz.

6. Özet

  1. loop: → Bir liste üzerinde döngü kurar.

  2. item: Döngüdeki her öğeyi temsil eder (string, dictionary vb.).

  3. Sade bir liste → item doğrudan string olur. Dictionary listesi → item.name, item.uid vb. alanlara erişirsiniz.

  4. with_items gibi eski yöntemler benzer amaçla kullanılır, loop daha güncel ve önerilir.

  5. Diğer “with_” direktifleri (like with_file, with_url) → Spesifik data kaynağına iterasyon uygular.


Lookup Plugin

Ansible’daki lookup plugin’ler, “harici bir kaynaktan veya farklı bir veri biçiminden” bilgiyi alıp Ansible içerisinde kullanmanızı sağlayan ufak eklentilerdir. Örneğin with_file, with_url, with_k8s gibi “with_” direktiflerini gördüğünüzde, aslında bunlar birer lookup plugin çağırır ve iterasyona (döngüye) uygun veriyi döndürür.

1. Lookup Plugin Nedir?

  • Amaç: Ansible’ın normalde “inventory” veya “vars” gibi basit yerlerden okuduğu veriyi daha geniş kaynaklardan (dosya sistemleri, veri tabanları, API’ler, bulut servisleri, vs.) dinamik olarak çekebilmenizi sağlamak.

  • Çalışma Mantığı: Her “with_*” direktifi (ör. with_dict, with_file, with_k8s) kendi özel plugin’ini kullanır. Bu plugin, belirtilen kaynaktan döngü yapabileceğiniz bir veri listesi veya veri sözlüğü üretir.

Örnek:

  • with_filetree → Belirttiğiniz bir dizin ağacını (file tree) tarar, orada bulduğu dosyalar hakkında Ansible’da iterasyon yapmayı sağlar.

  • with_dict → Elinizdeki sözlük (dictionary) yapısını döngüleyip key/value çiftlerini item.key, item.value gibi alanlar halinde kullanmanıza izin verir.

  • with_sequence → Otomatik sayı listesi (örn. 1’den 10’a) oluşturmak ve her birinde ayrı işlem yapmak için kullanılır.

Nasıl Çalıştığını Düşünmek

  1. Görevinizde with_XXX (örn. with_dict) parametresi yazarsınız.

  2. Ansible, “XXX” ismindeki lookup plugin’ini çağırır.

  3. Plugin, belirtilen kaynaktan (dictionary, environment variables, vs.) verileri toplar ve bir liste veya sözlük haline getirip Ansible’a döndürür.

  4. Ansible, bu listede veya sözlükteki öğeler üzerinde döngü (loop) işlemi yapar. Her öğeyi item veya benzer bir değişkene atar.

2. Örnek: with_dict

- name: Process dictionary items
  hosts: localhost
  tasks:
    - debug:
        msg: "Key={{ item.key }}, Value={{ item.value }}"
      with_dict:
        my_var1: "Hello"
        my_var2: "World"
        my_var3: "Test"
  • Bu görevde, with_dict plugin’i, { my_var1: "Hello", my_var2: "World", my_var3: "Test" } sözlüğünü key-value çiftleri listesine dönüştürür.

  • Her turda item.key ve item.value geliyor (örn. my_var1 / Hello, my_var2 / World).

3. Sık Kullanılan Lookup Plugin’leri

  • with_filetree: Belirlediğiniz dizin altındaki dosya/yapıları iterasyona sokar.

  • with_env: Ortam değişkenlerini ($ENV_VAR) okuyup döngü kurar.

  • with_inventory_hostnames: Envanterdeki host isimlerine döngü.

  • with_password: Parolalar oluşturmak/okumak için.

  • with_sequence: Bir sayı dizisi (1..N) üzerinde döngü kurmak için.

  • with_subelements: İç içe listeler (nested lists) üzerinde döngü.

Not: Hepsi benzer şekilde çalışır: Farklı kaynaklardan verileri okur, Ansible’a bir liste döndürür, Ansible her öğeyle bir tur görev çalıştırır.

4. Özet

  • lookup plugin: Ansible’ın “dış kaynak / özel format” verilerini alması ve döngü kurması için gerekli küçük eklentilerdir.

  • with_ direktifleri*: Bu plugin’leri kullanım şekli. Örnek: with_dict, with_file, with_url vb.

  • Mantık: Plugin veriyi toplar → Ansible bir liste elde eder → her öğe item şeklinde döngüde kullanılır.

  • Yararları: Daha esnek, dinamik playbook’lar yazmanızı sağlar (farklı veri kaynaklarından kolay veri çekebilirsiniz).

Bu sayede, Ansible’da “with_*” yazdığınızda aslında bir “lookup plugin”’i çağırdığınızı bilmelisiniz. Böylece veri kaynağını plugin aracılığıyla döngüye sokuyor, her turda item değişkeni ile o öğeyi kullanıyorsunuz.

PreviousAnsible Conditionals based on facts, variables, re-useNextAnsible Modules

Last updated 8 days ago

Was this helpful?