In this step, you will write your first Ansible Playbook. A playbook is a file written in YAML format that describes a set of tasks to be executed on your managed hosts. You will create a playbook that installs the Apache web server (httpd) and starts its service on the localhost machine defined in your inventory.
-
First, use the nano text editor to create a new file named apache.yml. This file will contain your playbook.
nano apache.yml
-
Inside the nano editor, you will define a "play." A play is the core unit of a playbook and maps a group of hosts to a set of tasks. Add the following content to apache.yml.
---: This is a standard YAML marker indicating the start of a document.
- name: ...: This is the beginning of your play. Giving it a descriptive name is a best practice.
hosts: webservers: This tells Ansible to run this play on all hosts in the webservers group from your inventory file.
become: true: This instructs Ansible to use privilege escalation (like sudo) to execute the tasks. This is necessary for actions like installing software or managing services.
tasks:: This keyword begins the list of tasks to be performed.
---
- name: Install and start Apache web server
hosts: webservers
become: true
tasks:
-
Now, add the tasks to your playbook. Each task is a single action that calls an Ansible module. Indentation is critical in YAML, so ensure the tasks are indented correctly under the tasks: section.
- Task 1: Install httpd. This task uses the
ansible.builtin.dnf module to ensure the httpd package is installed. The state: present parameter means Ansible will install the package if it's missing, and do nothing if it's already installed.
- Task 2: Start httpd service. This task uses the
ansible.builtin.service module. state: started ensures the service is running, and enabled: true ensures it will start automatically on system boot.
Add the following tasks to your apache.yml file, directly below the tasks: line:
- name: Install httpd package
ansible.builtin.dnf:
name: httpd
state: present
- name: Start and enable httpd service
ansible.builtin.service:
name: httpd
state: started
enabled: true
-
Your complete apache.yml playbook should now look like this. Double-check the indentation carefully.
---
- name: Install and start Apache web server
hosts: webservers
become: true
tasks:
- name: Install httpd package
ansible.builtin.dnf:
name: httpd
state: present
- name: Start and enable httpd service
ansible.builtin.service:
name: httpd
state: started
enabled: true
Save the file and exit nano (Ctrl+O, Enter, Ctrl+X).
-
Before running your playbook, it's a good practice to check it for syntax errors using the ansible-playbook command with the --syntax-check flag.
ansible-playbook --syntax-check apache.yml
If the syntax is correct, the command will print the playbook's filename without any errors.
playbook: apache.yml
-
Now, execute the playbook.
ansible-playbook apache.yml
Ansible will run through the tasks. Since this is the first run, you will see changed status for both tasks, indicating that the system state was modified.
PLAY [Install and start Apache web server] *************************************
TASK [Gathering Facts] *********************************************************
ok: [localhost]
TASK [Install httpd package] ***************************************************
changed: [localhost]
TASK [Start and enable httpd service] ******************************************
changed: [localhost]
PLAY RECAP *********************************************************************
localhost : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
-
Finally, verify that the Apache web server is running by using curl to request the default web page from localhost.
curl http://localhost
You should see the default Apache Test Page, which confirms your playbook worked successfully.
<html>
<head>
<title>Test Page</title>
</head>
<body>
<h1>Test Page</h1>
<p>This is the default test page for the Apache HTTP server.</p>
</body>
</html>