EX294 Transform data with filters and plugins • Complete Question Bank
Complete EX294 Transform data with filters and plugins question bank — all 0 questions with answers and detailed explanations.
Refer to the exhibit.
- name: Parse CSV
hosts: localhost
vars:
csv_data: "name,age\nAlice,30\nBob,25"
tasks:
- name: Convert to list of dicts
set_fact:
parsed: "{{ csv_data | community.general.parse_csv }}"
- debug:
var: parsedRefer to the exhibit.
- name: Configure firewall
hosts: all
tasks:
- name: Allow HTTP
ansible.posix.firewalld:
service: http
permanent: yes
state: enabled
register: fw_result
- debug:
msg: "{{ 'changed' if fw_result.changed else 'no change' }}"Your organization manages a fleet of 500 web servers running RHEL 8. Each server has a custom fact file /etc/ansible/facts.d/web.fact containing:
[web]
docroot=/var/www/html port=80 admin=admin@example.com
You have a playbook that needs to configure the firewall to allow traffic on the port defined in the custom fact for each server. The playbook uses the 'ansible_local' variable to access these facts. However, some servers have the custom fact file missing or malformed. The task to open the firewall port fails on those servers with 'VARIABLE IS UNDEFINED'. You need to implement a solution that handles missing custom facts gracefully, setting a default port of 8080 if the fact is not defined, and still log a warning. Which approach should you take?
You are maintaining an Ansible automation for a large Red Hat Enterprise Linux deployment. The playbook configures NTP servers on all managed nodes. It uses a variable 'ntp_servers' defined in group_vars/all.yml as a list: ['0.rhel.pool.ntp.org', '1.rhel.pool.ntp.org', '2.rhel.pool.ntp.org']. The playbook task uses the 'uri' module to test connectivity to each server, but only if the server is reachable. The task currently uses:
``` - name: Test NTP server reachability uri: url: "http://{{ item }}:123" timeout: 5 register: result loop: "{{ ntp_servers }}" until: result.status == 200 retries: 3 delay: 2 ```
However, the playbook fails because the NTP servers do not respond to HTTP on port 123. You need to change the approach to test ICMP reachability using the 'ping' module, but the 'ping' module does not support a custom destination port. You also want to continue using a loop and register the success/failure per server. Which of the following is the best course of action?
Refer to the exhibit.
```
- name: Check Apache status
hosts: webservers
tasks:
- name: Get URL content
uri:
url: http://localhost/server-status
return_content: yes
register: result
- name: Debug output
debug:
var: result
```
Output:
```
ok: [web01] => {
"result": {
"changed": false,
"content": "<html><body><h1>Apache Status</h1>...</body></html>",
"status": 200,
"url": "http://localhost/server-status"
}
}
```Drag steps to the numbered slots on the right, or tap a step then tap a slot.
Drag a concept onto its matching description — or click a concept then click the description.
SELinux user identity
Role-based access control
Type enforcement (most common)
MLS sensitivity level
MLS/MCS category range
Refer to the exhibit. ``` [defaults] filter_plugins = /path/to/custom_filters ```
Refer to the exhibit.
```yaml
- hosts: localhost
tasks:
- debug:
msg: "{{ ['apple', 'banana'] | map('upper') | list | first }}"
```Refer to the exhibit.
```
fatal: [localhost]: FAILED! => {"msg": "template error while templating string: no filter named 'encrypt_string'. Use `ansible-doc -t filter plugin` to find available filters."}
```Refer to the exhibit.
```yaml
- name: Transform data
hosts: localhost
vars:
raw_data:
- " apple "
- "BANANA"
- " cherry "
tasks:
- name: Clean and normalize
set_fact:
clean_list: "{{ raw_data | map('trim') | map('lower') | list }}"
```Refer to the exhibit.
```yaml
- name: Example playbook
hosts: all
tasks:
- name: Set version fact
set_fact:
os_version: "{{ ansible_facts['ansible_distribution_version'] }}"
- name: Check if version filter works
debug:
msg: "{{ '7.9' is version('7.8', '>=') }}"
```Refer to the exhibit.
```yaml
- name: Get interface IP
hosts: localhost
vars:
interfaces:
- name: eth0
ip: 192.168.1.10
- name: eth1
ip: 10.0.0.5
tasks:
- name: Show IP of eth0
debug:
msg: "{{ interfaces | selectattr('name', 'equalto', 'eth0') | map(attribute='ip') | first }}"An administrator is writing a playbook to manage multiple web servers. The playbook uses a variable "server_facts" which is a list of dictionaries with keys "hostname", "ip", and "status". The administrator needs to extract a list of all hostnames where status is "online". The administrator writes:
- name: Get online hosts set_fact: online_hosts: "{{ server_facts | selectattr('status', '==', 'online') | map(attribute='hostname') | list }}"
However, when running the playbook, the "selectattr" filter fails with an error: "Invalid data passed to filter". The administrator checks the structure of "server_facts" and confirms it is a list of dicts with the expected keys. What is the most likely cause of the error?
An Ansible playbook is used to generate configuration files for network devices. The variables are defined in a vars file like: ---
interfaces:
- name: GigabitEthernet1 ip: 192.168.1.1/24 - name: GigabitEthernet2 ip: 10.0.0.1/24
The playbook uses a Jinja2 template to render the config. The template iterates over interfaces and writes "ip address" lines. However, the designer wants to support an additional field "secondary_ips" which is a list of IP addresses (e.g., ["192.168.2.1/24", "192.168.3.1/24"]). In the template, they want to generate multiple "ip address" lines for each interface, one for the primary IP and one for each secondary IP. The following template fragment is used:
{% for iface in interfaces %}
interface {{ iface.name }}
ip address {{ iface.ip }}
{% for sec in iface.secondary_ips|default([]) %}
ip address {{ sec }}
{% endfor %}
{% endfor %}This works when secondary_ips is defined. However, some interfaces have secondary_ips defined as a string (e.g., "192.168.2.1/24") instead of a list. The playbook fails because the inner loop tries to iterate over a string. The engineer wants to normalize the data in the playbook before passing to the template, so that secondary_ips is always a list. Which of the following set_fact tasks will correctly transform the interfaces list to ensure secondary_ips is always a list (even if missing or a string)?
An Ansible automation is used to manage firewall rules on a set of Linux servers. The playbook defines a variable "allow_rules" as: allow_rules: - proto: tcp dport: 80 comment: HTTP - proto: tcp dport: 443 comment: HTTPS
The engineer needs to use the "iptables" module to create rules. The module expects "chain" to be specified, and the engineer wants to dynamically set the chain based on the port: ports 80 and 443 go to "INPUT" chain, while others go to "FORWARD". The engineer writes a loop: - name: Add iptables rules iptables: chain: "{{ item.dport | map('some_filter') }}" protocol: "{{ item.proto }}" destination_port: "{{ item.dport }}" comment: "{{ item.comment }}" loop: "{{ allow_rules }}"
But this fails because the chain field expects a string, not a list. The engineer realizes the map filter returns a list. Which of the following modifications correctly sets the chain based on port number?
A senior automation engineer is optimizing a playbook that processes large amounts of data. The playbook uses the "json_query" filter to filter and extract specific fields from a complex JSON structure returned by an API. The engineer notices that the playbook runs very slowly and consumes a lot of memory. They suspect the json_query filter is inefficient for this use case. The engineer wants to replace json_query with a combination of built-in Ansible filters to improve performance. The JSON structure is as follows:
{
"servers": [
{"name": "web01", "status": "active", "role": "web"},
{"name": "web02", "status": "active", "role": "web"},
{"name": "db01", "status": "active", "role": "db"}
]
}The engineer needs to extract a list of server names where the status is "active" and the role is "web". The current code using json_query is: server_names: "{{ api_result | json_query(\"servers[?status=='active' && role=='web'].name\") }}" Which of the following alternatives uses only Ansible built-in filters (not json_query) and is likely to be more efficient?
Refer to the exhibit.
```yaml
---
- hosts: localhost
vars:
original_list:
- "hello123"
- "world456"
- "test789"
tasks:
- name: Filter list
set_fact:
filtered_list: "{{ original_list | select('match') }}"
```