In this exercise, you define and use variables in a playbook.
Outcomes
Define variables in a playbook.
Create tasks that use defined variables.
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 data-variables
Procedure 3.1. Instructions
Change into the /home/student/data-variables directory.
[student@workstation ~]$ cd ~/data-variables
[student@workstation data-variables]$Over the next several steps, you create a playbook that consists of a single play that installs the Apache web server and opens the ports for the service to be reachable. The play also queries the web server to ensure it is up and running.
Create a playbook named playbook.yml.
Create a play named "Deploy and start Apache HTTPD service", target the host group webserver as the managed hosts, and define the following variables in its vars section:
Table 3.2. Variables
| Variable | Description |
|---|---|
web_pkg
| Web server package to install |
firewall_pkg
| Firewall package to install |
web_service
| Web service to manage |
firewall_service
| Firewall service to manage |
python_pkg
| Required package for the uri module |
rule
| The service name to open |
---
- name: Deploy and start Apache HTTPD service
hosts: webserver
vars:
web_pkg: httpd
firewall_pkg: firewalld
web_service: httpd
firewall_service: firewalld
python_pkg: python3-PyMySQL
rule: httpCreate the tasks block and create the first task, using the ansible.builtin.dnf module to make sure the latest versions of the required packages are installed.
tasks:
- name: Required packages are installed and up to date
ansible.builtin.dnf:
name:
- "{{ web_pkg }}"
- "{{ firewall_pkg }}"
- "{{ python_pkg }}"
state: latestYou can use ansible-navigator doc ansible.builtin.dnf -m stdout to review the syntax for the ansible.builtin.dnf module.
(If you have the ansible-core package installed, you can also use ansible-doc ansible.builtin.dnf.)
The documentation shows that the module's name directive can take a list of packages that the module should work with, so that you do not need separate tasks to make sure that each package is up-to-date.
Create two tasks that make sure that the httpd and firewalld services are started and enabled.
- name: The {{ firewall_service }} service is started and enabled
ansible.builtin.service:
name: "{{ firewall_service }}"
enabled: true
state: started
- name: The {{ web_service }} service is started and enabled
ansible.builtin.service:
name: "{{ web_service }}"
enabled: true
state: startedThe ansible.builtin.service module works differently from the ansible.builtin.dnf module, as documented by ansible-doc ansible.builtin.service.
Its name directive takes the name of exactly one service to work with.
You can write a single task that ensures both services are started and enabled, using the loop keyword covered later in this course.
Add a task that ensures specific content exists in the /var/www/html/index.html file.
- name: Web content is in place
ansible.builtin.copy:
content: "Example web content"
dest: /var/www/html/index.htmlAdd a task that uses the ansible.posix.firewalld module to ensure that the firewall ports are open for the firewalld service named in the rule variable.
- name: The firewall port for {{ rule }} is open
ansible.posix.firewalld:
service: "{{ rule }}"
permanent: true
immediate: true
state: enabledCreate a new play that queries the web service to ensure that everything has been correctly configured.
It must run on workstation.
Because of that Ansible fact, Ansible does not have to change identity, so set the become module to false.
You can use the ansible.builtin.uri module to inspect a URL.
For this task, verify that a status code of 200 is returned to confirm that the web server on servera.lab.example.com is running and correctly configured.
- name: Verify the Apache service
hosts: workstation
become: false
tasks:
- name: Ensure the webserver is reachable
ansible.builtin.uri:
url: http://servera.lab.example.com
status_code: 200When completed, the playbook contains the following content: Review the playbook and confirm that both plays are correct.
---
- name: Deploy and start Apache HTTPD service
hosts: webserver
vars:
web_pkg: httpd
firewall_pkg: firewalld
web_service: httpd
firewall_service: firewalld
python_pkg: python3-PyMySQL
rule: http
tasks:
- name: Required packages are installed and up to date
ansible.builtin.dnf:
name:
- "{{ web_pkg }}"
- "{{ firewall_pkg }}"
- "{{ python_pkg }}"
state: latest
- name: The {{ firewall_service }} service is started and enabled
ansible.builtin.service:
name: "{{ firewall_service }}"
enabled: true
state: started
- name: The {{ web_service }} service is started and enabled
ansible.builtin.service:
name: "{{ web_service }}"
enabled: true
state: started
- name: Web content is in place
ansible.builtin.copy:
content: "Example web content"
dest: /var/www/html/index.html
- name: The firewall port for {{ rule }} is open
ansible.posix.firewalld:
service: "{{ rule }}"
permanent: true
immediate: true
state: enabled
- name: Verify the Apache service
hosts: workstation
become: false
tasks:
- name: Ensure the webserver is reachable
ansible.builtin.uri:
url: http://servera.lab.example.com
status_code: 200Before you run the playbook, use the ansible-navigator run --syntax-check command to verify its syntax.
If it reports any errors, correct them before moving to the next step.
You should see output similar to the following:
[student@workstation data-variables]$ansible-navigator run \>-m stdout playbook.yml --syntax-checkplaybook: /home/student/data-variables/playbook.yml
Use the ansible-navigator run command to run the playbook.
Watch the output as Ansible installs the packages, starts and enables the services, and ensures the web server is reachable.
[student@workstation data-variables]$ansible-navigator run \>-m stdout playbook.ymlPLAY [Deploy and start Apache HTTPD service] *********************************** TASK [Gathering Facts] ********************************************************* ok: [servera.lab.example.com] TASK [Required packages are installed and up to date] ************************** changed: [servera.lab.example.com] TASK [The firewalld service is started and enabled] **************************** ok: [servera.lab.example.com] TASK [The httpd service is started and enabled] ******************************** changed: [servera.lab.example.com] TASK [Web content is in place] ************************************************* changed: [servera.lab.example.com] TASK [The firewall port for http is open] ************************************** changed: [servera.lab.example.com] PLAY [Verify the Apache service] *********************************************** TASK [Gathering Facts] ********************************************************* ok: [workstation] TASK [Ensure the webserver is reachable] *************************************** ok: [workstation] PLAY RECAP ********************************************************************* servera.lab.example.com : ok=6 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 workstation : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
This concludes the section.