Set up directories that contain multiple host variable files for some of your managed hosts, and override the name used in the inventory file with a different name or IP address for at least one of your hosts.
Outcomes
Split the location of host variable files across multiple directories to improve maintainability.
As the student user on the workstation machine, use the lab command to prepare your system for this exercise.
This command ensures that Ansible is installed on workstation and also prepares the /home/student/inventory-variables directory for this exercise.
[student@workstation ~]$ lab start inventory-variables
Procedure 5.3. Instructions
Get familiar with the Ansible project and its current state.
As the student user on the workstation machine, change to the /home/student/inventory-variables directory.
[student@workstation ~]$ cd ~/inventory-variables/List the files that the lab command has prepared for you.
[student@workstation inventory-variables]$tree -L 1 -F. ├── ansible.cfg ├── ansible-navigator.yml ├── deploy_apache.yml├── deploy_haproxy.yml
├── deploy_webapp.yml
├── inventory.yml
├── roles/ └── site.yml
The | |
The | |
The | |
The | |
The global |
Review the inventory.yml file.
[student@workstation inventory-variables]$ cat inventory.yml
lb_servers:
hosts:
servera.lab.example.com:
web_servers:
hosts:
server[b:c].lab.example.com:The deploy_haproxy.yml playbook declares two variables: haproxy_appservers, which provides the HAProxy configuration; and firewall_rules, which defines the firewall configuration.
Move those two variables into inventory variable files.
Review the deploy_haproxy.yml playbook.
[student@workstation inventory-variables]$cat deploy_haproxy.yml--- - name: Ensuring HAProxy is deployed hosts:lb_serversbecome: true gather_facts: false tasks: - name: Ensure HAProxy is installed and configured ansible.builtin.include_role: name: haproxy vars:haproxy_appservers: - name: serverb.lab.example.com ip: 172.25.250.11 port: 80 - name: serverc.lab.example.com ip: 172.25.250.12 port: 80 - name: Ensure the firewall ports are opened ansible.builtin.include_role: name: firewall vars:firewall_rules: - port: 80/tcp
Create the group_vars/lb_servers/ directory to hold the variable files for the lb_servers host group.
[student@workstation inventory-variables]$ mkdir -p group_vars/lb_serversCreate the group_vars/lb_servers/haproxy.yml variable file to store the HAProxy configuration.
Copy and paste the haproxy_appservers variable definition from the deploy_haproxy.yml playbook.
[student@workstation inventory-variables]$ cat group_vars/lb_servers/haproxy.yml
---
haproxy_appservers:
- name: serverb.lab.example.com
ip: 172.25.250.11
port: 80
- name: serverc.lab.example.com
ip: 172.25.250.12
port: 80Create the group_vars/lb_servers/firewall.yml variable file to store the firewall configuration.
Copy and paste the firewall_rules variable definition from the deploy_haproxy.yml playbook.
[student@workstation inventory-variables]$ cat group_vars/lb_servers/firewall.yml
---
firewall_rules:
- port: 80/tcpEdit the deploy_haproxy.yml playbook and then remove the two variables.
The resulting file should consist of the following content:
---
- name: Ensuring HAProxy is deployed
hosts: lb_servers
become: true
gather_facts: false
tasks:
- name: Ensure HAProxy is installed and configured
ansible.builtin.include_role:
name: haproxy
- name: Ensure the firewall ports are opened
ansible.builtin.include_role:
name: firewallThe deploy_apache.yml playbook declares the firewall_rules variable that defines the firewall configuration for the web servers.
Move those two variables into an inventory variable file.
Review the deploy_apache.yml playbook.
[student@workstation inventory-variables]$cat deploy_apache.yml--- - name: Ensuring Apache HTTP Server is deployed hosts:web_serversbecome: true gather_facts: false tasks: - name: Ensure Apache HTTP Server is installed and started ansible.builtin.include_role: name: apache - name: Ensure the firewall ports are opened ansible.builtin.include_role: name: firewall vars:firewall_rules: # Allow HTTP requests from the load_balancer - zone: internal service: http - zone: internal source: "172.25.250.10"
Create the group_vars/web_servers/ directory to hold the variable files for the web_servers host group.
[student@workstation inventory-variables]$ mkdir -p group_vars/web_serversCreate the group_vars/web_servers/firewall.yml variable file to store the firewall configuration.
Copy and paste the firewall_rules variable definition from the deploy_apache.yml playbook.
[student@workstation inventory-variables]$ cat group_vars/web_servers/firewall.yml
---
firewall_rules:
# Allow HTTP requests from the load_balancer
- zone: internal
service: http
- zone: internal
source: "172.25.250.10"Edit the deploy_apache.yml playbook and then remove the firewall_rules variable.
The resulting file should consist of the following content:
---
- name: Ensuring Apache HTTP Server is deployed
hosts: web_servers
become: true
gather_facts: false
tasks:
- name: Ensure Apache HTTP Server is installed and started
ansible.builtin.include_role:
name: apache
- name: Ensure the firewall ports are opened
ansible.builtin.include_role:
name: firewallEdit the inventory.yml static inventory file so that Ansible displays the servera.lab.example.com host as load_balancer in its output.
The resulting file should consist of the following content:
lb_servers:
hosts:
load_balancer:
ansible_host: servera.lab.example.com
web_servers:
hosts:
server[b:c].lab.example.com:Run the site.yml playbook and confirm that the web deployment was successful.
Use the ansible-navigator run command to run the site.yml playbook.
[student@workstation inventory-variables]$ansible-navigator run \>site.yml --mode stdoutPLAY [Ensuring HAProxy is deployed] ******************************************** TASK [Ensure HAProxy is installed and configured] ****************************** TASK [haproxy : Ensure the HAProxy packages are installed] ********************* changed: [load_balancer] TASK [haproxy : Ensure HAProxy is started and enabled] ************************* changed: [load_balancer] TASK [haproxy : Ensure HAProxy configuration is set] *************************** changed: [load_balancer] ...output omitted...
Use the curl command to confirm that the HAProxy server distributes the HTTP requests between the two Apache HTTP Servers.
[student@workstation inventory-variables]$curl http://servera.lab.example.comThis isserverb.lab.example.com. (version v1.0) [student@workstation inventory-variables]$curl http://servera.lab.example.comThis isserverc.lab.example.com. (version v1.0)