Skip to content

jeetendra29gupta/Ansible-Tutorial-Guide

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

Ansible Automation Guide

Introduction

Ansible is an open-source IT automation tool that simplifies configuration management, application deployment, and orchestration. Written in Python, it uses SSH for agentless communication with remote systems.

Key Features

  • Agentless Architecture: No client installation required
  • Push-based Configuration: Master server pushes changes to nodes
  • YAML-based Playbooks: Human-readable automation scripts
  • Extensible Modules: 1000+ built-in modules for various tasks
  • Multi-Platform Support: Manages Linux, Windows, network devices
  • Idempotent Operations: Safe to run multiple times

How Ansible Works

Core Components

  1. Inventory (/etc/ansible/hosts):

    [webservers]
    web01 ansible_host=192.168.1.1
    web02 ansible_host=192.168.1.2
  2. Modules (Execution units):

    ansible all -m apt -a "name=nginx state=present"
  3. Playbooks (YAML automation files):

    - name: Install Apache
      hosts: webservers
      tasks:
        - name: Install package
          apt: 
            name: apache2 
            state: present

Setup Guide

Lab Configuration

  1. Master Node Setup:

    sudo apt update
    sudo apt install ansible
    ssh-keygen -t rsa
    ssh-copy-id user@remote-host
  2. Verify Installation:

    ansible --version
    ansible localhost -m ping
  3. Sample Inventory:

    sudo nano /etc/ansible/hosts

Ad-Hoc Commands Cheatsheet

Command Description
ansible all -m ping Test node connectivity
ansible web -m apt -a "name=nginx state=absent" -b Remove package with sudo
ansible db -m copy -a "src=app.conf dest=/etc/" Copy configuration files
ansible all -m command -a "uptime" Check system uptime
ansible web -m service -a "name=apache2 state=restarted" Restart services

Playbook Examples

Basic Web Server Setup

webserver.yaml:

- name: Configure Web Cluster
  hosts: webservers
  become: yes
  tasks:
    - name: Install Apache
      apt:
        name: apache2
        state: latest
        
    - name: Start Service
      service:
        name: apache2
        state: started
        enabled: yes

Execution Commands:

ansible-playbook --syntax-check webserver.yaml  # Validate syntax
ansible-playbook webserver.yaml --ask-become-pass  # Run with sudo

Configuration Management

File Purpose Location
Main Config Global settings /etc/ansible/ansible.cfg
Inventory Host definitions /etc/ansible/hosts (default)
Roles Reusable components /etc/ansible/roles/

Best Practices

  1. Use version control for playbooks
  2. Implement Ansible Vault for secrets
  3. Use tags for selective execution
  4. Create modular roles for reuse
  5. Test with --check mode before execution

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages