In this exercise, you write and run an Ansible Playbook.
Outcomes
You should be able to write a playbook using basic YAML syntax and Ansible Playbook structure, and successfully run it with the ansible-navigator run command.
As the student user on the workstation machine, use the lab command to prepare your system for this exercise.
This command prepares your environment and ensures that all required resources are available.
[student@workstation ~]$ lab start playbook-basic
Procedure 2.3. Instructions
The /home/student/playbook-basic Ansible project directory has been created on the workstation machine for this exercise.
This directory already has an ansible.cfg configuration file.
It also has an inventory file named inventory that defines a web group, which includes the managed hosts serverc.lab.example.com and serverd.lab.example.com as members.
This exercise has you create a playbook named site.yml in the project directory.
This playbook contains one play, which targets members of the web host group.
The playbook uses tasks that run modules to ensure that the following conditions are met on the managed hosts:
The httpd package is present, using the ansible.builtin.dnf module.
The local files/index.html file is copied to /var/www/html/index.html on each managed host, using the ansible.builtin.copy module.
The httpd service is started and enabled, using the ansible.builtin.service module.
You can use the ansible-navigator doc command to help you understand the keywords needed for each of the modules.
After the playbook is written, validate its syntax and then use the ansible-navigator run command to run the playbook to implement the configuration.
Change into the /home/student/playbook-basic directory.
[student@workstation ~]$ cd ~/playbook-basic
[student@workstation playbook-basic]$Create a new playbook called /home/student/playbook-basic/site.yml.
Start writing a play that targets the hosts in the web host group.
Create and open ~/playbook-basic/site.yml.
The first line of the file should be three dashes to indicate the start of the playbook.
---
The next line starts the play.
It needs to start with a dash and a space before the first keyword in the play.
Name the play with an arbitrary string documenting the play's purpose, using the name keyword.
--- - name: Install and start Apache HTTPD
Add a hosts keyword-value pair to specify that the play run on hosts in the inventory's web host group.
Make sure that the hosts keyword is indented two spaces so it aligns with the name keyword in the preceding line.
The complete site.yml file should consist of the following content:
--- - name: Install and start Apache HTTPD hosts: web
Continue to edit the /home/student/playbook-basic/site.yml file, and add a tasks keyword and the three tasks for your play that were specified in the instructions.
Add a tasks keyword indented by two spaces (aligned with the hosts keyword) to start the list of tasks.
The file should now consist of the following content:
--- - name: Install and start Apache HTTPD hosts: web tasks:
Add the first task.
Indent by four spaces, and start the task with a dash and a space, and then give the task a name, such as Ensure httpd package is present.
Use the ansible.builtin.dnf module for this task.
Indent the module keywords two more spaces; set the package name to httpd and the package state to present.
The task should consist of the following content:
- name: Ensure httpd package is present
ansible.builtin.dnf:
name: httpd
state: presentAdd the second task.
Match the format of the previous task, and give the task a name, such as Correct index.html is present.
Use the ansible.builtin.copy module.
Configure the module keywords to set the src key to files/index.html and the dest key to /var/www/html/index.html.
The task should consist of the following content:
- name: Correct index.html is present
ansible.builtin.copy:
src: files/index.html
dest: /var/www/html/index.htmlAdd the third task to start and enable the httpd service.
Match the format of the previous two tasks, and give the new task a name, such as Ensure httpd is started.
Use the ansible.builtin.service module for this task.
Set the name key of the service to httpd, the state key to started, and the enabled key to true.
The task should consist of the following content:
- name: Ensure httpd is started
ansible.builtin.service:
name: httpd
state: started
enabled: trueYour entire site.yml Ansible Playbook should match the following example.
Make sure that the indentation of your play's keywords, the list of tasks, and each task's keywords are all correct.
---
- name: Install and start Apache HTTPD
hosts: web
tasks:
- name: Ensure httpd package is present
ansible.builtin.dnf:
name: httpd
state: present
- name: Correct index.html is present
ansible.builtin.copy:
src: files/index.html
dest: /var/www/html/index.html
- name: Ensure httpd is started
ansible.builtin.service:
name: httpd
state: started
enabled: trueSave the file and exit.
Before running your playbook, run the ansible-navigator run --syntax-check command to validate its syntax.
Correct any reported errors before continuing.
You should see output similar to the following:
[student@workstation playbook-basic]$ansible-navigator run \>-m stdout site.yml --syntax-checkplaybook: /home/student/playbook-basic/site.yml
Run your playbook. Read through the output generated to ensure that all tasks completed successfully.
[student@workstation playbook-basic]$ansible-navigator run \>-m stdout site.ymlPLAY [Install and start Apache HTTPD] ****************************************** TASK [Gathering Facts] ********************************************************* ok: [serverd.lab.example.com] ok: [serverc.lab.example.com] TASK [Ensure httpd package is present] ************************************************ changed: [serverd.lab.example.com] changed: [serverc.lab.example.com] TASK [Correct index.html is present] ******************************************* changed: [serverd.lab.example.com] changed: [serverc.lab.example.com] TASK [Ensure httpd is started] ******************************************************** changed: [serverd.lab.example.com] changed: [serverc.lab.example.com] PLAY RECAP ********************************************************************* serverc.lab.example.com : ok=4 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 serverd.lab.example.com : ok=4 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
If all went well, you should be able to run the playbook a second time and see all tasks complete with no changes to the managed hosts.
[student@workstation playbook-basic]$ansible-navigator run \>-m stdout site.ymlPLAY [Install and start Apache HTTPD] ****************************************** TASK [Gathering Facts] ********************************************************* ok: [serverd.lab.example.com] ok: [serverc.lab.example.com] TASK [Ensure httpd package is present] ************************************************ ok: [serverd.lab.example.com] ok: [serverc.lab.example.com] TASK [Correct index.html is present] ******************************************* ok: [serverc.lab.example.com] ok: [serverd.lab.example.com] TASK [Ensure httpd is started] ******************************************************** ok: [serverd.lab.example.com] ok: [serverc.lab.example.com] PLAY RECAP ********************************************************************* serverc.lab.example.com : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 serverd.lab.example.com : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Use the curl command to verify that both serverc.lab.example.com and serverd.lab.example.com are configured as an HTTPD server.
[student@workstation playbook-basic]$curl serverc.lab.example.comThis is a test page. [student@workstation playbook-basic]$curl serverd.lab.example.comThis is a test page.
This concludes the section.