Ansible for Infrastructure Automation Quiz Quiz

Challenge your understanding of Ansible fundamentals for infrastructure automation, including YAML syntax, modules, playbooks, and key configuration concepts. Enhance your expertise in automated IT orchestration by answering scenario-based questions designed for professionals and enthusiasts.

  1. Understanding Ansible YAML Syntax

    Which of the following YAML snippets correctly defines a simple Ansible task that installs the 'vim' package on all managed hosts?

    1. - install: vimn package: vim present
    2. - task: Install vimn module: apt name=vim state=latest
    3. - name: Install vimn apt:n name: vimn state: present
    4. - name: Install vimn package: name=vim state=present

    Explanation: The correct YAML snippet uses the 'name' and 'apt' module with properly nested parameters, which aligns with Ansible's expected syntax for tasks. The first option uses an incorrect key ('package' instead of 'apt') and merges parameters improperly. The second uses non-existent keys 'task' and 'module'. The fourth has invalid keys and structure. Correct indentation and module usage are crucial in Ansible task definitions.

  2. Selecting the Appropriate Ansible Module

    You need to ensure a specific line exists in a configuration file across multiple servers. Which Ansible module is most suitable for this task?

    1. shellcmd
    2. blockinfile
    3. lineinfile
    4. copyfile

    Explanation: The 'lineinfile' module is specifically designed to add, modify, or remove individual lines in files, making it ideal for ensuring the presence of a particular line. 'copyfile' is not an actual module and would not work. 'shellcmd' is invalid and does not exist. 'blockinfile' is used for inserting or removing blocks of text, not individual lines.

  3. Ansible Inventory Syntax

    When organizing hosts into groups within an Ansible inventory file, which of these group definitions is valid?

    1. webservers: {server1, server2}
    2. [webservers]nserver1nserver2
    3. [webservers]n- server1n- server2
    4. group webservers:n- server1n- server2

    Explanation: Placing the group name in brackets and listing hostnames underneath, as in the correct answer, matches the standard inventory file format. The second and fourth options use YAML-style or bullet-point notation, which is invalid for standard INI-style inventories. The third uses curly braces and comma separation, which is not supported syntax.

  4. Understanding Ansible Playbook Execution Order

    If an Ansible playbook lists three tasks under 'tasks:', in which order will the tasks be run on the specified hosts?

    1. Tasks are executed sequentially in the order listed
    2. Tasks are all executed in parallel
    3. Tasks are executed randomly on each run
    4. Tasks are executed in reverse order

    Explanation: Ansible processes tasks in the sequence they appear in the playbook, ensuring predictable execution. Random or parallel execution does not occur at the individual task level, even though multiple hosts can be acted on in parallel. Reverse order is not supported unless explicitly instructed in the playbook logic.

  5. Managing Variables in Ansible

    Which file would you use to define group-specific variables for a group called 'dbservers' in a typical Ansible project directory?

    1. host_vars/dbservers.yml
    2. vars_main/dbservers.yml
    3. group_vars/dbservers.yml
    4. inventory_vars/dbserver

    Explanation: By convention, variables for a host group are placed in 'group_vars/groupname.yml' or a similarly named file located within the 'group_vars' directory. 'host_vars/dbservers.yml' is meant for individual hosts, not groups. 'vars_main/dbservers.yml' and 'inventory_vars/dbserver' are not standard directory or file names in Ansible's variable precedence structure.