In this exercise, you will define and use variables in a playbook.
Outcomes
You should be able to:
Define variables in a playbook.
Create tasks that use defined variables.
Log in to workstation as student using student as the password.
On workstation, run the lab data-variables start command. This function creates the data-variables working directory, and populates it with an Ansible configuration file and host inventory.
[student@workstation ~]$lab data-variables start
Procedure 3.1. Instructions
On workstation, as the student user, change into the /home/student/data-variables directory.
[student@workstation ~]$cd ~/data-variables[student@workstation data-variables]$
Over the next several steps, you will create a playbook that installs the Apache web server and opens the ports for the service to be reachable. The playbook queries the web server to ensure it is up and running.
Create the playbook.yml playbook and define the following variables in the 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, which should use the yum module to make sure the latest versions of the required packages are installed.
tasks:
- name: Required packages are installed and up to date
yum:
name:
- "{{ web_pkg }}"
- "{{ firewall_pkg }}"
- "{{ python_pkg }}"
state: latestYou can use ansible-doc yum to review the syntax for the yum module. The syntax shows that its name directive can take a list of packages that the module should work with, so that you do not need separate tasks to makes sure each package is up-to-date.
Create two tasks to make sure that the httpd and firewalld services are started and enabled.
- name: The {{ firewall_service }} service is started and enabled
service:
name: "{{ firewall_service }}"
enabled: true
state: started
- name: The {{ web_service }} service is started and enabled
service:
name: "{{ web_service }}"
enabled: true
state: startedThe service module works differently from the yum module, as documented by ansible-doc 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
copy:
content: "Example web content"
dest: /var/www/html/index.htmlAdd a task that uses the firewalld module to ensure the firewall ports are open for the firewalld service named in the rule variable.
- name: The firewall port for {{ rule }} is open
firewalld:
service: "{{ rule }}"
permanent: true
immediate: true
state: enabledCreate a new play that queries the web service to ensure everything has been correctly configured. It should run on localhost. Because of that Ansible fact, Ansible does not have to change identity, so set the become module to false. You can use the uri module to check a URL. For this task, check for a status code of 200 to confirm the web server on servera.lab.example.com is running and correctly configured.
- name: Verify the Apache service
hosts: localhost
become: false
tasks:
- name: Ensure the webserver is reachable
uri:
url: http://servera.lab.example.com
status_code: 200When completed, the playbook should appear as follows. 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
yum:
name:
- "{{ web_pkg }}"
- "{{ firewall_pkg }}"
- "{{ python_pkg }}"
state: latest
- name: The {{ firewall_service }} service is started and enabled
service:
name: "{{ firewall_service }}"
enabled: true
state: started
- name: The {{ web_service }} service is started and enabled
service:
name: "{{ web_service }}"
enabled: true
state: started
- name: Web content is in place
copy:
content: "Example web content"
dest: /var/www/html/index.html
- name: The firewall port for {{ rule }} is open
firewalld:
service: "{{ rule }}"
permanent: true
immediate: true
state: enabled
- name: Verify the Apache service
hosts: localhost
become: false
tasks:
- name: Ensure the webserver is reachable
uri:
url: http://servera.lab.example.com
status_code: 200Before you run the playbook, use the ansible-playbook --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-playbook --syntax-check playbook.ymlplaybook: playbook.yml
Use the ansible-playbook 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-playbook 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: [localhost] TASK [Ensure the webserver is reachable] *************************************** ok: [localhost] PLAY RECAP ********************************************************************* localhost : ok=2 changed=0 unreachable=0 failed=0 servera.lab.example.com : ok=6 changed=4 unreachable=0 failed=0