Skip to main content

Ansible: Rapid Start Cheatsheet

·485 words·3 mins

The following Ansible-specific terms are largely used throughout this guide:

  • Control Node (Management Node): This is a system where Ansible is installed and configured to connect and execute commands on nodes.
  • Managed Node (Target Node): a server controlled by Ansible.
  • Inventory File: a file that contains information about the servers Ansible controls, typically located at /etc/ansible/hosts.
  • Playbook: a file containing a series of tasks to be executed on a remote server.
  • Role: a collection of playbooks and other files that are relevant to a goal, such as installing a web server.
  • Play: a full Ansible run. A play can have several playbooks and roles, including a single playbook that acts as an entry point.
  • Task: an action (invoked by Ansible).

Advantages to Ansible: #

  1. Free, open-source, and backed by Red Hat.
  2. Minimal system requirements
  3. Agentless
  4. YAML Syntax (easy to read and understand for beginners)
  5. Developed in Python
  6. Strong community

How does it work? #

  1. From the Control Node, Ansible connects and pushes modules to them. The modules are removed when they are done.
  2. The Control Node (often referred to as the Management Node) controls the playbook execution.
  3. The inventory file provides the list of hosts where the modules will run.
  4. The Control Node (Management Node) performs an “ssh” connection and executes the modules that perform the steps defined in the play or playbook.

Ansible Commands: #

Below is a list of ad-hoc example commands you can invoke in Ansible.

Test Connectivity to Hosts

ansible <group> -m ping

Display System Information for Hosts

ansible <group> -m setup | less

Start a Service

ansible <group> -m service -a “name=docker state=”started”

Create a New User

ansible <group> -m user -a “name=newadmin password=<encrypted password>”

Remove a User

ansible <group> -m user -a “name=ansible state-absent”

Install Package (to latest version) if it is not present

ansible <group> -m yum -a “name=htop state=latest”

Ansible Sample Playbooks #

Below are a few Ansible playbooks you can use to get started.

Download and Install Apache on RedHat, CentOS, Fedora, or Amazon Linux. Replace yum with apt if you are on Debian or Ubuntu.

---
- name: Download and Install Apache
  hosts: example_group
  gather_facts: False
  become: True

  tasks:
    - name: Download Apache
      yum:
        name: httpd
        state: latest

    - name: Start Apache
      service:
        name: httpd
        state: started

Remove Package (Ubuntu or Debian). For example, replace ansible.builtin.apt with yum for RedHat, CentOS, Fedora, or Amazon Linux.

---
- name: Remove htop
  hosts: example_group
  become: true

  tasks:
  - name: Remove htop
    ansible.builtin.apt:
      name: htop
      state: absent

Add a User

---
- hosts: example_group
  gather_facts: False
  become: True

  tasks:
    - name: Adding User 
      user: 
        name: example_username
        comment: example_comment
        shell: /bin/bash
        password: some_password

Troubleshooting:

Common issues with playbooks:

  • Indentation
  • YAML Formatting
  • Missing quotes
  • Unsupported Module (ex: running apt module on Red Hat)

Strategies for resolution:

  • Log verbosity. Adding -“v” (up to 4x) at the end of your Ansible command will show verbose output.
  • Debug
  • Register

Questions? Contact me on Twitter, @aarongxa