Convert an inventory from INI format to YAML and apply best practices to variable structuring.
Outcomes
Organize playbook variables in a directory structure.
Write an inventory file in YAML format.
Assign arbitrary hostnames for remote hosts in an inventory.
As the student user on the workstation machine, use the lab command to prepare your system for this exercise.
This command initializes the remote Git repository that you need for this lab.
The Git repository contains a site.yml playbook that configures a front-end load balancer and a pool of back-end web servers.
The back-end server pool is partitioned into two groups, A and B.
Each pool partition deploys an independent version of the web application.
Use Student@123 as the Git password when you push changes to this remote repository.
[student@workstation ~]$ lab start inventory-review
Procedure 5.4. Instructions
Clone the https://git.lab.example.com/student/inventory-review.git Git repository into the /home/student/git-repos directory and then create the exercise branch.
In the ~/git-repos/inventory-review/ directory, review the inventory file and then run the site.yml playbook using automation content navigator.
You can use the curl command to verify that the same version of the web application is deployed to both back-end web servers by making at least two web requests to servera.
From a terminal, create the /home/student/git-repos directory if it does not exist, and then change into it.
[student@workstation ~]$mkdir -p ~/git-repos/[student@workstation ~]$cd ~/git-repos/
Clone the https://git.lab.example.com/student/inventory-review.git repository and then change directory to the cloned repository:
[student@workstation git-repos]$git clone \>https://git.lab.example.com/student/inventory-review.gitCloning into 'inventory-review'... ...output omitted... [student@workstation git-repos]$cd inventory-review
Create the exercise branch and check it out.
[student@workstation inventory-review]$ git checkout -b exercise
Switched to a new branch 'exercise'Review the inventory file.
[student@workstation inventory-review]$ cat inventory
[lb_servers]
servera.lab.example.com
[web_servers]
[web_servers:children]
a_web_servers
b_web_servers
# Group "A" of Web Servers
[a_web_servers]
serverb.lab.example.com
# Group "B" of Web Servers
[b_web_servers]
serverc.lab.example.comIn this project, the web_servers host group is composed of two other groups: a_web_servers and b_web_servers.
Using the ee-supported-rhel8 execution environment, run the site.yml playbook.
[student@workstation inventory-review]$ansible-navigator run site.ymlPlay name Ok Changed ... Skipped ... Task count Progress 0│Ensure HAProxy is deployed 7 6 ... 2 ... 9 Complete 1│Ensure Apache is deployed 14 12 ... 2 ... 16 Complete 2│Ensure Web App is deployed 4 2 ... 0 ... 4 Complete ^f/PgUp page up ^b/PgDn page down ↑↓ scroll esc back ...Successful
Press ESC to exit from the ansible-navigator command.
Verify that the same version of the web application is deployed to all back-end web servers.
[student@workstation inventory-review]$for x in 1 2; do curl servera; doneHello from serverb.lab.example.com. (version v1.1) Hello from serverc.lab.example.com. (version v1.1)
Configure the web servers in each host group to report different version numbers for the web application, as follows:
Create a directory for variable files for the a_web_servers host group.
Create a variable file in that directory called webapp.yml.
That file must set the value of the webapp_version variable to v1.1a.
Create another directory for the b_web_servers host group.
It should also contain a variable file called webapp.yml.
That second variable file must set the value of the webapp_version variable to v1.1b.
Create directories to hold group variable files for the a_web_servers and b_web_servers host groups.
[student@workstation inventory-review]$ mkdir -pv group_vars/{a,b}_web_servers
mkdir: created directory 'group_vars/a_web_servers'
mkdir: created directory 'group_vars/b_web_servers'Create the webapp.yml variable file for the a_web_servers group.
Use it to set the value of the webapp_version variable to v1.1a.
[student@workstation inventory-review]$echo "webapp_version: v1.1a" > \>group_vars/a_web_servers/webapp.yml
Create the webapp.yml variable file for the b_web_servers group.
Use it to set the value of the webapp_version variable to v1.1b.
[student@workstation inventory-review]$echo "webapp_version: v1.1b" > \>group_vars/b_web_servers/webapp.yml
Using automation content navigator, run the deploy_webapp.yml playbook to update the version information displayed by the web application.
Use the curl command to confirm that requests for web pages sent to servera produce two different versions of the web application.
Commit the files that you have created to your local Git repository.
Run the deploy_webapp.yml playbook:
[student@workstation inventory-review]$ ansible-navigator run deploy_webapp.yml
Play name Ok Changed ... Failed ... Task count Progress
0│Ensure Web App is deployed 4 2 ... 0 ... 4 CompletePress ESC to exit from the ansible-navigator command.
Verify that requests to servera produce two different versions of the web application.
[student@workstation inventory-review]$for x in 1 2; do curl servera; doneHello fromserverb.lab.example.com. (version v1.1a) Hello fromserverc.lab.example.com. (version v1.1b)
Commit these changes to your local Git repository.
Optionally, if you decide to push the branch at this step, use Student@123 as the Git password.
[student@workstation inventory-review]$git statusOn branch exercise Untracked files: (use "git add <file>..." to include in what will be committed)group_vars/a_web_servers/group_vars/b_web_servers/nothing added to commit but untracked files present (use "git add" to track) [student@workstation inventory-review]$git add group_vars/{a,b}_web_servers[student@workstation inventory-review]$git statusOn branch exercise Changes to be committed: (use "git restore --staged <file>..." to unstage) new file: group_vars/a_web_servers/webapp.yml new file: group_vars/b_web_servers/webapp.yml [student@workstation inventory-review]$git commit \>-m "Created variable files for the A and B groups."...output omitted...
Create a YAML-formatted inventory file called inventory.yml using the data from the INI-formatted inventory file.
If you generate the inventory.yml file using the ansible-navigator command, then remove the variables that are added for each host.
Do not remove the original inventory file.
Run the site.yml playbook using the new inventory.yml file.
Create a YAML-formatted inventory file called inventory.yml using the data from the INI-formatted inventory file:
[student@workstation inventory-review]$ansible-navigator inventory \>-i inventory --list --yaml -m stdout > inventory.yml
Ensure that the inventory.yml file does not contain host variables.
Either of the following two examples for the inventory.yml file are acceptable.
Trim the file to match one or the other example.
Example 1:
This example includes the all group and includes the optionally ungrouped group.
all:
children:
lb_servers:
hosts:
servera.lab.example.com:
ungrouped: {}
web_servers:
children:
a_web_servers:
hosts:
serverb.lab.example.com:
b_web_servers:
hosts:
serverc.lab.example.com:Example 2:
This example more closely matches the existing inventory file.
lb_servers:
hosts:
servera.lab.example.com:
web_servers:
children:
a_web_servers:
hosts:
serverb.lab.example.com:
b_web_servers:
hosts:
serverc.lab.example.com:Test the new inventory.yml inventory file.
[student@workstation inventory-review]$ansible-navigator run site.yml \>-i inventory.ymlPlay name Ok Changed ... Skipped ... Task count Progress 0│Ensure HAProxy is deployed 5 0 ... 2 ... 7 Complete 1│Ensure Apache is deployed 12 0 ... 2 ... 14 Complete 2│Ensure Web App is deployed 4 0 ... 0 ... 4 Complete ^f/PgUp page up ^b/PgDn page down ↑↓ scroll esc back ...Successful
Press ESC to exit from the ansible-navigator command.
You have decided to change your YAML inventory to use inventory hostnames that are mapped to their purposes, rather than using actual hostnames.
Modify the inventory.yml file as follows:
In the lb_servers host group, change servera.lab.example.com to loadbalancer_1.
Assign loadbalancer_1 an ansible_host variable that configures its real hostname as servera.lab.example.com.
In the a_web_servers host group, change serverb.lab.example.com to backend_a1.
Assign backend_a1 an ansible_host variable that configures its real hostname as serverb.lab.example.com.
In the b_web_servers host group, change serverc.lab.example.com to backend_b1.
Assign backend_b1 an ansible_host variable that configures its real hostname as serverc.lab.example.com.
Modify the inventory.yml file so that each server follows its naming convention:
lb_servers:
hosts:
loadbalancer_1:
ansible_host: servera.lab.example.com
web_servers:
children:
a_web_servers:
hosts:
backend_a1:
ansible_host: serverb.lab.example.com
b_web_servers:
hosts:
backend_b1:
ansible_host: serverc.lab.example.comVerify that the site.yml playbook runs without errors when you use the updated inventory.yml inventory file.
You can use the curl command to test the load balancer again.
After the playbook runs without errors, commit the new inventory.yml file to your local Git repository.
If prompted, then use Student@123 as the Git password.
After you have committed all project changes, push the local commits to the remote repository.
Verify that the site.yml playbook runs without errors, and that the playbook output contains inventory hostnames that follow the naming conventions.
[student@workstation inventory-review]$ansible-navigator run site.yml \>-i inventory.ymlPlay name Ok Changed ... Skipped ... Task count Progress 0│Ensure HAProxy is deployed 5 0 ... 2 ... 7 Complete 1│Ensure Apache is deployed 12 0 ... 2 ... 14 Complete 2│Ensure Web App is deployed 4 2 ... 0 ... 4 Complete ^f/PgUp page up ^b/PgDn page down ↑↓ scroll esc back ...Successful
Press ESC to exit from the ansible-navigator command.
Verify that requests to servera produce two different versions of the web application using backend_a1 and backend_b1.
[student@workstation inventory-review]$for x in 1 2; do curl servera; doneHello frombackend_a1. (versionv1.1a) Hello frombackend_b1. (versionv1.1b)
Commit the new inventory.yml file.
[student@workstation inventory-review]$git add inventory.yml[student@workstation inventory-review]$git commit -m "Added YAML inventory"...output omitted...
Verify that all project changes are committed, and push all local commits to the remote repository.
If prompted, then use Student@123 as the Git password.
[student@workstation inventory-review]$git statusOn branch exercise Your branch is ahead of 'origin/exercise' by 2 commits. (use"git push"topublish your local commits) nothing to commit, working tree clean [student@workstation inventory-review]$git push -u origin exercisePassword for 'https://student@git.lab.example.com':Student@123...output omitted...