Question 353 of 528
Implement advanced Ansible automationmediumMultiple ChoiceObjective-mapped

Ansible lineinfile Duplicate Lines — Fix Regexp Matching Issues | Red Hat Certified Engineer Explained

This EX294 practice question tests your understanding of implement advanced ansible automation. Read the scenario carefully and evaluate each option against the stated constraints before committing to an answer. After answering, compare your reasoning against the explanation and wrong-answer breakdown below. Once you have made your selection, read the full explanation to reinforce the concept and understand why each distractor is designed to mislead on exam day.

You have an Ansible playbook that uses the 'lineinfile' module to manage the /etc/ssh/sshd_config file. The playbook runs without errors, but after execution, the SSH service becomes unreachable on some hosts. Investigation reveals that the file contains duplicate lines for 'Port 22' and 'PermitRootLogin no'. The playbook uses the following task:

- name: Ensure SSH settings ansible.builtin.lineinfile: path: /etc/ssh/sshd_config regexp: "^{{ item.key }}" line: "{{ item.key }} {{ item.value }}" loop: - { key: 'Port', value: '22' } - { key: 'PermitRootLogin', value: 'no' }

The playbook is run multiple times. What is the most likely cause of the duplicate lines?

Clue words in this question

Noticing these words before you look at the options changes how you read each choice.

  • Clue: "most likely"

    Why it matters: Probability qualifier — the question wants the most probable cause or outcome, not a guaranteed one. Eliminate low-probability options.

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

The regexp pattern does not match the existing lines exactly, causing new lines to be added instead of replacing.

Option A is correct because the regexp pattern `^{{ item.key }}` matches only the start of a line (e.g., `^Port` or `^PermitRootLogin`), but the existing lines in `/etc/ssh/sshd_config` may have leading spaces or tabs (common in SSH config files). Since the regex does not match the actual line due to whitespace, the `lineinfile` module inserts a new line instead of replacing the existing one. Each playbook run adds another duplicate, eventually causing SSH to fail due to conflicting directives.

Key principle: Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option.

Answer analysis

Option-by-option breakdown

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

  • The regexp pattern does not match the existing lines exactly, causing new lines to be added instead of replacing.

    Why this is correct

    If the line has leading spaces or is not at the start, it may not match.

    Clue confirmation

    The clue word "most likely" in the question point toward this answer.

    Related concept

    Read the scenario before looking for a memorised answer.

  • The 'lineinfile' module is not idempotent when used with loops.

    Why it's wrong here

    The module is idempotent if the regexp matches correctly.

  • The loop should use 'with_items' instead of 'loop' for proper idempotence.

    Why it's wrong here

    Both 'loop' and 'with_items' work similarly.

  • The 'line' parameter should use backreferences to avoid duplication.

    Why it's wrong here

    Backreferences are not needed for simple key-value pairs.

Common exam traps

Common exam trap: answer the scenario, not the keyword

The trap here is that candidates assume `regexp` with `^` will match any line starting with the key, but they overlook that SSH config files often have leading whitespace, causing the module to add duplicate lines instead of replacing.

Trap categories for this question

  • Similar concept trap

    Both 'loop' and 'with_items' work similarly.

Detailed technical explanation

How to think about this question

The `lineinfile` module uses the `regexp` parameter to locate an existing line; if no match is found, it appends the `line` parameter as a new line. SSH config files often have indentation (e.g., ` Port 22`), so `^Port` fails to match. A robust fix would use `regexp: '^\s*{{ item.key }}'` to ignore leading whitespace. In real-world scenarios, this issue also appears with other config files like `/etc/chrony.conf` or `/etc/security/limits.conf` where indentation is common.

KKey Concepts to Remember

  • Read the scenario before looking for a memorised answer.
  • Find the constraint that changes the correct option.
  • Eliminate answers that are true in general but not in this case.

TExam Day Tips

  • Watch for words such as best, first, most likely and least administrative effort.
  • Review why wrong options are wrong, not only why the correct option is correct.

Key takeaway

Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option.

Real-world example

How this comes up in practice

A security administrator must allow nursing staff to reach a patient records server while blocking access from the guest Wi-Fi VLAN. After applying an extended ACL, traffic is still blocked from nursing workstations. The ACL was applied outbound instead of inbound on the wrong interface. Questions like this test ACL direction and placement rules.

What to study next

Got this wrong? Here's your next step.

Identify which exam domain this question belongs to, review the core concept, then practise similar questions from the same domain.

Related practice questions

Related EX294 practice-question pages

Use these pages to review the topic behind this question. This is how one missed question becomes focused revision.

Practice this exam

Start a free EX294 practice session

Short sessions build daily habit. Longer sessions build exam-day stamina. Try a timed session to simulate real conditions.

FAQ

Questions learners often ask

What does this EX294 question test?

Implement advanced Ansible automation — This question tests Implement advanced Ansible automation — Read the scenario before looking for a memorised answer..

What is the correct answer to this question?

The correct answer is: The regexp pattern does not match the existing lines exactly, causing new lines to be added instead of replacing. — Option A is correct because the regexp pattern `^{{ item.key }}` matches only the start of a line (e.g., `^Port` or `^PermitRootLogin`), but the existing lines in `/etc/ssh/sshd_config` may have leading spaces or tabs (common in SSH config files). Since the regex does not match the actual line due to whitespace, the `lineinfile` module inserts a new line instead of replacing the existing one. Each playbook run adds another duplicate, eventually causing SSH to fail due to conflicting directives.

What should I do if I get this EX294 question wrong?

Identify which exam domain this question belongs to, review the core concept, then practise similar questions from the same domain.

Are there clue words in this question I should notice?

Yes — watch for: "most likely". Probability qualifier — the question wants the most probable cause or outcome, not a guaranteed one. Eliminate low-probability options.

What is the key concept behind this question?

Read the scenario before looking for a memorised answer.

About these practice questions

Courseiva creates original exam-style practice questions with explanations and wrong-answer analysis. It does not publish real exam questions, exam dumps, or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

Same concept, more angles

1 more ways this is tested on EX294

These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.

Variation 1. An Ansible playbook uses the lineinfile module to ensure a specific line is present in a configuration file. However, after running the playbook multiple times, the line is duplicated each time. Which option best explains this behavior?

easy
  • A.The lineinfile module is missing the regexp parameter, so it adds the line every run.
  • B.The state parameter is set to 'append' instead of 'present'.
  • C.The backup parameter is not set, causing file corruption.
  • D.The insertafter parameter is incorrectly set to EOF, causing multiple adds.

Why A: Option A is correct because the lineinfile module without a regexp parameter cannot identify an existing line to match, so it adds the line every time the playbook runs, causing duplicates. Option B is incorrect because 'append' is not a valid state for lineinfile; valid states are 'present' (default) and 'absent'. Option C is incorrect because the backup parameter only creates a backup file and does not affect line duplication. Option D is incorrect because insertafter=EOF would place the line at the end of the file, but without a regexp, the module still cannot detect an existing matching line and will add it each run.

Keep practising

More EX294 practice questions

Last reviewed: Jun 11, 2026

Question Discussion

Share a tip, memory trick, or ask about the reasoning behind this question. Do not post real exam questions, leaked content, braindumps, or copyrighted exam material. Comments are moderated and may be removed without notice.

Loading comments…

Sign in to join the discussion.

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.