Creating Your First Playbook
Let's start by creating a simple Ansible playbook that will create a directory and a file on the local machine. This will help you understand the basic structure of a playbook and how to execute it.
First, let's create a new file named first_playbook.yml
in the /home/labex/project
directory:
nano /home/labex/project/first_playbook.yml
This command opens the nano text editor. If you're not familiar with nano, don't worry - it's a simple text editor. You can type directly into it.
Now, add the following content to the file:
---
- name: My First Playbook
hosts: localhost
connection: local
tasks:
- name: Create a directory
file:
path: /home/labex/project/test_directory
state: directory
mode: "0755"
- name: Create a file
copy:
content: "Hello from Ansible!"
dest: /home/labex/project/test_directory/hello.txt
Let's break down this playbook to understand each part:
- The
---
at the top marks the start of a YAML file. YAML is the format used for Ansible playbooks.
name: My First Playbook
is a descriptive name for this play. It helps you identify what this playbook does.
hosts: localhost
specifies that this playbook will run on the local machine. In a real-world scenario, you might specify remote hosts here.
connection: local
tells Ansible to run the playbook locally instead of using SSH. This is useful for testing and for tasks that need to be performed on the Ansible control node itself.
tasks:
is followed by a list of tasks to be executed. Each task is an action you want Ansible to perform.
- Each task has a
name
for description. This helps you understand what each task does and makes troubleshooting easier.
- The tasks use Ansible modules:
- The
file
module is used to create the directory.
- The
copy
module is used to create a file with specific content.
Don't worry if you don't understand all the modules yet. As you progress, you'll learn about many more modules and their uses.
Save and exit the editor. In nano, you can do this by pressing Ctrl+X
, then Y
, then Enter
.
Now, let's run this playbook. In your terminal, type:
ansible-playbook /home/labex/project/first_playbook.yml
This command tells Ansible to run the playbook we just created. You should see output similar to this:
PLAY [My First Playbook] ******************************************************
TASK [Gathering Facts] *********************************************************
ok: [localhost]
TASK [Create a directory] ******************************************************
changed: [localhost]
TASK [Create a file] ***********************************************************
changed: [localhost]
PLAY RECAP *********************************************************************
localhost : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
This output shows that Ansible executed our tasks successfully. The "changed" status indicates that Ansible made changes to the system (created a directory and a file).
If you want to verify the results manually, you can use these commands:
ls -l /home/labex/project/test_directory
cat /home/labex/project/test_directory/hello.txt
The first command should show the directory we created, and the second should display the content of the file we created.