Red Hat Certified Engineer RHCE · Free Practice Question Hard
Question 20
You are tasked with managing a heterogeneous environment with both CentOS and Ubuntu servers. How would you use Ansible’s inventory and conditional tasks to install different software packages on the CentOS servers and the Ubuntu servers?
-
A
Installing Different Packages on CentOS and Ubuntu
Use conditionals based on the OS family:- - hosts: all
- tasks:
- - name: Install package on CentOS
- ansible.builtin.yum:
- name: centos-package
- state: present
- when: ansible_os_family == "RedHat"
- - name: Install package on Ubuntu
- ansible.builtin.apt:
- name: ubuntu-package
- state: present
- when: ansible_os_family == "Debian"
This installs different packages depending on the OS 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.
This solution demonstrates how to use Ansible to install different software packages on heterogeneous environments with CentOS and Ubuntu servers. By leveraging the ansible_os_family fact, the playbook applies conditional tasks to ensure the appropriate package manager and package are used based on the server's operating system family. For CentOS (RedHat family), the yum module is used to install a package, while for Ubuntu (Debian family), the apt module is used. The when condition ensures each task runs only on the relevant OS type. This method provides flexibility and ensures accurate package management across diverse environments.
Line-by-Line Explanation:
Playbook:
- hosts: all
Targets all servers in the inventory.tasks:
Begins the list of tasks to execute on the servers.
Task 1: Install Package on CentOS 3. - name: Install package on CentOS
Provides a descriptive name for the task to clarify its purpose.
ansible.builtin.yum:
Uses theyummodule, which is specific to RedHat-based systems like CentOS.name: centos-package
Specifies the name of the package to be installed on CentOS servers.state: present
Ensures the package is installed. If already present, no action is taken.when: ansible_os_family == "RedHat"
Conditional statement that ensures the task runs only on systems where theansible_os_familyisRedHat.
Task 2: Install Package on Ubuntu 8. - name: Install package on Ubuntu
Describes the task to install a package on Ubuntu servers.
ansible.builtin.apt:
Uses theaptmodule, which is specific to Debian-based systems like Ubuntu.name: ubuntu-package
Specifies the name of the package to be installed on Ubuntu servers.state: present
Ensures the package is installed if it is not already present.when: ansible_os_family == "Debian"
Conditional statement that ensures the task runs only on systems where theansible_os_familyisDebian.
Key Notes:
The
ansible_os_familyfact dynamically identifies the operating system type, enabling conditional task execution.The
yummodule is used for RedHat-based systems (e.g., CentOS), while theaptmodule is used for Debian-based systems (e.g., Ubuntu).The
whencondition ensures tasks run only on relevant systems, preventing errors or misconfigurations.This approach ensures efficient and accurate package management across heterogeneous environments.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
