Top Ansible Interview Questions and Answers (2024)
In this post, questions from Ansible Interviews will be answered for Experienced and Freshers. We're trying to share our experience and learn how to help you make progress in your career.
What is Ansible?
What are the benefits of Ansible?
What is Ansible module?
Explain how Ansible works and its workflow?
What is an Ansible Playbook?
How to create a module in Ansible?
How do you locally verify your ansible module code?
How do you verify your module code in a playbook?
How to perform Ansible's sanity checks in a container?
How to add unit tests for your module in Ansible?
What is module utilities in Ansible?
What is Plugins in Ansible?
What is inventory in Ansible?
Q: What is Ansible?
Ans:
Ansible is an open source tool for IT Configuration Management, Deployment, and Orchestration. The goal is to provide significant productivity gains to a wide range of automation challenges. This tool is extremely easy to use while also being powerful enough to automate complex multi-tier IT application environments.
Q: What are the benefits of Ansible?
Ans:
- Ansible is an open source tool.
- Ansible is very reliable and lightweight, with no restrictions imposed by the operating system or underlying hardware.
- Because of its agentless capabilities and open SSH security features, it is extremely secure.
- Ansible requires no special system administrator expertise to instal or use.
- Ansible has a smooth learning curve due to its extensive documentation and simple structure and configuration.
- Ansible's modularity in terms of plugins, inventories, modules, and playbooks makes it an ideal companion for orchestrating large environments.
Q: What is Ansible module?
Ans:
An Ansible module is a reusable, standalone script that Ansible executes on your behalf, either locally or remotely. Modules communicate to your local machine, an API, or a remote system to carry out the activities such as changing a database password or launching a cloud instance.
Q: Explain how Ansible works and its workflow?
Ans:
The Management Node is the controlling node that supervises playbook execution. The inventory file contains a list of hosts on which the Ansible modules are always run. The Management Node establishes an SSH connection and executes the small modules on the host's machine, as well as installing the software.
Ansible removes the modules after they have been expertly installed. It tries to connect to the host machine, executes the instructions, and if successful, removes the code that was copied on the host machine.
Q: What is an Ansible Playbook?
Ans:
An Ansible playbook is a logical collection of scripts that describes the work for a server configuration handled by the Ansible automation tool. Ansible is a configuration management solution that uses Ansible playbooks to automate the configuration of multiple servers.
Playbooks can finely orchestrate multiple slices of your infrastructure topology, giving you complete control over how many servers to tackle at any given time.
This is how the simple playbook looks.
---
- hosts: webservers
serial: 5 # update 5 machines at a time
roles:
- common
- webapp
- hosts: content_servers
roles:
- common
- content
Checkout our related posts :
Q: How to create a module in Ansible?
Ans:
To create a module, do the following:
- Navigate to the appropriate directory for your new module:
$ cd lib/ansible/modules/
. If you're working on a collection module, navigate to $ cd plugins/modules/ within the collection development tree. - Create a new module file with
$ touch my_test_techgeeknext_example.py
. - Copy and paste the code into your new module file. Follow correct Ansible format.
- For pointers on writing clean and concise module code, see the programming tips and 3 compatibility pages.
#!/usr/bin/python
DOCUMENTATION = r'''
---
module: my_test_techgeeknext_example
short_description: Example for creating test module
# In case of part of a collection, use semantic versioning,
version_added: "1.0.0"
...........
...........
def run_module():
..............
..............
def main():
run_module()
if __name__ == '__main__':
main()
Q: How do you locally verify your ansible module code?
Ans:
Sometimes when your module does not have to reach a remote host, you can efficiently and simply test it locally, as shown below:
- Create an arguments file, which is a simple JSON configuration file that passes parameters to your
module so that it can be run.
/tmp/args.json
is the name of the arguments file, and it contains the following content: - When you're using a virtual environment (which is strongly recommended for development), make the
following changes:
$ . venv/bin/activate
- Set up a development environment:
$ . hacking/env-setup
- Locally and directly run your test module:
$ python -m ansible.modules.my_test_techgeeknext_example /tmp/args.json
This should produce the following output:
{
"ANSIBLE_MODULE_ARGS": {
"name": "hello techgeeknext user!",
"new": true
}
}
{"changed": true, "state": {"original_message": "hello techgeeknext user!", "new_message": "Have nice day!"}, "invocation": {"module_args": {"name": "hello techgeeknext user!", "new": true}}}
Q: How do you verify your module code in a playbook?
Ans:
The following step in verifying your new module is to consume it with an Ansible playbook.
- In any directory, create a playbook:
$ touch testmod.yml
- To the new playbook file, add the following:
- Run the playbook and examine the results:
$ ansible-playbook ./testmod.yml
- name: test my new module
hosts: localhost
tasks:
- name: run the new module
my_test_techgeeknext_example:
name: 'hello techgeeknext user'
new: true
register: testout
- name: example to test output
debug:
msg: '{{ testout }}'
Q: How to perform Ansible's sanity checks in a container?
Ans:
You can perform in the following Ansible sanity checks in container:
You would require docker to run this test, install docker from here.
$ ansible-test sanity -v --docker --python 2.7 MODULE_NAME
Q: How to add unit tests for your module in Ansible?
Ans:
./test/units/modules
. First, you should set up
your testing environment.
$ pip3 install -r ./test/lib/ansible_test/_data/requirements/units.txt
. hacking/env-setup
$ ansible-test units --python 3.5
$ pytest -r a --cov=. --cov-report=html --fulltrace --color yes test/units/modules/.../test/my_test_techgeeknext_example.py
Q: What is module utilities in Ansible?
Ans:
If multiple modules are using the same code, Ansible stores those functions as module utilities. This
reduces duplication and maintenance. Such as the code that parses URLs is lib/ansible/module_utils/url.p
.
You can also create your own module utilities. Only Python or PowerShell can be used to create module
utilities.
Q: What is Plugins in Ansible?
Ans:
Ansible's fundamental functionality is enhanced by plugins, which are small pieces of code. To provide a
comprehensive, adaptable, and expandable feature set, Ansible utilizes a plugin architecture.
Ansible is pre-loaded with a lot of useful plugins, and you can easily create your own.
Ansible comes with several different sorts of plugins:
- Action Plugins
- Become Plugins
- Cache Plugins
- Callback Plugins
- Cliconf Plugins
- Connection Plugins
- Httpapi Plugins
- Inventory Plugins
- Lookup Plugins
- Netconf Plugins
- Shell Plugins
- Strategy Plugins
- Vars Plugins
- Using filters to manipulate data
- Tests
- Rejecting modules
Q: What is inventory in Ansible?
Ans:
The Ansible inventory file specifies the hosts and groups of hosts on which commands, modules, and tasks in a playbook run. The file could be in one of many formats based on your Ansible environment and plugins.
A plain text inventory file looks like this:
---
[webservers]
www1.techgeeknext.com
www2.techgeeknext.com
[dbservers]
db0.techgeeknext.com
db1.techgeeknext.com
If inventory hosts have been listed, variables could be assigned to them in simple text files (in a
subdirectory known as 'group_vars/'
or 'host_vars/'
or directly in the inventory
file.
Or, you can use a dynamic inventory to pull your inventory from data sources such as EC2, Rackspace, or
OpenStack.