Create your first Ansible Playbook!
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:
The
becomekeyword uses existing privilege escalation tools like sudo, su, pfexec, doas, pbrun, dzdo, ksu, runas, machinectl and others.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 defin…
No comments yet. Add the first comment to start the discussion.