MOCs

Overview

Ansible is an open-source automation tool, or platform, used for IT tasks such as configuration management, application deployment, intra-service orchestration, and provisioning. It uses no agents and no additional custom security infrastructure, making it easy to deploy.

Documentation

Base Documentation
Ansible Galaxy

Tips and Tricks

  • Dry Run: Use the --check flag to perform a “dry run” of your playbook.
  • Tags: Use tags to run specific parts of your playbook.
  • Vault: Use Ansible Vault to encrypt sensitive data. How to make a vault

Installation

Ansible can be installed on many systems with a simple command. For example, on a Debian-based system:

sudo apt update
sudo apt install ansible

Basic Concepts

  • Control Node: The machine from which Ansible is run.
  • Managed Nodes: The servers being managed with Ansible.
  • Inventory: A list of managed nodes.
  • Modules: Units of scripts that Ansible executes.
  • Tasks: Units of action in Ansible.
  • Playbooks: YAML files for defining, running, and managing configurations and deployments to remote machines.

Running Ad-Hoc Commands

Ad-hoc commands are one-line commands that let you perform tasks quickly without writing a playbook. They are a great way to learn Ansible’s capabilities.

ansible all -m ping ansible <host-pattern> -a "<command>"

Playbooks

Playbooks are the heart of Ansible for running tasks. They describe the desired state of your systems using YAML.

  • Sample Playbook:
---
- name: Update web servers
  hosts: webservers
  tasks:
    - name: Ensure nginx is at the latest version
      ansible.builtin.yum:
        name: nginx
        state: latest

Roles

Roles offer a framework for fully independent or interdependent collections of variables, tasks, files, templates, and modules.

ansible-galaxy init myrole

Variables and Facts

Variables are used to deal with differences between systems, users, and other variables that can change between plays or executions.

vars:
  http_port: 80

Facts are global variables that Ansible collects from the managed nodes.

Inventory

The inventory is a list of nodes that can be managed by Ansible. It can specify information like IP address and domain name. A inventory can be made as an ini or a yaml

[webservers]
web1 ansible_host=192.0.2.1
web2 ansible_host=192.0.2.2

Modules

Modules are the units of work that Ansible executes. Each module has a particular use, from managing packages to executing shell commands.

ansible all -m setup # Gather facts
ansible all -m file -a "path=/tmp/test state=touch" # Create a file

Ansible Galaxy

Ansible Galaxy is a repository for sharing and reusing Ansible roles. Roles can be downloaded and used in your playbooks.

ansible-galaxy install username.rolename