Skip to content

Ansible Cheatsheet

Agentless configuration management and automation tool.

01

Getting Started

Inventory

Inventory defines target hosts.

ansible
# inventory.ini
[webservers]
web1 ansible_host=192.168.1.10
web2 ansible_host=192.168.1.11

[databases]
db1 ansible_host=192.168.1.20

Playbook

Playbooks define automation tasks in YAML.

ansible
---
- hosts: webservers
  tasks:
    - name: Install nginx
      apt: name=nginx state=present
    - name: Start nginx
      service: name=nginx state=started

Commands

Ad-hoc commands and playbook execution.

ansible
# ansible all -m ping
# ansible-playbook site.yml
# ansible-playbook -l webservers site.yml
02

Features

Variables

Variables and Jinja2 templates.

ansible
vars:
  http_port: 80
  max_clients: 200

tasks:
  - name: Template config
    template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf

Roles

Roles for reusable automation units.

ansible
# ansible-galaxy init myrole

# structure:
# myrole/tasks/main.yml
# myrole/vars/main.yml
# myrole/templates/

Conditionals

When conditionals for task execution.

ansible
tasks:
  - name: Install on Debian
    apt: name=nginx
    when: ansible_os_family == "Debian"

Was this helpful?