0%

0/1 Lessons

Course Introduction

• 19min

0 / 2 lessons complete

IT Lab Setup

• 31min

0 / 3 lessons complete

Installing Ansible

• 57min

0 / 4 lessons complete

Managing your Ansible Inventory

• 2hr 45min

0 / 8 lessons complete

Ansible Basics

• 2hr 54min

0 / 9 lessons complete

Ansible Roles

• 2hr 38min

0 / 8 lessons complete

Ansible Galaxy

• 2hr 28min

0 / 6 lessons complete

Ansible Facts, Variables, Passwords and Templates

• 3hr 20min

0 / 9 lessons complete

Advanced Ansible Playbook Creation

• 2hr 23min

0 / 8 lessons complete

Course Conclusion

• 11min

0 / 1 lessons complete

Create your first Ansible Playbook!

Instructions

Q&A (0)

Notes (0)

Resources (0)

Saving Progress...

Resources

There are no resources for this lesson.

Notes can be saved and accessed anywhere in the course. They also double as bookmarks so you can quickly review important lesson material.

Create note

In this lesson, we're going to step into the practical side of Ansible by creating and executing your first playbook. The goal is straightforward: use Ansible to create an empty file named "ansible_created_this_file.txt" on our managed servers. This task, while basic, is a great way to get hands-on experience with writing and running an Ansible playbook.

Step 1: Creating Your Playbook File

Begin by creating a new YAML file for your playbook. Let's name it first_playbook.yml to reflect its purpose.

touch ~/code/first_playbook.yml

Step 2: Writing Your Playbook

Open the first_playbook.yml file in your favorite text editor (I'm sure that's VIM, right? lol). For simplicity's sake, I'll use the nano editor:

nano ~/code/first_playbook.yml

We will write a simple play in this playbook to create an empty file.

Pay special attention to ALL whitespace shown in the code below - as this will make or break your YAML playbook!

Start by adding the --- characters as the first line in the file, which marks the beginning of the YAML file.

---

Next let's start the play, which will be start on the next line:

- hosts: all

Inside of this playbook, we want to define the become field, and the tasks. Let's start with the become field (remember to keep the whitespace):

  become: false

The become directive ensures that the plays inside of this playbook will be run with privilege escalation (as the root user). It's like telling our Ansible Controller that it should use sudo while running the plays. Per the official docs:

https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_privilege_escalation.html

This can also be defined in each task as well as at the root of the play. Since we haven't defined the sudo password or learn about Ansible Vaults yet, we can't set this setting to true just yet. That's why we set it to false and in this case, we could omit the become directive entirely, but I want you to be aware that it exists and explain what it does since setting this to true is very common in Ansible Playbooks.

Let's define the tasks for our playbook! With the same tab indentation, write the start of the tasks:

  tasks:

Below this, we will write a list in YAML format, which if you remember from our YAML lesson, will be indented one more time and be prefixed with a hyphen (- name:).

    - name: Create an empty file

That defines the name of our first task. Inside of our task, let's call the file module:

      file:

Notice that the indentation for this puts it passed the previous hyphen and on the same indentation as the text name. To understand what settings we can configure for the file module, we can save and close our playbook. If you're using the nano editor, you can do this by pressing ctrl+x, shift+y, then enter.

To see the help for our file module, run the following code at our terminal:

ansible-doc file

Look through the help content for path and state. For the state parameter, we want to use touch. At the bottom of this help, we can see an example for how to touch a file with Ansible. Now that we have an understanding of the file module, let's go back to editing our playbook file.

Under the file module, let's add the path to the file we want to create which will be /tmp/ansible_created_this_file.txt:

        path: /tmp/ansible_created_this_file.txt

Next add the state parameter:

        state: touch

Server Academy Members Only

Sorry, this lesson is only available to Server Academy Full Access members. Become a Full-Access Member now and you’ll get instant access to all of our courses.

0 0 votes
Lesson Rating
Subscribe
Notify of
profile avatar
0 Comments
Inline Feedbacks
View all comments