Courseiva

EX294 Transform data with filters and plugins Practice Question

A playbook reads a JSON inventory file with `lookup('file', 'hosts.json') | from_json` and registers it as `hostdata`. The structure is a top-level dictionary where each key is a hostname and each value is a dictionary of attributes, for example `{"web1": {"env": "prod", "cpu": 4}, "db1": {"env": "dev", "cpu": 8}}`. You must build a list containing only the hostnames whose `env` attribute equals `prod`. Which task correctly produces that list?

⚠ Common exam trap

The trap here is assuming `selectattr` can filter a plain dictionary directly and that nested attributes are reachable without first converting the mapping with `dict2items`.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

✓

- set_fact: prod_hosts: "{{ hostdata | dict2items | selectattr('value.env', 'equalto', 'prod') | map(attribute='key') | list }}"

Because the source is a dictionary keyed by hostname, it must first be turned into an iterable list of pairs with `dict2items`. Each pair then exposes the hostname as `key` and the attribute dictionary as `value`, so the environment test must reference `value.env`. Extracting `key` afterwards yields exactly the list of prod hostnames the task requires.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✗

    - set_fact: prod_hosts: "{{ hostdata | dict2items | selectattr('value.env', 'equalto', 'prod') | map(attribute='value') | list }}"

    Why it's wrong here

    The filtering portion is correct, but `map(attribute='value')` returns the attribute dictionaries such as `{'env': 'prod', 'cpu': 4}` rather than the hostnames. The requirement is a list of hostnames, which are stored under the `key` attribute of each pair. Using `value` yields dictionaries, not the requested strings.

  • ✗

    - set_fact: prod_hosts: "{{ hostdata | dict2items | selectattr('env', 'equalto', 'prod') | map(attribute='key') | list }}"

    Why it's wrong here

    After `dict2items`, each element is a pair with only `key` and `value` attributes; there is no top-level `env` attribute. The `selectattr('env', ...)` test therefore matches nothing and the result is an empty list. The environment value actually lives at `value.env`, so the attribute path must reflect that nesting.

  • ✓

    - set_fact: prod_hosts: "{{ hostdata | dict2items | selectattr('value.env', 'equalto', 'prod') | map(attribute='key') | list }}"

    Why this is correct

    `dict2items` converts the mapping into a list of `{key, value}` pairs, so each item exposes the hostname under `key` and its attributes under `value`. `selectattr('value.env', 'equalto', 'prod')` keeps only prod entries, and `map(attribute='key')` extracts the hostnames into a list. This chain matches the nested data shape exactly.

  • ✗

    - set_fact: prod_hosts: "{{ hostdata | selectattr('env', 'equalto', 'prod') | map(attribute='name') | list }}"

    Why it's wrong here

    `selectattr` operates on a list of items, not on a dictionary, so passing the raw mapping fails before any filtering happens. Even if it iterated, `env` is nested one level down under each value, and there is no `name` attribute anywhere in this structure. The `map` call would therefore produce undefined entries.

About these practice questions

One of 392 original EX294 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written and reviewed by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

Last reviewed September 2026 · checked against the official Red Hat exam blueprint

This EX294 practice question is part of Courseiva's free Red Hat certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the EX294 exam.