Red Hat Certified Engineer RHCE · Free Practice Question Medium
Question 11
You are tasked with configuring a group of servers to act as web servers, with all necessary packages installed, the correct configuration files deployed, and the web service started. How would you create a playbook that ensures these servers are configured to the desired state?
-
A
Configuring Web Servers to the Desired State
To install packages, deploy config files, and start the web service:- - hosts: web_servers
- tasks:
- - name: Install necessary packages
- ansible.builtin.package:
- name: nginx
- state: present
- - name: Deploy configuration file
- ansible.builtin.copy:
- src: /path/to/nginx.conf
- dest: /etc/nginx/nginx.conf
- - name: Ensure nginx is running
- ansible.builtin.service:
- name: nginx
- state: started
- enabled: yes
This configures the servers as web servers.
-
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 playbook defines tasks to install the necessary packages, deploy the configuration file, and ensure that theThis Ansible playbook configures web servers by installing the necessary web server software, deploying configuration files, and ensuring the web service is running. The playbook performs three tasks in sequence: first, it installs the Nginx package using the package module; second, it deploys the Nginx configuration file to the target servers using the copy module; and third, it ensures the Nginx service is running and enabled to start at boot using the service module. By executing these tasks, the playbook ensures the web servers are configured to the desired state, with the necessary packages, configurations, and services in place.
Line-by-Line Explanation
- hosts: web_serversSpecifies that the tasks in this playbook will run on the group of hosts named
web_servers.
tasks:This introduces the list of tasks to be executed on the target hosts.
- name: Install necessary packagesThis task is named "Install necessary packages," which installs the required software package for the web server.
ansible.builtin.package:This uses the
packagemodule to install software packages on the managed nodes.
name: nginxSpecifies the name of the package to be installed, in this case,
nginx.
state: presentEnsures that the
nginxpackage is installed. If it is already present, no action will be taken.
- name: Deploy configuration fileThis task is responsible for deploying the configuration file for Nginx.
ansible.builtin.copy:Uses the
copymodule to copy a file from the local machine to the target machine.
src: /path/to/nginx.confSpecifies the source path of the Nginx configuration file on the local machine.
dest: /etc/nginx/nginx.confSpecifies the destination path where the configuration file will be copied on the target machine.
- name: Ensure nginx is runningThis task ensures that the Nginx service is started and running on the target web servers.
ansible.builtin.service:Uses the
servicemodule to manage system services on the target machines.
name: nginxSpecifies the service name, in this case,
nginx, which is the web server service.
state: startedEnsures the Nginx service is started.
enabled: yesEnsures that the Nginx service is enabled to start automatically on system boot.
By following this structure, the playbook ensures that the web servers are correctly configured, with the required software, configuration files, and services running as expected. nginx web service is running on the designated web servers. By using the Ansible built-in modules for package installation, file copying, and service management, this playbook effectively configures the servers to act as web servers with the desired state.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
