Top Ansible Interview Questions (2024) | TechGeekNext

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.

  1. What is Ansible?
  2. What are the benefits of Ansible?
  3. What is Ansible module?
  4. Explain how Ansible works and its workflow?
  5. What is an Ansible Playbook?
  6. How to create a module in Ansible?
  7. How do you locally verify your ansible module code?
  8. How do you verify your module code in a playbook?
  9. How to perform Ansible's sanity checks in a container?
  10. How to add unit tests for your module in Ansible?
  11. What is module utilities in Ansible?
  12. What is Plugins in Ansible?
  13. What is inventory in Ansible?

Q: What is Ansible?

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.

Ansible workflow

Q: What are the benefits of Ansible?

  1. Ansible is an open source tool.
  2. Ansible is very reliable and lightweight, with no restrictions imposed by the operating system or underlying hardware.
  3. Because of its agentless capabilities and open SSH security features, it is extremely secure.
  4. Ansible requires no special system administrator expertise to instal or use.
  5. Ansible has a smooth learning curve due to its extensive documentation and simple structure and configuration.
  6. 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?

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?

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?

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
  - common
  - webapp

- hosts: content_servers
  - common
  - content

Checkout our related posts :

Q: How to create a module in Ansible?

To create a module, do the following:

  1. 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.
  2. Create a new module file with $ touch
  3. Copy and paste the code into your new module file. Follow correct Ansible format.
  4. #!/usr/bin/python
    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():
    if __name__ == '__main__':
  5. For pointers on writing clean and concise module code, see the programming tips and 3 compatibility pages.

Q: How do you locally verify your ansible module code?

Sometimes when your module does not have to reach a remote host, you can efficiently and simply test it locally, as shown below:

  1. 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:
  2. {
            "name": "hello techgeeknext user!",
            "new": true
  3. When you're using a virtual environment (which is strongly recommended for development), make the following changes: $ . venv/bin/activate
  4. Set up a development environment:$ . hacking/env-setup
  5. Locally and directly run your test module:$ python -m ansible.modules.my_test_techgeeknext_example /tmp/args.json This should produce the following output:
  6. {"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?

The following step in verifying your new module is to consume it with an Ansible playbook.

  1. In any directory, create a playbook:$ touch testmod.yml
  2. To the new playbook file, add the following:
  3. - name: test my new module
      hosts: localhost
      - name: run the new module
          name: 'hello techgeeknext user'
          new: true
        register: testout
      - name: example to test output
          msg: '{{ testout }}'
  4. Run the playbook and examine the results:$ ansible-playbook ./testmod.yml

Q: How to perform Ansible's sanity checks in a container?

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?

  • Unit tests for your module can be added in ./test/units/modules. First, you should set up your testing environment.
  • Install the following requirements (aside from your virtual environment):
  • $ pip3 install -r ./test/lib/ansible_test/_data/requirements/units.txt
  • Execute below command.
  • . hacking/env-setup
  • To run all tests, perform the following steps:
  • $ ansible-test units --python 3.5
  • These tests will be run automatically if you are using a CI environment.
  • You could use the following command to run pytest against a single test module. Check that you are providing the correct test module path:
  • $ pytest -r a --cov=. --cov-report=html --fulltrace --color yes test/units/modules/.../test/

    Q: What is module utilities in Ansible?

    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?

    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:

    1. Action Plugins
    2. Become Plugins
    3. Cache Plugins
    4. Callback Plugins
    5. Cliconf Plugins
    6. Connection Plugins
    7. Httpapi Plugins
    8. Inventory Plugins
    9. Lookup Plugins
    10. Netconf Plugins
    11. Shell Plugins
    12. Strategy Plugins
    13. Vars Plugins
    14. Using filters to manipulate data
    15. Tests
    16. Rejecting modules

    Q: What is inventory in Ansible?

    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:

    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.

    Recommendation for Top Popular Post :