In this exercise, you will include and import playbooks and tasks in a top-level Ansible Playbook.
Outcomes
You will be able to include task and playbook files in playbooks.
Log in to workstation as student using student as the password.
On workstation, run the lab projects-file start command.
The script creates the working directory, /home/student/projects-file, and associated project files.
[student@workstation ~]$lab projects-file start
Procedure 6.2. Instructions
On workstation, as the student user, change to the /home/student/projects-file directory.
[student@workstation ~]$cd ~/projects-file[student@workstation projects-file]$
Review the contents of the three files in the tasks subdirectory.
Review the contents of the tasks/environment.yml file.
The file contains tasks for package installation and service administration.
---
- name: Install the {{ package }} package
yum:
name: "{{ package }}"
state: latest
- name: Start the {{ service }} service
service:
name: "{{ service }}"
enabled: true
state: startedReview the contents of the tasks/firewall.yml file.
The file contains tasks for installation, administration, and configuration of firewall software.
---
- name: Install the firewall
yum:
name: "{{ firewall_pkg }}"
state: latest
- name: Start the firewall
service:
name: "{{ firewall_svc }}"
enabled: true
state: started
- name: Open the port for {{ rule }}
firewalld:
service: "{{ item }}"
immediate: true
permanent: true
state: enabled
loop: "{{ rule }}"Review the contents of the tasks/placeholder.yml file.
This file contains a task for populating a placeholder web content file.
---
- name: Create placeholder file
copy:
content: "{{ ansible_facts['fqdn'] }} has been customized using Ansible.\n"
dest: "{{ file }}"Review the contents of the test.yml file in the plays subdirectory.
This file contains a play which tests connections to a web service.
---
- name: Test web service
hosts: localhost
become: no
tasks:
- name: connect to internet web server
uri:
url: "{{ url }}"
status_code: 200Create a playbook named playbook.yml.
Define the first play with the name Configure web server.
The play should execute against the servera.lab.example.com managed host defined in the inventory file.
The beginning of the file should look like the following:
--- - name: Configure web server hosts: servera.lab.example.com
In the playbook.yml playbook, define the tasks section with three sets of tasks.
Include the first set of tasks from the tasks/environment.yml tasks file.
Define the necessary variables to install the httpd package and to enable and start the httpd service.
Import the second set of tasks from the tasks/firewall.yml tasks file.
Define the necessary variables to install the firewalld package to enable and start the firewalld service, and to allow http connections.
Import the third task set from the tasks/placeholder.yml task file.
Create the tasks section in the first play by adding the following entry to the playbook.yml playbook.
tasks:
Include the first set of tasks from tasks/environment.yml using the include_tasks feature.
Set the package and service variables to httpd.
- name: Include the environment task file and set the variables
include_tasks: tasks/environment.yml
vars:
package: httpd
service: httpdImport the second set of tasks from tasks/firewall.yml using the import_tasks feature.
Set the firewall_pkg and firewall_svc variables to firewalld.
Set the rule variable to http.
- name: Import the firewall task file and set the variables
import_tasks: tasks/firewall.yml
vars:
firewall_pkg: firewalld
firewall_svc: firewalld
rule:
- http
- httpsImport the last task set from tasks/placeholder.yml using the import_tasks feature.
Set the file variable to /var/www/html/index.html.
- name: Import the placeholder task file and set the variable
import_tasks: tasks/placeholder.yml
vars:
file: /var/www/html/index.htmlAdd a second and final play to the playbook.yml playbook using the contents of the plays/test.yml playbook.
Add a second play to the playbook.yml playbook to validate the web server installation.
Import the play from plays/test.yml.
Set the url variable to http://servera.lab.example.com.
- name: Import test play file and set the variable
import_playbook: plays/test.yml
vars:
url: 'http://servera.lab.example.com'Your playbook should look like the following after the changes are complete:
---
- name: Configure web server
hosts: servera.lab.example.com
tasks:
- name: Include the environment task file and set the variables
include_tasks: tasks/environment.yml
vars:
package: httpd
service: httpd
- name: Import the firewall task file and set the variables
import_tasks: tasks/firewall.yml
vars:
firewall_pkg: firewalld
firewall_svc: firewalld
rule:
- http
- https
- name: Import the placeholder task file and set the variable
import_tasks: tasks/placeholder.yml
vars:
file: /var/www/html/index.html
- name: Import test play file and set the variable
import_playbook: plays/test.yml
vars:
url: 'http://servera.lab.example.com'Save the changes to the playbook.yml playbook.
Before running the playbook, verify its syntax is correct by running ansible-playbook --syntax-check.
If errors are reported, correct them before moving to the next step.
[student@workstation projects-file]$ansible-playbook playbook.yml --syntax-checkplaybook: playbook.yml
Execute the playbook.yml playbook.
The output of the playbook shows the import of the task and play files.
[student@workstation projects-file]$ansible-playbook playbook.ymlPLAY [Configure web server] *********************************************** TASK [Gathering Facts] **************************************************** ok: [servera.lab.example.com] TASK [Install the httpd package] ****************************************** changed: [servera.lab.example.com] TASK [Start the httpd service] ******************************************** changed: [servera.lab.example.com] TASK [Install the firewall] *********************************************** ok: [servera.lab.example.com] TASK [Start the firewall] ************************************************* ok: [servera.lab.example.com] TASK [Open the port for ['http', 'https']] ******************************** changed: [servera.lab.example.com] => (item=http) changed: [servera.lab.example.com] => (item=https) TASK [Create placeholder file] ******************************************** changed: [servera.lab.example.com] PLAY [Test web service] *************************************************** TASK [Gathering Facts] **************************************************** ok: [localhost] TASK [connect to internet web server] ************************************* ok: [localhost] PLAY RECAP **************************************************************** localhost : ok=2 changed=0 unreachable=0 failed=0 servera.lab.example.com : ok=8 changed=4 unreachable=0 failed=0