Red Hat Certified Engineer RHCE · Free Practice Question Medium
Question 19
A Content Collection you’re using includes roles for configuring network devices. How would you ensure that the roles are applied to specific network devices based on their inventory group (e.g., routers, switches) in your playbook?
-
A
Applying Roles to Specific Network Devices Based on Inventory Groups
In yourinventory.ini:- [routers]
- router1 ansible_host=192.168.1.1
- [switches]
- switch1 ansible_host=192.168.1.2
In the playbook, apply roles based on the group:
- - hosts: routers
- roles:
- - configure_router_role
- - hosts: switches
- roles:
- - configure_switch_role
This ensures roles are applied based on the device type.
-
B
This is a performance based question and not multiple choice. Therefore the answer is in #1. If you require additional explanation, please ask your question in the Q&A section.
Reveal correct answer
Correct answer: A
A.
In this solution, the Ansible playbook is structured to apply specific roles to different network devices based on their inventory group. In the inventory.ini file, network devices are grouped into [routers] and [switches] groups. Each device in these groups is associated with a specific role. In the playbook, the routers group will apply the configure_router_role, while the switches group will apply the configure_switch_role. This ensures that the correct configuration role is applied to each device type (router or switch) based on the group it belongs to in the inventory, allowing for efficient management of network devices.
Line-by-Line Explanation
[routers]Defines the
routersgroup in the inventory. This group will include all the routers that need to be configured.
router1 ansible_host=192.168.1.1Specifies that
router1is part of theroutersgroup, and it has the IP address192.168.1.1. Ansible will use this IP to connect to the router.
[switches]Defines the
switchesgroup in the inventory. This group will include all the switches that need to be configured.
switch1 ansible_host=192.168.1.2Specifies that
switch1is part of theswitchesgroup, and it has the IP address192.168.1.2. Ansible will use this IP to connect to the switch.
- hosts: routersIn the playbook, this line indicates that the following task will be executed on all hosts in the
routersgroup.
roles:Introduces the list of roles to be applied to the hosts in the
routersgroup.
- configure_router_roleSpecifies the role to be applied to the devices in the
routersgroup. In this case, it is theconfigure_router_role, which contains tasks for configuring routers.
- hosts: switchesIn the playbook, this line indicates that the following task will be executed on all hosts in the
switchesgroup.
roles:Introduces the list of roles to be applied to the hosts in the
switchesgroup.
- configure_switch_roleSpecifies the role to be applied to the devices in the
switchesgroup. In this case, it is theconfigure_switch_role, which contains tasks for configuring switches.
This method ensures that the correct role is applied to each device type (router or switch), based on the inventory group the device belongs to. It makes managing different network devices efficient by applying group-specific roles for configuration.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
