Ansible Interview Q&A

1. What is the Ansible and why is it used?

Ansible is an open-source automation tool for configuration management, application deployment, provisioning, and orchestration. It is agentless and communicates with nodes over SSH. It simplifies repetitive tasks and ensures consistency across servers.

2. Explain the architecture of Ansible.

Ansible has the following components: - Controller: Installed with Ansible, runs playbooks. - Managed Nodes: Target systems where tasks are executed. - Inventory: Hosts list. - Modules: Predefined task executors. - Playbooks: YAML-based automation instructions. - Roles: Organized reusable structures of playbooks, tasks, and handlers.

3. What is an Ansible inventory?

An inventory is a file containing a list of target machines that Ansible will manage. It can be static (defined in a file like /etc/ansible/hosts) or dynamic (fetched from cloud APIs such as AWS, GCP, or Azure).

4. What is a playbook in Ansible?

A playbook is a YAML file that defines a series of tasks to run on managed nodes. Playbooks support variables, loops, conditionals, handlers, roles, and templates for automation.

5. Give an example of a simple playbook.

Example: --- - name: Ping worker nodes hosts: all tasks: - name: Ping all nodes ping: {}

6. What are Ansible modules?

Modules are small programs that Ansible sends to target machines for execution. Examples: yum (install packages), apt, service, command, shell, copy, file, git, archive, debug, etc.

7. What is an Ansible role?

A role is a way to structure playbooks into reusable components. It contains directories like tasks/, handlers/, files/, templates/, vars/, defaults/, and meta/.

8. What is the standard directory structure of an Ansible role?

Example structure: lamp/ ├── tasks/ ├── handlers/ ├── files/ ├── templates/ ├── vars/ ├── defaults/ ├── meta/ └── README.md

9. What are ad-hoc commands in Ansible?

Ad-hoc commands are one-line commands used for quick tasks without writing playbooks. Example: ansible all -m ping.

10. How do you check connectivity between worker nodes?

Run: ansible all -m ping or ansible -i inventory all -m ping.

11. How do you gather facts about a node?

Use the setup module: ansible all -m setup.

12. How do you ping a host with username and password?

Run: ansible all -m ping --user=ansadm --ask-pass.

13. How do you check system uptime?

Run: ansible all -a uptime.

14. How do you check memory usage?

Run: ansible all -a 'free -m'.

15. How do you run commands as root using Ansible?

Use become: ansible all -m shell -a 'cat /etc/passwd' -b -K.

16. How do you execute a command as a different user?

Run: ansible all -m file -a 'path=/tmp/test state=directory mode=0755' -b --become-user=ansadm.

17. How do you create a Linux group?

Run: ansible all -m group -a 'name=developers state=present' -b -K.

18. How do you create a file with specific permissions?

Run: ansible all -m file -a 'path=/home/user/testfile state=touch mode=0755'.

19. How do you change ownership of a file?

Run: ansible all -m file -a 'path=/home/user/testfile owner=weblogic group=weblogic' -b.

20. How do you install a package?

Run: ansible all -m yum -a 'name=httpd state=installed'.

21. How do you start and stop a service?

Start: ansible all -m service -a 'name=httpd state=started enabled=yes' Stop: ansible all -m service -a 'name=httpd state=stopped enabled=no'.

22. What is ansible.cfg and what does it contain?

ansible.cfg is the main configuration file. It contains settings like inventory path, remote user, private key, timeout, roles path, log path, etc.

23. What is Ansible Vault?

Ansible Vault is used to encrypt sensitive data such as passwords and keys. Commands include: ansible-vault encrypt, decrypt, rekey, edit, and view.

24. How do you run only specific tasks in a playbook?

Use tags. Example: --- - name: Install Nginx hosts: web tasks: - name: Install Nginx apt: name: nginx state: present tags: install Run with: ansible-playbook site.yml --tags install

25. Real-time: Your playbook fails due to a missing package. How do you troubleshoot?

Check the playbook logs, ensure correct package name, check repository availability, and validate if package manager (yum/apt) is working on the node.

26. Real-time: How do you ensure idempotency in Ansible?

Use Ansible modules instead of raw commands. For example, use service: instead of shell: systemctl start nginx. Modules check the state and avoid duplicate actions.

27. Real-time: How do you run Ansible against only one host?

Use the -l flag: ansible-playbook site.yml -l hostname.

28. Real-time: How do you test a playbook without making changes?

Run in check mode: ansible-playbook site.yml --check.

29. Real-time: How do you debug variables inside a playbook?

Use the debug module. Example: - debug: msg: 'The value of var1 is {{ var1 }}'

30. Real-time: You need to install multiple packages. How do you do it?

Example task: - name: Install multiple packages yum: name: - httpd - git - wget state: present