Ansible Interview Questions and Answers
Q1. Difference Between Terraform and Ansible?
![](https://www.bestdevops.com/wp-content/uploads/2024/06/image-1024x277.png)
Q2. What Kind of Work you did in Ansible?
![](https://www.bestdevops.com/wp-content/uploads/2024/06/image-1-1024x258.png)
Q3. What Kind of Playbook you have written in Ansible?
- Playbook for Patching & Upgrading Linux APP Servers
- Playbook for Patching & Upgrading Linux DB Servers
- Playbook for Installing Applications in Linux VMS
- Playbook for Installing Database in Linux VMS
- Playbook for Installing Web Servers in Linux VMS
![](https://www.bestdevops.com/wp-content/uploads/2024/06/image-9.png)
![](https://www.bestdevops.com/wp-content/uploads/2024/06/image-2-1024x597.png)
Q4. What is Architecture of Ansible or How it works?
![](https://www.bestdevops.com/wp-content/uploads/2024/06/image-3-1024x623.png)
Q5. Whats there in Playbooks? How you write playbook?
![](https://www.bestdevops.com/wp-content/uploads/2024/06/image-4.png)
Q6. Example of Plybook of Ubuntu Servers
---
- name: Update web servers
hosts: web
tasks:
- name: Install Apache in ubuntu
ansible.builtin.apt:
name: "apache2"
state: latest
- name: Copy index.html
ansible.builtin.copy:
src: index.html
dest: /var/www/html/index.html
- name: Starting a Apache Server
ansible.builtin.service:
name: "apache2"
state: started
---
- name: Update web servers
hosts: web
vars:
myname: "Rajesh Kumar"
httpport: 8090
tasks:
- name: Install Apache in ubuntu
ansible.builtin.apt:
name: apache2
state: latest
- name: Copy index.html
ansible.builtin.copy:
src: index.html
dest: /var/www/html/index.html
- name: Starting a Apache Server
ansible.builtin.service:
name: apache2
state: started
- name: Template for httpd.conf
template:
src: ports.conf.j2
dest: /etc/apache2/ports.conf
notify:
- ReStarting a Apache Server
handlers:
- name: ReStarting a Apache Server
ansible.builtin.service:
name: apache2
state: restarted
Q7. List of TOP Ansible modules you have used in Playbook
Certainly! Here is a list of commonly used Ansible modules in Playbook format, presented in a table without examples:
![](https://www.bestdevops.com/wp-content/uploads/2024/06/image-7.png)
- name: Install a package
apt:
name: git
state: present
- name: Ensure nginx is running
service:
name: nginx
state: started
enabled: true
- name: Copy a file to the remote server
copy:
src: /local/path/to/file
dest: /remote/path/to/file
- name: Deploy a configuration file
template:
src: /local/path/to/template.j2
dest: /remote/path/to/config.conf
- name: Ensure a directory exists
file:
path: /path/to/directory
state: directory
mode: '0755'
- name: Create a user
user:
name: johndoe
state: present
groups: sudo
- name: Run a custom shell command
shell: echo "This is a test"
- name: Run a simple command
command: /usr/bin/uptime
- name: Clone a Git repository
git:
repo: 'https://github.com/example/repo.git'
dest: /path/to/destination
- name: Ensure a line is present in a file
lineinfile:
path: /path/to/file
line: 'This is a line in the file'
Q8. What is ROLE?
![](https://www.bestdevops.com/wp-content/uploads/2024/06/image-5-1024x392.png)
![](https://www.bestdevops.com/wp-content/uploads/2024/06/image-6.png)
roles/
├── common
│ ├── defaults
│ │ └── main.yml # Default variables for the role
│ ├── files
│ │ └── ... # Files to be copied to remote hosts
│ ├── handlers
│ │ └── main.yml # Handlers, triggered by tasks
│ ├── meta
│ │ └── main.yml # Role dependencies and metadata
│ ├── tasks
│ │ └── main.yml # Main list of tasks to execute
│ ├── templates
│ │ └── ... # Jinja2 templates to be deployed
│ ├── tests
│ │ ├── inventory # Test inventory file for Vagrant
│ │ └── test.yml # Test playbook
│ └── vars
│ └── main.yml # Variables for the role
├── webserver
│ ├── defaults
│ │ └── main.yml # Default variables for the role
│ ├── files
│ │ └── ... # Files to be copied to remote hosts
│ ├── handlers
│ │ └── main.yml # Handlers, triggered by tasks
│ ├── meta
│ │ └── main.yml # Role dependencies and metadata
│ ├── tasks
│ │ └── main.yml # Main list of tasks to execute
│ ├── templates
│ │ └── ... # Jinja2 templates to be deployed
│ ├── tests
│ │ ├── inventory # Test inventory file for Vagrant
│ │ └── test.yml # Test playbook
│ └── vars
│ └── main.yml # Variables for the role
└── database
├── defaults
│ └── main.yml # Default variables for the role
├── files
│ └── ... # Files to be copied to remote hosts
├── handlers
│ └── main.yml # Handlers, triggered by tasks
├── meta
│ └── main.yml # Role dependencies and metadata
├── tasks
│ └── main.yml # Main list of tasks to execute
├── templates
│ └── ... # Jinja2 templates to be deployed
├── tests
│ ├── inventory # Test inventory file for Vagrant
│ └── test.yml # Test playbook
└── vars
└── main.yml # Variables for the role
Q9. What are top 10 Roles you have used in your work?
![](https://www.bestdevops.com/wp-content/uploads/2024/06/image-8.png)
[…] https://www.bestdevops.com/ansible-interview-questions-and-answers […]