Red Hat Certified Engineer RHCE · Free Practice Question Medium
Question 17
You need to ensure that all managed nodes have the latest updates installed from their respective repositories. How would you use the yum or apt module in Ansible to perform a system-wide update on all nodes?
-
A
Performing a System-Wide Update Using
yumorapt
To update all packages on managed nodes:- - hosts: all
- tasks:
- - name: Perform a system-wide update (YUM)
- ansible.builtin.yum:
- name: "*"
- state: latest
- - name: Perform a system-wide update (APT)
- ansible.builtin.apt:
- update_cache: yes
- upgrade: dist
This ensures all packages are updated to the latest version.
-
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 answer, two Ansible modules, yum and apt, are used to perform a system-wide update on managed nodes. The first task uses the yum module to update all packages on nodes that use YUM-based package managers (like RHEL/CentOS), while the second task uses the apt module to update all packages on nodes that use APT-based package managers (like Ubuntu/Debian). The state: latest ensures that the packages are updated to their latest available versions, and for APT, the update_cache: yes ensures that the package cache is updated before performing the upgrade.
Line-by-Line Explanation
- hosts: allSpecifies that the tasks will be executed on all hosts in the inventory.
tasks:Introduces the list of tasks that Ansible will perform on the managed nodes.
- name: Perform a system-wide update (YUM)This task performs the system-wide update using YUM on nodes that use a YUM-based package manager.
ansible.builtin.yum:This specifies the use of the
yummodule to manage packages on RHEL/CentOS or other YUM-based systems.
name: "*"This ensures that the update applies to all packages available in the repositories.
state: latestThis ensures that the packages are updated to the latest version available in the repository.
- name: Perform a system-wide update (APT)This task performs the system-wide update using APT on nodes that use an APT-based package manager.
ansible.builtin.apt:This specifies the use of the
aptmodule to manage packages on Debian/Ubuntu-based systems.
update_cache: yesThis ensures that the local package cache is updated before performing any upgrades, which helps ensure that the latest package versions are fetched.
upgrade: distThis upgrades all installed packages to the latest available versions, including distribution upgrades. The
distoption means upgrading to the latest distribution release, not just package updates.
This playbook ensures that all packages on managed nodes are updated to the latest available versions. The playbook uses the yum module for systems with YUM package management (e.g., RHEL/CentOS) and the apt module for systems with APT package management (e.g., Ubuntu/Debian), ensuring compatibility across different Linux distributions. The state: latest ensures the packages are upgraded, and the update_cache: yes ensures the package manager has the latest information on available updates.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
