Red Hat Certified Engineer RHCE · Free Practice Question Medium
Question 1
You are tasked with deploying an application that uses different versions of a configuration file depending on the environment (e.g., development, staging, production). How would you use Ansible’s with_first_found lookup to deploy the correct version of the configuration file based on the environment?
-
A
Using with_first_found to Deploy Environment-Specific Config
Usewith_first_foundto choose the correct configuration based on the environment:- - hosts: all
- vars:
- env: "{{ lookup('env', 'ENVIRONMENT') }}"
- tasks:
- - name: Deploy environment-specific config
- ansible.builtin.template:
- src: "{{ item }}"
- dest: /etc/myapp/config.yml
- with_first_found:
- - "configs/{{ env }}/config.yml"
- - "configs/default/config.yml"
This deploys the correct config file based on the environment (dev, staging, prod).
-
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.
The provided Ansible playbook snippet demonstrates the correct usage of the with_first_found lookup to deploy environment-specific configuration files based on the environment variable. It first sets the 'env' variable to the value of the 'ENVIRONMENT' environment variable. Then, it uses with_first_found to look for the configuration file specific to the current environment in the 'configs/{{ env }}/config.yml' path. If the file is found, it is deployed to '/etc/myapp/config.yml'. If not found, it falls back to the default configuration file in 'configs/default/config.yml'.
Line-by-Line Code Explanation
1. - hosts: all
Specifies the target hosts for the playbook.
all means the playbook will apply to all hosts listed in the inventory file.
2. vars:
Declares variables used within the playbook.
Variables can be static or dynamically populated using lookups or other Ansible constructs.
3. env: "{{ lookup('env', 'ENVIRONMENT') }}"
Defines a variable env using the lookup plugin.
lookup('env', 'ENVIRONMENT') fetches the value of the ENVIRONMENT environment variable from the system running the playbook.
Example: If ENVIRONMENT is set to production, then env will be production.
If ENVIRONMENT is not set, env will be empty.
4. tasks:
Lists the actions (tasks) that Ansible will perform on the target hosts.
5. - name: Deploy environment-specific config
Provides a human-readable description of the task for logging and readability.
6. ansible.builtin.template:
Specifies the template module (part of Ansible's core modules).
This module processes a Jinja2 template file (src) and writes the rendered output to a destination file (dest) on the target host.
7. src: "{{ item }}"
Indicates the source file for the template module.
item is dynamically populated by the with_first_found loop.
Ansible will search for files in the order specified in the loop and use the first one that exists.
8. dest: /etc/myapp/config.yml
Specifies the destination path where the rendered template will be copied on the target host.
In this case, it will save the configuration file to /etc/myapp/config.yml.
9. with_first_found:
A looping construct that takes a list of file paths.
The template module will use the first file in the list that exists on the Ansible controller (or in the repository).
10. - "configs/{{ env }}/config.yml"
The first file path in the with_first_found list.
Uses the env variable to construct the path dynamically based on the environment.
Example: If env is production, the path becomes configs/production/config.yml.
11. - "configs/default/config.yml"
A fallback file path if the environment-specific file does not exist.
Ensures that the playbook still completes successfully by using a default configuration.
Execution Flow
Ansible resolves the value of env based on the ENVIRONMENT environment variable.
It checks the paths in the with_first_found list, starting with configs/{{ env }}/config.yml.
If the environment-specific file is found, it is used; otherwise, the default file is applied.
The selected file is processed as a Jinja2 template and saved to /etc/myapp/config.yml on the target host.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
