Ansible Interview Questions and Answers
Freshers / Beginner level questions & answers
Ques 1. What is Ansible?
Ansible is an open-source automation tool used for configuration management, application deployment, task automation, and orchestration.
Ques 2. What is an Ansible Playbook?
An Ansible Playbook is a YAML file that defines a set of tasks to be executed on remote hosts. Playbooks are used for automation and orchestration.
Ques 3. How can you define variables in Ansible?
Variables in Ansible can be defined in playbooks, inventory files, or external variable files. They are used to make playbooks more dynamic.
Ques 4. How can you run Ansible playbooks against multiple inventories?
You can use the '-i' option followed by the path to the inventory file when running the 'ansible-playbook' command.
Example:
ansible-playbook -i production_inventory.ini site.yml
Ques 5. What is the Ansible Galaxy?
Ansible Galaxy is a platform for sharing, discovering, and installing Ansible content, including roles, collections, and playbooks.
Ques 6. What is the purpose of the 'become' keyword in Ansible?
The 'become' keyword is used to execute tasks with escalated privileges, usually by using 'sudo' or 'su'. It is equivalent to 'sudo' in Unix-like systems.
Example:
become: yes
Ques 7. How can you execute only a specific task from an Ansible playbook?
You can use the '--tags' option followed by the task tag when running the 'ansible-playbook' command to execute specific tasks.
Example:
ansible-playbook site.yml --tags deploy
Ques 8. How can you check the syntax of an Ansible playbook without executing it?
You can use the 'ansible-playbook' command with the '--syntax-check' option to validate the syntax of a playbook without running it.
Example:
ansible-playbook site.yml --syntax-check
Ques 9. How can you limit the execution of Ansible tasks to a specific set of hosts?
You can use the '--limit' option followed by the pattern of hosts when running the 'ansible-playbook' command to limit the execution to specific hosts.
Example:
ansible-playbook site.yml --limit web_servers
Ques 10. How can you use loops in Ansible playbooks?
Ansible supports loops using the 'with_items' keyword. It allows tasks to be repeated for each item in a list.
Example:
with_items:
- item1
- item2
Ques 11. How can you include external files in Ansible playbooks?
The 'include' statement is used to include external YAML files in Ansible playbooks. It allows for better organization and reuse of playbook components.
Example:
include: tasks/common-tasks.yml
Ques 12. How can you define and use roles in Ansible playbooks?
Roles are defined in separate directories and can be included in playbooks using the 'roles' keyword. They enhance playbook organization and reusability.
Example:
roles:
- common
- web_server
Ques 13. How can you check if a file or directory exists in Ansible?
The 'stat' module in Ansible can be used to check the existence of a file or directory. The result can be checked using the 'stat.exists' attribute.
Example:
stat:
path: /path/to/file.txt
Ques 14. How can you override variables in Ansible?
Variables in Ansible can be overridden at different levels, including playbooks, inventory files, and the command line. The precedence order determines which value is used.
Example:
ansible-playbook site.yml -e 'variable_name=new_value'
Most helpful rated by users: