πŸ”§ DevOps

Ansible

Red Hat Ansible

An IT automation tool. YAML-based playbooks. Agentless configuration management and deployment automation.

πŸ“– Detailed Explanation

Ansible is an IT automation tool developed by Red Hat that performs server configuration management (Configuration Management), application deployment, and orchestration with YAML-based playbooks. Its most notable characteristic is its agentless approach: it performs tasks over SSH alone, without installing any separate software on the target servers.

Ansible's core concepts are Inventory (the list of target servers), Playbook (the tasks to run), Module (an individual unit of work), and Role (a reusable bundle of playbooks). Playbooks are written declaratively, so if you define that "nginx must be installed and running," Ansible checks the current state and performs only the necessary tasks (idempotency).

It is often compared with Terraform, but the two have different roles. Terraform is strong at infrastructure provisioning (creating VMs and networks), while Ansible is strong at installing and configuring software on provisioned servers. In practice, they are used in combination, as in "create EC2 with Terraform -> install nginx with Ansible."

Ansible Galaxy is a repository where the community shares Roles, and for common configurations such as nginx, docker, and kubernetes, you can take and use verified Roles. With AWX (open source) or Ansible Tower (commercial), you can run playbooks from a web UI and manage permissions and even audit logs.

πŸ’» Code Example

# deploy-app.yml - μ›Ή μ• ν”Œλ¦¬μΌ€μ΄μ…˜ 배포 ν”Œλ ˆμ΄λΆ
---
- name: Deploy Web Application
  hosts: webservers
  become: yes  # sudo κΆŒν•œμœΌλ‘œ μ‹€ν–‰
  vars:
    app_version: "{{ lookup('env', 'APP_VERSION') | default('latest') }}"
    app_port: 8080

  tasks:
    - name: Install required packages
      apt:
        name:
          - docker.io
          - docker-compose
          - python3-docker
        state: present
        update_cache: yes

    - name: Create app directory
      file:
        path: /opt/myapp
        state: directory
        mode: '0755'

    - name: Copy docker-compose file
      template:
        src: templates/docker-compose.yml.j2
        dest: /opt/myapp/docker-compose.yml
      notify: Restart application

    - name: Pull latest Docker image
      docker_image:
        name: "myregistry/myapp:{{ app_version }}"
        source: pull
        force_source: yes

    - name: Ensure application is running
      docker_compose:
        project_src: /opt/myapp
        state: present
      register: output

    - name: Wait for application to be healthy
      uri:
        url: "http://localhost:{{ app_port }}/health"
        status_code: 200
      retries: 10
      delay: 5

  handlers:
    - name: Restart application
      docker_compose:
        project_src: /opt/myapp
        state: present
        restarted: yes

# μ‹€ν–‰: ansible-playbook -i inventory/prod deploy-app.yml -e "APP_VERSION=v1.2.3"

πŸ—£οΈ How It's Said in Practice

Senior: "We need to install the monitoring agent on 50 new servers, and I think we can just push it out all at once with Ansible."

Junior: "So I add the new server group to the inventory and just use the existing datadog role as is, right?"

Senior: "Yes, test on just 5 servers first with --limit, and if there are no problems, apply it to everything."

Interviewer: "Do you have experience with Configuration Management tools?"

Candidate: "I mainly used Ansible. Being agentless made it easy to adopt, and since it uses YAML, the learning curve for team members was low. I managed package updates and configuration changes for 200 servers with playbooks, and managed execution history and permissions from the web UI with AWX."

Take a mock interview on this topic β†’ WIA SOOM AI Interview Β· Start for free

Reviewer: "Running apt update every time in this task makes it slow. Please use the update_cache_valid_time option so that it updates only when the cache has expired."

Author: "Oh, right. I'll add cache_valid_time: 3600. And I'll also add the handler I missed."

⚠️ Cautions

πŸ”₯ Real-World Incidents

Mid-sized Company Case 2021

Overwriting all server configurations with a playbook that ignored idempotency

Cause: Overwrote configuration files directly with echo using the shell module, and ran every time because changed_when was not set

Impact: Manually configured settings on 500 servers were reset all at once, causing 12 hours of service instability

Resolution: Manually restored the configuration on each server and restored configuration files from backup

Lesson: Use dedicated modules instead of shell/command, and a dry-run with --check mode before production is mandatory

Startup case Common

Secret exposure through a Vault password Git commit

Cause: Saved the ansible-vault password in a .vault_pass file and omitted it from .gitignore

Impact: DB passwords and API keys were exposed in a public repository

Resolution: Rotated all secrets and completely removed the file from the repository history

Lesson: Use environment variables or a CI/CD secret manager for the vault password, and set up git-secrets hooks

πŸ“ Comprehension Quiz

Q1. What is the most notable characteristic of Ansible?

Q2. What is the way to securely manage secrets (passwords, API keys) in Ansible?

Q3. What is the difference in roles between Terraform and Ansible?

πŸ“š Learn More

πŸ’¬ Questions & Contributions

If this page has any errors or you would like to add something, please let us know!