// LEARN LINUX TV • ARTICLE

Ansible for Complete Beginners: Creating Your First Playbook

ADVERTISEMENT

Ansible lets you configure a server once, write it down, and never do it by hand again. In this tutorial we’ll build your first playbook from scratch – then run it with ansible-pull, which I think is the better way to use Ansible for most setups.

We’ll create a git repository to hold the playbook, write it step by step, break the settings out into a variables file, then SSH into a Linode server, install Ansible, and let the machine pull its own configuration and apply it. No control node, no inventory file to babysit.

By the end you’ll have a working, version-controlled setup that any new server can configure itself from in one command.

Note: You’ll find the code I used in this video here.

YouTube player

For even more Ansible learning, check out my hands-on Ansible course to take your skills even further!

Repository Setup

And we’ll begin by creating a repository for our Ansible playbook, which I’ll create on Github. Github isn’t required though, you can use Gitlab or even a self-hosted repository server if you have one. I’m just using Github for this tutorial because it’s the one most people are accustomed to.

But you might be wondering, is it an actual requirement to create a repository for Ansible playbooks? Well, not really – but it is a best practice. Version control (such as git) makes it easy to track changes so it really makes sense with Ansible. So a repository isn’t a requirement for Ansible, but it’s definitely a good idea.

For today’s project though, you will need a Git repository in order to follow along. And the reason for this is because I’m going to use ansible-pull for this tutorial, and a repository is required for that. I’ll explain the difference later, but for now I recommend setting up a repository.

Anyway, what we’ll do is create a few files within our repository. And the first of those will be a Playbook named “local.yml”, and it should be at the base level of our repository. For those that aren’t already aware, a Playbook is a set of instructions for Ansible to run, and it’s written in YAML. A full walkthrough of YAML is beyond the scope of this video, but you can literally just learn YAML by following along with Ansible tutorials like this one, it’s very easy to learn.

A good short summary of what a YAML file is that it’s a human-readable markup language, and it follows the “keep it simple” approach. YAML itself stands for Yet Another Markup Language, in case you were curious. And it’s not specific to Ansible either, but it is used heavily by Ansible.

Anyway, what we’ll do right now is build our first Playbook and I’ll walk you through it step by step. I’ve selected a number of tasks, which Ansible calls “Plays” and each one is an example of something Ansible can do.

---

- name: Basic system setup
  hosts: localhost
  connection: local
  become: true

  vars_files:
    - vars/main.yml

  tasks:

    - name: Install packages and keep them updated
      ansible.builtin.package:
        name: "{{ packages }}"
        state: latest

    - name: Set the system timezone
      ansible.builtin.file:
        src: "/usr/share/zoneinfo/{{ system_timezone }}"
        dest: /etc/localtime
        state: link
        force: true

    - name: Create the admin user
      ansible.builtin.user:
        name: "{{ admin_user }}"
        groups: "{{ admin_group }}"
        append: true
        shell: /bin/bash
        create_home: true

    # "validate" runs the syntax check before the file is put in place:
    - name: Allow the admin group to use sudo without a password
      ansible.builtin.copy:
        content: "%{{ admin_group }} ALL=(ALL) NOPASSWD: ALL\n"
        dest: /etc/sudoers.d/99-ansible
        owner: root
        group: root
        mode: "0440"
        validate: "visudo -cf %s"

    # Facts gathered from the machine are available as variables:
    - name: Write a message of the day
      ansible.builtin.copy:
        content: |
          ***********************************************************
           {{ ansible_facts['hostname'] }} is managed by ansible-pull
           {{ ansible_facts['distribution'] }} {{ ansible_facts['distribution_version'] }}
           Last applied: {{ ansible_date_time.iso8601 }}
          ***********************************************************
        dest: /etc/motd
        owner: root
        group: root
        mode: "0644"

    - name: Make sure the service is enabled and running
      ansible.builtin.service:
        name: "{{ managed_service }}"
        state: started
        enabled: true

    # This is what turns a one-time run into a machine that keeps itself in sync:
    - name: Schedule ansible-pull to run every 30 minutes
      ansible.builtin.cron:
        name: ansible-pull
        user: root
        minute: "*/30"
        job: "/usr/bin/ansible-pull -o -U {{ ansible_pull_repo }} >> /var/log/ansible-pull.log 2>&1"
        cron_file: ansible-pull
      when: ansible_pull_repo | length > 0

We’ll also need a variable file as well. We’ll create this as vars/main.yml inside our repository:

---

# The user account to create:
admin_user: automation

# The group that grants sudo access:
admin_group: wheel

# Packages to install and keep updated.
packages:
  - curl
  - git
  - tmux
  - vim
  - wget

# System timezone (run "timedatectl list-timezones" to see the valid values):
system_timezone: Etc/UTC

# Service to make sure is running:
managed_service: sshd

# The repository this playbook lives in:
ansible_pull_repo: "https://github.com/LearnLinuxTV/ansible-pull-test"

How do you run it?

Okay, so we’ve created a Playbook, and also a variable file. So how do we actually run this?

Normally, the way this works is that you’ll have a server set up somewhere – typically called a “Control Host”. On that server, you’ll have an inventory file that will list all of your target servers, and it’ll connect to each one via SSH and execute your playbook commands against them.

But setting up an entire server takes time, and this video is intended to be a quick introduction to Ansible. So is there a faster way?

Well, yes! We’re going to use ansible-pull, which side-steps the server requirement altogether. With ansible-pull, each machine will basically run the Playbook against themselves, fetching it directly from your repository.

And you know what? ansible-pull is actually a great way to automate. In fact, I use ansible-pull exclusively within my environment. Without a control host, there’s no central point of failure, and overall it’s not only simpler but more effective. I teach the control host method too in my full Ansible course, but let’s take a look at how you can use ansible-pull right now.

To do that, you’ll first access whichever Linux machine you want to apply your Playbook to, and install the ansible-core package:

sudo apt install ansible-core

And now that we’ve installed Ansible, the ansible-pull binary should be available to us now:

command -v ansible-pull

And now that we’ve confirmed it’s available, we’re already set up to run it with no additional tweaks being necessary. Here’s how the syntax works out:

ansible-pull -U https://url/to/repo
ADVERTISEMENT