Ansible DevOps

M ChidrupM Chidrup
3 min read

Ansible is an open-source automation tool by Red Hat. It helps automate configuration management, application deployment, and task execution across servers—agentless and simple to use.

“Ansible is to DevOps what Git is to developers.”

Why Choose Ansible?

Agentless – Uses SSH
YAML-based playbooks – Human-readable
Idempotent – Safe to run multiple times
Modular – Rich set of modules
Integrates easily – With Jenkins, Docker, Kubernetes, etc.

CommandPurpose
ansible all -m pingTest connection to all hosts
ansible-inventory --listShow inventory
ansible-playbook site.ymlRun a playbook
ansible web -m shell -a "uptime"Run shell command on ‘web’ group
ansible-playbook --checkDry run
ansible-playbook --syntax-check site.ymlSyntax check

1. What is Ansible and how is it different from other tools like Puppet or Chef?

Answer:
Ansible is agentless, uses SSH, and playbooks are written in YAML. It’s simpler to learn than Chef/Puppet which have steeper learning curves and require agents.


2. What is an Ansible inventory file?

Answer:
It defines the list of hosts and groups for Ansible to manage. It can be static (.ini) or dynamic (cloud-based like AWS EC2).

Example:

iniCopyEdit[web]
web1 ansible_host=192.168.1.10
web2 ansible_host=192.168.1.11

3. What are handlers in Ansible?

Answer:
Handlers are tasks triggered only when notified. They are useful for restarting services after a change.


4. How is Ansible idempotent?

Answer:
Ansible tasks do not make changes if the desired state already exists. This prevents repeated unnecessary execution.


5. What are roles in Ansible?

Answer:
Roles help organize playbooks into reusable components (tasks, handlers, vars, templates). They follow a standard directory structure.

bashCopyEditroles/
  nginx/
    tasks/
    handlers/
    templates/

6. What is a module in Ansible?

Answer:
Modules are units of work (e.g., yum, apt, copy, file, service). Ansible comes with 1000+ built-in modules.


7. How do you encrypt data in Ansible?

Answer:
Use Ansible Vault to encrypt secrets like passwords and keys.

bashCopyEditansible-vault create secrets.yml
ansible-vault edit secrets.yml
ansible-playbook --ask-vault-pass playbook.yml

8. How do you debug an Ansible playbook?

Answer:
Use -v, -vv, or -vvv for verbosity. You can also use the debug module to print variables.


9. What is the difference between vars, defaults, and facts?

  • vars: variables defined in playbooks.

  • defaults: lowest precedence, usually defined in roles.

  • facts: variables gathered automatically from systems using setup module.


10. Can Ansible be used in a CI/CD pipeline?

Answer:
Yes. Ansible integrates with Jenkins, GitLab CI, and GitHub Actions to automate deployments and configuration after code is pushed.


📚 Resources to Level Up

  • Ansible Documentation

  • Ansible Galaxy Roles

  • Install and start Nginx hosts: webservers become: yes tasks:

  • name: Install nginx apt: name: nginx state: present

  • name: Ensure nginx is running service: name: nginx state: started enabled: yes Run it with:

bash Copy Edit ansible-playbook nginx_setup.yml -i inventory.iniAnsible Use Cases

  • Provisioning servers on cloud (AWS, Azure, GCP)

  • Setting up environments (Dev/Test/Prod)

  • CI/CD pipelines with Jenkins or GitHub Actions

  • Configuration drift detection

  • Patch management

  • Application deployment (e.g., NGINX, Node.js apps)Installing Ansible

📍 On Ubuntu/Debian:

bashCopyEditsudo apt update
sudo apt install ansible -y

📍 On CentOS/RHEL:

bashCopyEditsudo yum install epel-release -y
sudo yum install ansible -y

📍 Using pip (cross-platform):

bashCopyEditpip install ansible

✅ Verify Installation:

bashCopyEditansible --version
0
Subscribe to my newsletter

Read articles from M Chidrup directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

M Chidrup
M Chidrup

Certified Azure Cloud Enthusiast and Full Stack Developer with a strong foundation in building secure, scalable cloud-native applications. Passionate about integrating AI and automation in DevOps pipelines and exploring intelligent cloud systems. I specialize in React, Node.js, Azure, Kubernetes, and DevSecOps, and I love solving real-world problems through code, collaboration, and continuous learning.