DEV Community

KALPESH
KALPESH

Posted on

Ansible

Ansible Puppet
Push Mechanism Pull Mechanism
Agentless Master
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.

Configure the Ansible Configuration File

Set the default private key in Ansible’s configuration file (ansible.cfg):

[defaults]
inventory = ~/ansible/hosts
host_key_checking = False
private_key_file = ~/.ssh/ansible_key
remote_user = ubuntu
Enter fullscreen mode Exit fullscreen mode
  • In order to avoid mentioning private key while running ansible-book

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 or hosts (mention path if NOT default)> 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
Enter fullscreen mode Exit fullscreen mode

Running first-playbook.yml:

  • Execute the playbook against servers listed in the inventory:

    ansible-playbook -i <inventory or hosts (mention path if NOT default)> first-playbook.yml
    

    OR

  • If private key & other parameter not configured in ansible.cfg

     ansible-playbook -i ~/ansible/hosts --private-key=~/.ssh/ansible_key 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
Enter fullscreen mode Exit fullscreen mode

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! 😊

Top comments (0)