Red Hat Certified Engineer RHCE · Free Practice Question Medium
Question 13
You are asked to write a playbook that configures a web server to serve a static website. How would you use Ansible to ensure that the web server’s document root is configured, a sample HTML file is deployed, and the nginx service is started and enabled?
-
A
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.
-
B
Configuring a Web Server to Serve a Static Website
To configure the document root, deploy an HTML file, and startnginx:- - hosts: all
- tasks:
- - name: Configure document root
- ansible.builtin.file:
- path: /var/www/html
- state: directory
- - name: Deploy sample HTML file
- ansible.builtin.copy:
- src: /path/to/index.html
- dest: /var/www/html/index.html
- - name: Start and enable nginx
- ansible.builtin.service:
- name: nginx
- state: started
- enabled: yes
This sets up a static website and ensures
nginxis running.
Reveal correct answer
Correct answer: B
B.
This playbook configures a web server to host a static website by performing three tasks. First, it creates a document root directory for storing the website files. Second, it deploys a sample HTML file to this directory to serve as the website's homepage. Finally, it ensures the nginx service is installed, started, and enabled at boot. This setup guarantees that the static website is accessible and automatically starts with the server.
Line-by-Line Explanation:
Define Target Hosts:
- - hosts: all
Specifies that the playbook will run on all managed nodes listed in the inventory.
Create Document Root Directory:
- - name: Configure document root
- ansible.builtin.file:
- path: /var/www/html
- state: directory
Creates the
/var/www/htmldirectory, which will serve as the document root for the static website.The
state: directoryensures the specified path exists as a directory.
Deploy Sample HTML File:
- - name: Deploy sample HTML file
- ansible.builtin.copy:
- src: /path/to/index.html
- dest: /var/www/html/index.html
Copies a sample HTML file (
index.html) from the local system to the document root directory on the managed node.The
srcparameter specifies the file's source location, whiledestspecifies the destination location.
Start and Enable nginx:
- - name: Start and enable nginx
- ansible.builtin.service:
- name: nginx
- state: started
- enabled: yes
Ensures the
nginxservice is started and enabled to run at boot.The
state: startedparameter ensures the service is running, whileenabled: yesensures it starts automatically after a reboot.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
