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:

    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:

  1. - hosts: all
    Targets all servers in the inventory.

  2. 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.

  1. ansible.builtin.yum:
    Uses the yum module, which is specific to RedHat-based systems like CentOS.

  2. name: centos-package
    Specifies the name of the package to be installed on CentOS servers.

  3. state: present
    Ensures the package is installed. If already present, no action is taken.

  4. when: ansible_os_family == "RedHat"
    Conditional statement that ensures the task runs only on systems where the ansible_os_family is RedHat.

Task 2: Install Package on Ubuntu 8. - name: Install package on Ubuntu
Describes the task to install a package on Ubuntu servers.

  1. ansible.builtin.apt:
    Uses the apt module, which is specific to Debian-based systems like Ubuntu.

  2. name: ubuntu-package
    Specifies the name of the package to be installed on Ubuntu servers.

  3. state: present
    Ensures the package is installed if it is not already present.

  4. when: ansible_os_family == "Debian"
    Conditional statement that ensures the task runs only on systems where the ansible_os_family is Debian.

Key Notes:

  • The ansible_os_family fact dynamically identifies the operating system type, enabling conditional task execution.

  • The yum module is used for RedHat-based systems (e.g., CentOS), while the apt module is used for Debian-based systems (e.g., Ubuntu).

  • The when condition 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.

You must be logged in to post a comment.

Preparing For

Your Certification?

255+ certifications
Detailed explanations
Free PDF samples

Has All The Questions You Need