Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Ansible Basics

1. Introduction

Ansible is an open-source automation tool that simplifies the process of managing and configuring systems, deploying applications, and orchestrating complex workflows.

2. Key Concepts

2.1 What is Ansible?

Ansible is an automation framework that uses a declarative language to define the desired state of your systems.

2.2 Agentless

Ansible operates in an agentless manner, using SSH for communication with remote machines.

2.3 Idempotency

Idempotency ensures that running the same Ansible playbook multiple times does not produce different results.

3. Installation

3.1 Installing Ansible on Ubuntu

sudo apt update
sudo apt install ansible

3.2 Installing Ansible on CentOS

sudo yum install epel-release
sudo yum install ansible

3.3 Verify Installation

ansible --version

4. Playbooks

Playbooks are YAML files that define the automation tasks. Each playbook consists of one or more plays.

4.1 Example Playbook

- hosts: webservers
  tasks:
    - name: Ensure Apache is installed
      yum:
        name: httpd
        state: present

5. Inventory

The inventory file lists the hosts or groups of hosts to be managed by Ansible.

5.1 Inventory File Example

[webservers]
192.168.1.10
192.168.1.11

6. Modules

Ansible modules are reusable scripts that can be used to perform various tasks.

6.1 Using Modules in Playbooks

- hosts: all
  tasks:
    - name: Create a directory
      file:
        path: /tmp/mydir
        state: directory

7. Best Practices

Tip: Use version control for your playbooks and roles to keep track of changes.
  • Keep playbooks simple and modular.
  • Use descriptive names for tasks and plays.
  • Test playbooks in a development environment before production.

8. FAQ

What is Ansible used for?

Ansible is used for configuration management, application deployment, task automation, and orchestration.

Does Ansible require agents on managed nodes?

No, Ansible does not require agents; it uses SSH for communication.

Can Ansible be used for Windows systems?

Yes, Ansible can manage Windows systems using WinRM.