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.
Which file extension is typically used for an Ansible playbook file, such as one named 'deploy_app'?
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.
What is the main key at the top level of an Ansible playbook that defines on which hosts the tasks will run?
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.
If you want to reference a variable named 'username' in an Ansible playbook, which syntax should you use?
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.
In a standard Ansible role structure, in which directory should the main list of tasks be placed?
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.
How do you include a role named 'webserver' in a playbook for application deployment?
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.
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
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.
In Ansible, what is the main function of a handler, for example, one that restarts a service after files change?
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.
If a variable is defined both in the 'defaults/' and 'vars/' directory of an Ansible role, which value will be used?
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.
What does it mean for Ansible tasks to be idempotent when managing infrastructure, such as creating a user account?
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.
If you want to run only certain tasks in a playbook, such as installing packages, which feature should you use?
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.