Optimize a playbook for more efficient execution, analyzing the playbook's performance with callback plug-ins.
Outcomes
Activate Ansible callback plug-ins in the configuration file.
Profile a playbook execution time.
Optimize a playbook.
As the student user on the workstation machine, use the lab command to prepare your system for this exercise.
This command creates an Ansible project in the /home/student/task-speed/ directory.
[student@workstation ~]$ lab start task-speed
Procedure 6.4. Instructions
From a terminal, change to the /home/student/task-speed/ directory and review the deploy_webservers.yml playbook.
Change to the /home/student/task-speed/ directory.
[student@workstation ~]$ cd ~/task-speed
[student@workstation task-speed]$Review the contents of the deploy_webservers.yml playbook.
[student@workstation task-speed]$ cat deploy_webservers.yml
---
- name: Deploy the web servers
hosts: web_servers
become: true
tasks:
- name: Ensure required packages are installed
ansible.builtin.yum:
name: "{{ item }}"
state: present
loop:
- httpd
- mod_ssl
- httpd-tools
- mariadb-server
- mariadb
- php
- php-mysqlnd
- name: Ensure the services are enabled
ansible.builtin.service:
name: "{{ item }}"
state: started
enabled: true
loop:
- httpd
- mariadb
- name: Ensure the web content is installed
ansible.builtin.copy:
src: web_content/
dest: /var/www/htmlThe playbook installs packages, starts services, and recursively copies a local directory to the managed nodes.
Activate the timer and the profile_tasks callback plug-ins, and then run the deploy_webservers.yml playbook.
Edit the ansible.cfg file, and then add the callbacks_enabled directive with the two callback plug-ins that you need to activate.
[defaults]
inventory=inventory.yml
remote_user=devops
callbacks_enabled=timer, profile_tasksRun the deploy_webservers.yml playbook, and take note of the total elapsed time.
[student@workstation task-speed]$ansible-navigator run \>-m stdout deploy_webservers.yml...output omitted... PLAY RECAP ************************************************************** serverb.lab.example.com : ok=4 changed=3 unreachable=0 ... serverc.lab.example.com : ok=4 changed=3 unreachable=0 ...Playbook run took 0 days, 0 hours, 1 minutes, 22 secondsWednesday 09 November 2022 20:27:38 +0000 (0:00:34.966) 0:01:22.396 **** ========================================================================= Ensure required packages are installed --------------------------- 38.29s Ensure the web content is installed ------------------------------ 34.97s Ensure the services are enabled ----------------------------------- 7.14s Gathering Facts --------------------------------------------------- 1.96s
The playbook and task execution times are probably different on your system. However, web content deployment and package installations should be the most time-consuming tasks.
Optimize the deploy_webservers.yml playbook.
To do so, disable fact gathering, because the playbook does not use facts.
Also remove the loop in the package installation task and replace the ansible.builtin.copy module with the ansible.posix.synchronize module.
If you make a mistake, you can restore the original file from the deploy_webservers.yml.backup file.
Edit the deploy_webservers.yml playbook.
Add the gather_facts directive and set it to false.
Do not close the file yet.
---
- name: Deploy the web servers
hosts: web_servers
become: true
gather_facts: false
...output omitted...Remove the loop from the package installation task.
To do so, move the list of packages under the name parameter and remove the loop directive.
...output omitted... tasks: - name: Ensure required packages are installed ansible.builtin.yum: name: -httpd-mod_ssl-httpd-tools-mariadb-server-mariadb-php-php-mysqlndstate: present ...output omitted...
The next task uses the ansible.builtin.service module to start the httpd and mariadb services.
This task also uses a loop.
However, in contrast to the ansible.builtin.yum module, the ansible.builtin.service module does not accept a list of services in its name parameter.
Leave this task as it is.
In the last task, replace the ansible.builtin.copy module with the ansible.posix.synchronize module, which is more efficient when deploying large directories.
...output omitted...
- name: Ensure the web content is installed
ansible.posix.synchronize:
src: web_content/
dest: /var/www/htmlSave and close the file when done.
Run the clean.yml playbook to clean up the managed hosts.
When done, run the optimized deploy_webservers.yml playbook and compare the execution time with its first execution.
Run the clean.yml playbook.
[student@workstation task-speed]$ ansible-navigator run -m stdout clean.yml
...output omitted...Run the deploy_webservers.yml playbook.
[student@workstation task-speed]$ansible-navigator run \>-m stdout deploy_webservers.yml...output omitted... PLAY RECAP ************************************************************** serverb.lab.example.com : ok=3 changed=3 unreachable=0 ... serverc.lab.example.com : ok=3 changed=3 unreachable=0 ...Playbook run took 0 days, 0 hours, 0 minutes, 22 secondsWednesday 09 November 2022 20:40:41 +0000 (0:00:01.524) 0:00:22.276 **** ========================================================================= Ensure required packages are installed --------------------------- 17.16s Ensure the services are enabled ----------------------------------- 3.56s Ensure the web content is installed ------------------------------- 1.52s
Notice that the fact gathering task is absent. The playbook and task execution times should also be reduced.
Disable the timer and the profile_tasks callback plug-ins.
Disable the callback plug-ins by deleting the callbacks_enabled line in the ansible.cfg file.
[student@workstation task-speed]$ cat ansible.cfg
[defaults]
inventory=inventory.yml
remote_user=devops