Ansible Automation Ansible Roles & Galaxy 1 — Questions and Answers
Question 1: What command creates a new role scaffold with the standard directory structure?
- ansible-galaxy role init myrole (Correct answer)
- ansible role create myrole
- ansible-playbook --create-role myrole
- ansible-galaxy create myrole
Correct answer: ansible-galaxy role init myrole
ansible-galaxy role init <name> generates a standardized role directory with tasks, handlers, defaults, vars, files, templates, and meta folders.
Question 2: In an Ansible role, which directory holds the default variable values that can be easily overridden?
- defaults/ (Correct answer)
- vars/
- variables/
- params/
Correct answer: defaults/
Variables in defaults/main.yml have the lowest precedence and are designed to be overridden by users of the role.
Question 3: Which role directory is used to store Jinja2 template files?
- templates/ (Correct answer)
- files/
- static/
- views/
Correct answer: templates/
The templates/ directory holds .j2 Jinja2 template files that are rendered by the template module during role execution.
Question 4: Which file in a role defines the role's metadata including dependencies?
- meta/main.yml (Correct answer)
- defaults/main.yml
- vars/main.yml
- role.yml
Correct answer: meta/main.yml
meta/main.yml contains role metadata such as author, license, supported platforms, and role dependencies.
Question 5: How do you call a role inside a playbook using the modern recommended syntax?
- roles: [myrole] (Correct answer)
- include_role: name=myrole
- use_role: myrole
- apply_role: myrole
Correct answer: roles: [myrole]
The roles: key in a play statically imports roles before any tasks run; it is the standard way to apply roles in a playbook.
Question 6: Which directory in a role stores static files that can be transferred to managed hosts unchanged?
- files/ (Correct answer)
- templates/
- static/
- assets/
Correct answer: files/
The files/ directory contains static files copied to managed hosts using the copy or synchronize modules without any Jinja2 processing.
What command creates a new role scaffold with the standard directory structure?