Ansible Playbooks and Roles: Building Blocks for Automation Quiz

Assess your understanding of writing and managing Ansible playbooks and roles, including proper structure, syntax, variables, and role use in cloud and DevOps environments. Gain valuable insights into key best practices for effective configuration management and automation workflows.

  1. Understanding Playbook Basics

    Which file extension is typically used for an Ansible playbook file, such as one named 'deploy_app'?

    1. .sh
    2. .json
    3. .txt
    4. .yaml

    Explanation: Ansible playbooks are usually written in YAML and use the '.yaml' extension, making them both human-readable and machine-parseable. '.txt' is for plain text files and doesn't convey any structure. '.json' is not standard for Ansible playbooks, even though it's a data format. '.sh' is used for shell scripts and is not suitable for Ansible playbooks.

  2. Playbook Structure

    What is the main key at the top level of an Ansible playbook that defines on which hosts the tasks will run?

    1. target
    2. nodes
    3. hosts
    4. machines

    Explanation: The 'hosts' key is required to specify the group or list of machines where the play's tasks run. 'target', 'machines', and 'nodes' are not valid playbook keys in Ansible; using them would cause errors or be ignored.

  3. Variable Usage

    If you want to reference a variable named 'username' in an Ansible playbook, which syntax should you use?

    1. $username$
    2. u003C% username %u003E
    3. {{ username }}
    4. [[ username ]]

    Explanation: The correct syntax for referencing variables in Ansible is double curly braces, like '{{ username }}'. 'u003C% username %u003E' is not valid in Ansible; it's sometimes used in other templating systems. '$username$' and '[[ username ]]' are also incorrect in the context of Ansible.

  4. Role Directory Structure

    In a standard Ansible role structure, in which directory should the main list of tasks be placed?

    1. vars/
    2. tasks/
    3. handlers/
    4. defaults/

    Explanation: The 'tasks/' directory holds the main list of tasks for the role, usually in a file named 'main.yaml'. 'vars/' is for setting custom variables, 'handlers/' for notifying actions after a change, and 'defaults/' for default variable values.

  5. Including Roles

    How do you include a role named 'webserver' in a playbook for application deployment?

    1. roles: - webserver
    2. include_role: webserver
    3. import_role: webserver
    4. play_role: webserver

    Explanation: Roles are commonly included in playbooks using the 'roles' keyword followed by a list, such as 'roles: - webserver'. 'include_role' and 'import_role' are used within tasks, not at the playbook's top level. 'play_role' is not a recognized keyword in Ansible.

  6. YAML Syntax Error Detection

    Given the following snippet in an Ansible playbook, which line will cause a syntax error?n- name: Install packagesn apt:n name: nginxn state present

    1. state present
    2. name: nginx
    3. apt:
    4. - name: Install packages

    Explanation: 'state present' is missing a colon, so it should be 'state: present' to conform with YAML syntax. The other lines 'name: nginx', '- name: Install packages', and 'apt:' are formatted correctly and do not cause syntax errors.

  7. Handlers Purpose

    In Ansible, what is the main function of a handler, for example, one that restarts a service after files change?

    1. To manage inventories
    2. To define default variable values
    3. To perform actions only when notified by a task
    4. To list the order of tasks explicitly

    Explanation: Handlers are special tasks executed only when notified by other tasks, which is useful for actions like restarting services upon changes. They do not define variables ('default variable values'), list task order, or manage inventories.

  8. Role Variables Priority

    If a variable is defined both in the 'defaults/' and 'vars/' directory of an Ansible role, which value will be used?

    1. The value in 'defaults/'
    2. The value in 'vars/'
    3. The value from an included task
    4. The value set by the role dependency

    Explanation: Ansible gives a higher priority to variables defined in the 'vars/' directory over those in 'defaults/'. Variables in 'defaults/' have the lowest precedence. Role dependencies and included tasks have their own variable scoping and do not override 'vars/' in this context.

  9. Task Idempotency

    What does it mean for Ansible tasks to be idempotent when managing infrastructure, such as creating a user account?

    1. Running multiple times does not change the result after the first successful run
    2. Every run duplicates the changes
    3. Tasks always require manual intervention
    4. Only one task can run at a time

    Explanation: Idempotency means that after the initial change, repeating the task produces no additional effect. This ensures predictable configuration. Running one task at a time, requiring manual intervention, or duplicating changes are not characteristics of idempotency.

  10. Playbook Tags Usage

    If you want to run only certain tasks in a playbook, such as installing packages, which feature should you use?

    1. Loops
    2. Tags
    3. Inventories
    4. Handlers

    Explanation: Tags allow users to select specific tasks to run or skip by marking them, making targeted automation possible. Handlers are for event-driven actions after change, inventories manage groups of hosts, and loops are for repeating actions multiple times.