Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions Content/ansible/01-intro.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<!-- omit in toc -->
# Introduction to Ansible

- [What is Ansible?](#what-is-ansible)
- [Core Concepts](#core-concepts)
- [Architecture](#architecture)
- [Installation](#installation)



## What is Ansible?

Ansible is an open-source software provisioning, configuration management, and application-deployment tool enabling infrastructure as code. It runs on many Unix-like systems, and can configure both Unix-like systems as well as Microsoft Windows.

Ansible is agentless, temporarily connecting remotely via SSH or Windows Remote Management (allowing remote PowerShell execution) to do its tasks.

## Core Concepts

### Inventory

Ansible works against multiple managed nodes or "hosts" in your infrastructure at the same time, using a list or group of lists known as inventory. You can pass inventory once at the command line, but most Ansible users create a file and keep it in their repo.

### Modules

Ansible works by connecting to your nodes and pushing out small programs, called "modules" to them. These programs are written to be resource models of the desired state of the system. Ansible executes these modules (over SSH by default), and removes them when finished.

### Playbooks

Playbooks are Ansible’s configuration, deployment, and orchestration language. They can describe a policy you want your remote systems to enforce, or a set of steps in a general IT process.

## Architecture

Ansible's architecture is simple and effective. It relies on:
- **Control Node**: The machine where Ansible is installed and from which you run commands.
- **Managed Nodes**: The devices (servers, network devices, etc.) that you manage with Ansible.
- **Plugins**: Code that expands Ansible's core functionality.
- **Inventory**: A list of managed nodes.
- **Playbooks**: YAML files containing the definition of automation.

## Installation

To install Ansible, you generally need Python installed on your control node.

```bash
sudo apt update
sudo apt install ansible
```

## Resources

- [Official Ansible Documentation](https://docs.ansible.com/)
84 changes: 84 additions & 0 deletions Content/ansible/02-inventory.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<!-- omit in toc -->
# Ansible Inventory

- [What is Inventory?](#what-is-inventory)
- [Inventory Formats](#inventory-formats)
- [INI Format](#ini-format)
- [YAML Format](#yaml-format)
- [Groups and Variables](#groups-and-variables)
- [Default Location](#default-location)

## What is Inventory?

The Ansible inventory is a file (or multiple files) that contains a list of the servers or nodes you want to manage. It allows you to organize your managed nodes into groups, which makes it easier to run automation against specific sets of servers (e.g., "webservers", "dbservers").

## Inventory Formats

Ansible supports multiple formats for inventory, but the most common are **INI** and **YAML**.

### INI Format

The INI format is simple and easy to read.

```ini
mail.example.com

[webservers]
foo.example.com
bar.example.com

[dbservers]
one.example.com
two.example.com
```

### YAML Format

The YAML format is more structured and consistent with Playbooks.

```yaml
all:
hosts:
mail.example.com:
children:
webservers:
hosts:
foo.example.com:
bar.example.com:
dbservers:
hosts:
one.example.com:
two.example.com:
```

## Groups and Variables

You can assign variables to hosts or groups in your inventory.

### Host Variables

```ini
[webservers]
web1 http_port=80 maxRequestsPerChild=808
web2 http_port=303 maxRequestsPerChild=909
```

### Group Variables

```ini
[webservers]
web1
web2

[webservers:vars]
ntp_server=ntp.atlanta.example.com
proxy=proxy.atlanta.example.com
```

## Default Location

The default location for the inventory file is `/etc/ansible/hosts`, but you can specify a different inventory file using the `-i` flag when running Ansible commands.

```bash
ansible all -i my_inventory_file -m ping
```
80 changes: 80 additions & 0 deletions Content/ansible/03-ad-hoc-commands.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<!-- omit in toc -->
# Ansible Ad-Hoc Commands

- [What are Ad-Hoc Commands?](#what-are-ad-hoc-commands)
- [Syntax](#syntax)
- [Common Modules](#common-modules)
- [Ping](#ping)
- [Shell / Command](#shell--command)
- [Package Management (apt/yum)](#package-management-aptyum)
- [Service Management](#service-management)

## What are Ad-Hoc Commands?

Ad-hoc commands are quick, one-off tasks that you run without writing a playbook. They are useful for quick tests, checking system status, or performing simple operations across your inventory.

## Syntax

The basic syntax for an ad-hoc command is:

```bash
ansible [pattern] -m [module] -a "[module options]"
```

- **pattern**: The host or group from your inventory to target (e.g., `all`, `webservers`).
- **-m [module]**: The Ansible module to run (default is `command`).
- **-a "[options]"**: Arguments to pass to the module.

## Common Modules

### Ping

Check connectivity to your hosts. This doesn't use ICMP ping, but rather verifies that Ansible can login and find a usable Python.

```bash
ansible all -m ping
```

### Shell / Command

Run arbitrary commands on remote systems.

**Command module** (default, safer, no shell variables/pipes):
```bash
ansible webservers -m command -a "uptime"
```

**Shell module** (allows pipes, redirects):
```bash
ansible webservers -m shell -a "echo 'hello' > /tmp/hello.txt"
```

### Package Management (apt/yum)

Install or remove packages.

**Install Nginx on Ubuntu/Debian:**
```bash
ansible webservers -m apt -a "name=nginx state=present" --become
```

**Install Git on CentOS/RHEL:**
```bash
ansible dbservers -m yum -a "name=git state=present" --become
```

*(Note: `--become` is used to escalate privileges, like `sudo`)*

### Service Management

Manage services (start, stop, restart).

**Start Nginx:**
```bash
ansible webservers -m service -a "name=nginx state=started" --become
```

**Restart Apache:**
```bash
ansible webservers -m service -a "name=apache2 state=restarted" --become
```
95 changes: 95 additions & 0 deletions Content/ansible/04-playbooks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<!-- omit in toc -->
# Ansible Playbooks

- [What is a Playbook?](#what-is-a-playbook)
- [YAML Syntax](#yaml-syntax)
- [Playbook Structure](#playbook-structure)
- [Example Playbook](#example-playbook)
- [Running a Playbook](#running-a-playbook)
- [Related Labs](#related-labs)


## What is a Playbook?

Playbooks are the files where Ansible code is written. Playbooks are written in YAML format. They allow you to declare configurations, orchestrate steps of any manually ordered process, even on different sets of machines, in a defined order, and launch tasks synchronously or asynchronously.

## YAML Syntax

YAML (YAML Ain't Markup Language) is a human-readable data serialization standard.
- Files start with `---`.
- Indentation is significant (use spaces, not tabs).
- Lists are denoted by hyphens `-`.
- Key-value pairs are separated by a colon `:`.

## Playbook Structure

A playbook is composed of one or more "plays".
- **Play**: Maps a group of hosts to some well-defined roles or tasks.
- **Task**: A call to an Ansible module.

```yaml
---
- name: Update web servers
hosts: webservers
become: yes

tasks:
- name: Ensure apache is at the latest version
apt:
name: apache2
state: latest
```

## Example Playbook

Here is a more complete example that installs and starts Nginx.

```yaml
---
- name: Install and Configure Nginx
hosts: webservers
become: yes

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

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

- name: Create a custom index.html
copy:
content: "<h1>Hello from Ansible!</h1>"
dest: /var/www/html/index.html
```

## Running a Playbook

To run a playbook, use the `ansible-playbook` command:

```bash
ansible-playbook my_playbook.yaml
```

You can also limit execution to a specific host:

```bash
ansible-playbook my_playbook.yaml --limit web1
```

---

## Related Labs

> [!TIP]
> **Practice what you learn!**
> Check out the [Ansible + Vagrant Lab](ansible-labs/ansible+vagrant-lab/README.md) to see real-world playbooks in action.
> * **Master Playbook**: [master-playbook.yaml](ansible-labs/ansible+vagrant-lab/master-playbook.yaml) orchestrates the entire deployment.
> * **Backend Playbook**: [backend-playbook.yaml](ansible-labs/ansible+vagrant-lab/backend-playbook.yaml) configures MongoDB.

Loading