Ravindra BagaleCourses & study guides Track your progress

Guides

Write Your First Ansible Playbook

Read this guide in: English · मराठी · हिंदी

Why do we need Ansible? Because there is a real problem between developers and operations when every server is configured by hand. Developers ask for “nginx and this file.” Operations — the employee on the server side — SSHs and types different commands each Friday. Config drifts. Ansible applies a desired state from YAML playbooks to hosts in an inventory (usually over SSH) so both sides get ok vs changed — not a new shell novel every time.

Friends, first see the “every server is a snowflake” fight. Then inventory + playbook stick.

Quick answer

Hold this vertical picture:

  1. Developer — needs a known setup for the app.
  2. Operations — server-side person who SSHs and must keep many hosts consistent.
  3. The problem — hand SSH; drift; “it works on server A not B.”
  4. Ansible — inventory names hosts; playbook names desired state; ansible-playbook applies it.
  5. Pocket line: describe what should be true → run twice → calm results.

Language: English · मराठी · हिंदी

Why do we need Ansible?

  1. Hand configuration does not scale past a few servers.
  2. Operations on the server side needs repeatable desired state.
  3. Developers benefit when every environment matches the playbook.

Need Ansible on a control node, SSH to a lab host you own (SSH into EC2).

What does operations have to do with Ansible?

  1. Operations often owns inventory, keys, and sudo rights.
  2. Developers may co-author playbooks — both read changed vs ok.
  3. Never commit private keys.

What is the problem? Suppose we are…

Suppose we are building Instagram (like updates)

Suppose each API host gets packages installed “however someone remembered.” One host missing a file breaks a fraction of users. Operations cannot prove all hosts match.

Suppose we are working at Netflix and we are giving updates

Suppose many hosts must stay identical for frequent releases. Hand SSH is too slow and too error-prone.

Suppose you are working in Infosys on a client project

Suppose client operations asks you to leave servers in a documented state. A playbook is the audit trail — not a private notepad of shell commands.

Ansible control node applies a playbook over SSH You write inventory and playbook on the control node. Ansible connects over SSH and applies the same desired state to managed hosts. Control node inventory.ini playbook.yml ansible-playbook desired state SSH tasks Host A packages / files Host B same desired state idempotent apply Audit trail for client ops — not a private notepad of shell commands

The control node runs a playbook over SSH so managed hosts reach the same desired state (idempotent).

Ansible playbook flow Terminal-style teaching diagram of inventory, playbook ping task and ansible-playbook recap.

Terminal-style teaching diagram: inventory + playbook ping task and ansible-playbook recap (use only hosts you own).

How Ansible answers that same fight

inventory.ini (replace with a host you own — 203.0.113.10 is documentation fiction):

[lab]
REPLACE_WITH_YOUR_HOST ansible_user=ec2-user

playbook.yml:

- name: Ping lab hosts
  hosts: lab
  gather_facts: false
  tasks:
    - name: Connectivity check
      ansible.builtin.ping:
ansible --version
mkdir ansiblelab && cd ansiblelab
# write inventory.ini and playbook.yml
ansible lab -i inventory.ini -m ping
ansible-playbook -i inventory.ini playbook.yml

Safer file task on hosts you own:

- name: Ensure lab marker file
  hosts: lab
  become: true
  tasks:
    - name: Create marker
      ansible.builtin.copy:
        dest: /tmp/devops-ansible.txt
        content: "Ansible lab marker\n"
        mode: "0644"

Run again — second run should show ok more than changed (idempotence).

Ravindra Bagale's Tip

Prefer modules (copy, package, service) over shell: for everything. Modules can skip work already done. Shell is a last resort. Lakshat theva.

Common errors — story pain then fix

Pain from the stories Likely cause Fix
UNREACHABLE SSH/user/key/SG Fix connectivity first
become fail No sudo rights Lab user with sudo; set become correctly
Host key prompting First SSH Accept once; manage known_hosts properly
Module not found Old Ansible Upgrade; use ansible.builtin.
Changed every run Non-idempotent shell Prefer modules

Samajla ka? Why Ansible = stop snowflake servers between developers and server-side operations; Instagram / Netflix / Infosys “suppose”; inventory + playbook + run twice. Calm second run means you are learning it right.

Frequently asked questions

What is Ansible?

An automation tool that pushes desired state to servers, often over SSH, using YAML playbooks.

What is a playbook?

A YAML file listing plays: which hosts, and which tasks to apply.

What is inventory?

The list of hosts and groups Ansible targets.

What does idempotent mean?

Running the same playbook again should not break things — only fix drift.

Do I need agents on servers?

No classic agent: SSH (or winrm) from the control node is enough for many setups.

Ansible vs Terraform?

Ansible often configures servers and apps. Terraform often creates cloud resources. Many teams use both.