Ansible

Ansible

ยท

2 min read

Ansible

Puppet

Push Mechanism

Pull Mechanism

Agentless

Master | Slave

Uses YAML Language

Use Puppet Language

User-Friendly

Not User-Friendly

Additional Features:

  • Python-based: Ansible is built using Python, leveraging its robustness and wide community support.

  • Ansible Galaxy: A repository where users can find and share Ansible roles, simplifying automation tasks.

  • Protocol Support: Supports SSH for Linux and

    WinRM for Windows, ensuring versatile connectivity across different platforms.

Ansible Ad Hoc Commands

Inventory:

  • An inventory file lists remote server IPs and groups them for organizational purposes.

  • Example: inventory

      [webserver]
      789.455.45.5
    
      [dbserver]
      75.658.48.3
    

Example Command:

  • Execute a shell command across all servers in the inventory:

      ansible -i inventory all -m shell -a "sudo devopsclass"
    
    • -m: Modules used for specific tasks.

    • -a: Arguments passed to modules.

Ansible Playbook

Example first-playbook.yml:

---
- name: Install Nginx
  hosts: all
  become: true

  tasks:
    - name: Install Nginx
      apt:
        name: nginx
        state: present

    - name: Start Nginx
      service:
        name: nginx
        state: started

Running first-playbook.yml:

  • Execute the playbook against servers listed in the inventory:

      ansible-playbook -i inventory first-playbook.yml
    

Verbose Mode:

  • Use -v or -vvv for increasing verbosity, aiding in understanding tasks executed by Ansible.

Ansible Galaxy

Overview:

  • It's a command-line tool and online repository where you can search for roles developed by other users, contribute your own roles.

Roles in Ansible:

  • Ansible Roles are a way of organizing your Ansible tasks, handlers, variables, and other configurations in a reusable and structured manner.

Example Role Initialization:

ansible-galaxy role init kubernetes

Directories and Files Created:

  • defaults/main.yml: Default variables.

  • files/: Static files for distribution.

  • handlers/main.yml: Handlers triggered by tasks.

  • meta/main.yml: Role metadata including dependencies.

  • tasks/main.yml: Main list of tasks.

  • templates/: Jinja2 templates directory.

  • tests/: Testing directory.

  • vars/: Additional variables.

  • README.md: Role documentation and usage guidelines.

Feel free to share and spread the knowledge! ๐ŸŒŸ๐Ÿ˜Š Enjoy Learning! ๐Ÿ˜Š

ย