Essential Ansible Playbook Writing u0026 Best Practices Quiz Quiz

Explore the fundamentals of writing Ansible playbooks and applying best practices to ensure automation reliability, readability, and efficiency. Sharpen your automation skills by identifying correct syntax, structure, indentation, and recommended strategies for creating effective Ansible playbooks.

  1. Understanding Playbook Structure

    Which element should always appear at the very beginning of an Ansible playbook to indicate its format and compatibility?

    1. vars:
    2. - name:
    3. hosts:
    4. ---

    Explanation: The triple dash --- is the YAML document start indicator and should appear at the beginning of every Ansible playbook for clarity. 'hosts:' and 'vars:' are important keys within playbooks but are not required to start the file. '- name:' is used to label specific tasks or plays inside the playbook, not at the top level. Omitting the YAML start marker can sometimes cause ambiguity in parsers, so it's considered best practice to include it.

  2. Defining Play Targets

    In an Ansible playbook, which key is used to specify the group of hosts on which the tasks should be run?

    1. grouped:
    2. tasks:
    3. roles:
    4. hosts:

    Explanation: The 'hosts:' key defines which group of hosts the playbook actions should target. 'roles:' lists reusable roles to include, and 'tasks:' is where individual actions are listed. 'grouped:' is not a valid playbook key. Without specifying 'hosts:', Ansible will not know where to execute the tasks.

  3. Indentation Rules

    What is the required indentation style for YAML files, including Ansible playbooks?

    1. Mix of tabs and spaces
    2. Consistent spaces only, no tabs allowed
    3. No indentation required
    4. Tabs only for indentation

    Explanation: YAML strictly requires using spaces for indentation and forbids tabs, as mixing the two can lead to parsing errors. Using tabs or mixing tabs and spaces will break YAML parsing and lead to difficult-to-trace bugs. No indentation would result in a flat structure, which is incorrect for playbooks needing nested content.

  4. Using Variables Effectively

    Where is it recommended to place variables that will be reused by multiple tasks within an Ansible playbook?

    1. vars:
    2. env:
    3. become:
    4. task_vars:

    Explanation: The 'vars:' section in a playbook is intended for defining variables accessible to all related tasks. 'become:' is used for privilege escalation and does not store variables. 'task_vars:' and 'env:' are not standard YAML sections in playbooks; environment variables use 'environment:', and variable scoping is managed with standard keys like 'vars:'. Placing variables in 'vars:' improves playbook maintainability and clarity.

  5. Best Practices for Playbook Length

    When a playbook becomes long and complex, what is the best practice to maintain clarity and reusability?

    1. Split tasks into roles and include them in the playbook
    2. Remove comments to make the file shorter
    3. Rename the playbook for each new function
    4. Write everything in a single task for readability

    Explanation: Splitting tasks into reusable roles keeps playbooks modular, organized, and easier to maintain. Writing everything in a single task or removing comments reduces clarity. Renaming the playbook doesn't solve the problem of complexity. Using roles encourages reusability and better management as projects grow.

  6. Playbook Task Order

    If you have multiple tasks in an Ansible playbook, in which order are they executed on the target hosts?

    1. Bottom to top
    2. Top to bottom, as listed in the playbook
    3. Randomly
    4. In alphabetical order

    Explanation: Ansible executes tasks sequentially from the first to the last, maintaining the order specified. There's no random, reverse, or alphabetical execution for tasks. Maintaining logical and meaningful order is part of playbook best practices.

  7. Task Naming Clarity

    Which practice helps make it easier to understand what each task in your playbook does at a glance?

    1. Leave name fields blank to save space
    2. Use descriptive 'name' fields for tasks
    3. Abbreviate all 'name' fields
    4. Repeat the same name for each task

    Explanation: Descriptive 'name' fields make playbooks self-documenting and easier to troubleshoot. Leaving 'name' fields blank, abbreviating them, or repeating the same name diminishes the usefulness of task logs and makes maintenance harder. Clarity in task names is a recommended best practice for collaborative environments.

  8. Recommended File Extension

    What file extension is recommended for Ansible playbook files?

    1. .yml
    2. .xml
    3. .ans
    4. .ini

    Explanation: .yml is the standard and recommended file extension for Ansible playbooks as they are written in YAML. '.xml' and '.ini' are used for completely different formats, and '.ans' is not a recognized extension. Using '.yml' helps editors and tools recognize and process the playbook correctly.

  9. Idempotence in Playbooks

    What does it mean for an Ansible playbook to be idempotent?

    1. It always produces a different outcome with each run
    2. It ignores errors by default
    3. It only runs on one host at a time
    4. It can be run multiple times without changing the result after the first successful run

    Explanation: Idempotence means the actions result in the same system state regardless of how many times the playbook is run after the first successful application. Running only on one host or ignoring errors is unrelated, and always getting different outcomes is the opposite of idempotence. Achieving idempotence is a primary goal in reliable automation.

  10. Securing Sensitive Data

    Which Ansible feature is designed for encrypting sensitive information such as passwords in playbooks or variable files?

    1. Vault
    2. Hidden
    3. Cloak
    4. Shield

    Explanation: Ansible's Vault feature allows you to encrypt sensitive data within your projects. 'Cloak', 'Shield', and 'Hidden' sound plausible but are not actual Ansible features. Using Vault protects confidential values such as credentials from accidental disclosure in source control or logs.