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.
Which of the following YAML snippets correctly defines a simple Ansible task that installs the 'vim' package on all managed hosts?
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.
You need to ensure a specific line exists in a configuration file across multiple servers. Which Ansible module is most suitable for this task?
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.
When organizing hosts into groups within an Ansible inventory file, which of these group definitions is valid?
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.
If an Ansible playbook lists three tasks under 'tasks:', in which order will the tasks be run on the specified hosts?
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.
Which file would you use to define group-specific variables for a group called 'dbservers' in a typical Ansible project directory?
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.