RedHat Red Hat Certified Specialist in Developing Automation with Ansible Automation Platform - EX374 模擬練習
Add a role named webserver to your collection.
正解:
ansible-galaxy init roles/webserver --collection my_namespace.my_collection
Explanation:
The ansible-galaxy init command creates a skeleton role within the specified collection, facilitating the addition of reusable functionality.
Explanation:
The ansible-galaxy init command creates a skeleton role within the specified collection, facilitating the addition of reusable functionality.
Use group variables in a playbook to configure web servers.
正解:
- name: Configure web servers hosts: web
tasks:
- name: Install and start web server service:
name: httpd
state: started
- name: Configure port lineinfile:
path: /etc/httpd/conf/httpd.conf
regexp: "^Listen"
line: "Listen {{ http_port }}"
Explanation:
Group variables simplify configurations for multiple hosts by enabling shared values for tasks.
tasks:
- name: Install and start web server service:
name: httpd
state: started
- name: Configure port lineinfile:
path: /etc/httpd/conf/httpd.conf
regexp: "^Listen"
line: "Listen {{ http_port }}"
Explanation:
Group variables simplify configurations for multiple hosts by enabling shared values for tasks.
Use the lookup plugin to retrieve a value from an external file and assign it to a variable in a playbook.
正解:
# playbook.yml
- name: Retrieve data from external file hosts: localhost
tasks:
- name: Read value from file set_fact:
file_content: "{{ lookup('file', '/tmp/example.txt') }}"
- debug:
var: file_content
Explanation:
The file lookup plugin reads data from an external file and assigns it to a variable, useful for incorporating external configuration.
- name: Retrieve data from external file hosts: localhost
tasks:
- name: Read value from file set_fact:
file_content: "{{ lookup('file', '/tmp/example.txt') }}"
- debug:
var: file_content
Explanation:
The file lookup plugin reads data from an external file and assigns it to a variable, useful for incorporating external configuration.
Inspect the logs of an EE build process.
正解:
podman logs $(podman ps -aq --filter ancestor=my_execution_env:1.0)
Explanation:
Inspecting logs helps troubleshoot issues that occur during the build process, ensuring a functional EE.
Explanation:
Inspecting logs helps troubleshoot issues that occur during the build process, ensuring a functional EE.
Use lookup to retrieve the latest modification time of a file.
正解:
- name: Get file modification time hosts: localhost
tasks:
- name: Fetch modification time set_fact:
mod_time: "{{ lookup('pipe', 'stat -c %y /tmp/example.txt') }}"
- debug:
var: mod_time
Explanation:
The pipe plugin retrieves external command output, enabling access to file metadata like modification time.
tasks:
- name: Fetch modification time set_fact:
mod_time: "{{ lookup('pipe', 'stat -c %y /tmp/example.txt') }}"
- debug:
var: mod_time
Explanation:
The pipe plugin retrieves external command output, enabling access to file metadata like modification time.
Create SSH machine credentials to access inventory hosts.
正解:
ansible-vault create ssh_creds.yml
Add credentials:
ansible_user: "admin"
ansible_ssh_private_key_file: "~/.ssh/id_rsa"
Explanation:
Machine credentials stored in a Vault file ensure secure and consistent access to inventory hosts.
Add credentials:
ansible_user: "admin"
ansible_ssh_private_key_file: "~/.ssh/id_rsa"
Explanation:
Machine credentials stored in a Vault file ensure secure and consistent access to inventory hosts.
Run a command on the control node but store the result in the context of a managed host.
正解:
- name: Store control node result hosts: web1
tasks:
- name: Get disk usage on control node
command: df -h
delegate_to: localhost
register: disk_usage
- name: Show disk usage debug:
var: disk_usage
Explanation:
This task demonstrates delegating commands to the control node while retaining results within the managed host's context.
tasks:
- name: Get disk usage on control node
command: df -h
delegate_to: localhost
register: disk_usage
- name: Show disk usage debug:
var: disk_usage
Explanation:
This task demonstrates delegating commands to the control node while retaining results within the managed host's context.
Use delegate_to to copy a file from the control node to a managed node.
正解:
- name: Copy file using delegation hosts: web1
tasks:
- name: Copy file to db1 copy:
src: /tmp/example.txt
dest: /tmp/example.txt delegate_to: db1
Explanation:
The task uses the copy module to transfer files from the control node to db1, delegating the task regardless of the web1 context.
tasks:
- name: Copy file to db1 copy:
src: /tmp/example.txt
dest: /tmp/example.txt delegate_to: db1
Explanation:
The task uses the copy module to transfer files from the control node to db1, delegating the task regardless of the web1 context.
Trigger a playbook run using the Automation Controller API.
正解:
curl -X POST -H "Authorization: Bearer <token>" \
-d '{"inventory": 1, "job_template": 1}' \
https://controller.example.com/api/v2/job_templates/1/launch/
Explanation:
Using the API enables integration with external systems to trigger playbook executions programmatically.
-d '{"inventory": 1, "job_template": 1}' \
https://controller.example.com/api/v2/job_templates/1/launch/
Explanation:
Using the API enables integration with external systems to trigger playbook executions programmatically.
Add a group variable in an advanced inventory.
正解:
# inventory_group_vars.yml all:
children:
web:
hosts:
web1:
web2:
vars:
http_port: 8080
Explanation:
Group variables allow setting common configurations for a group of hosts, reducing redundancy in inventory management.
children:
web:
hosts:
web1:
web2:
vars:
http_port: 8080
Explanation:
Group variables allow setting common configurations for a group of hosts, reducing redundancy in inventory management.
Install the collection locally from the .tar.gz file.
正解:
ansible-galaxy collection install my_namespace-my_collection-1.0.0.tar.gz
Explanation:
Installing from a tarball ensures the collection is available in the local Ansible environment for immediate use.
Explanation:
Installing from a tarball ensures the collection is available in the local Ansible environment for immediate use.
Test a custom inventory plugin.
正解:
ansible-inventory -i plugins/inventory/my_plugin.py --list
Explanation:
Testing custom plugins ensures they generate valid inventories compatible with Ansible playbooks.
Explanation:
Testing custom plugins ensures they generate valid inventories compatible with Ansible playbooks.
Transform a list of numbers by doubling each value.
正解:
- name: Double numbers in a list hosts: localhost
vars:
numbers: [1, 2, 3, 4] tasks:
- name: Transform list debug:
var: "{{ numbers | map('multiply', 2) | list }}"
Explanation:
Using the map filter with multiply applies transformations to each element in a list.
vars:
numbers: [1, 2, 3, 4] tasks:
- name: Transform list debug:
var: "{{ numbers | map('multiply', 2) | list }}"
Explanation:
Using the map filter with multiply applies transformations to each element in a list.
Retrieve and display a list of available files in a directory using the lookup plugin.
正解:
- name: List directory files hosts: localhost
tasks:
- name: Get files in directory set_fact:
files: "{{ lookup('pipe', 'ls /tmp') }}"
- debug:
var: files
Explanation:
The pipe lookup plugin executes shell commands, and here it lists files in /tmp. This data is stored in a variable for further processing.
tasks:
- name: Get files in directory set_fact:
files: "{{ lookup('pipe', 'ls /tmp') }}"
- debug:
var: files
Explanation:
The pipe lookup plugin executes shell commands, and here it lists files in /tmp. This data is stored in a variable for further processing.