Ansible-Interview-Ques
Ansible
1. What is Ansible and what is it used for?
Ansible is an open-source automation tool that allows users to automate various IT tasks, such as configuration management, application deployment, and task orchestration. It uses a declarative language called YAML to define the desired state of a system, and then executes the necessary steps to bring the system into that state.
Ansible works by connecting to remote systems via SSH, WinRM, or other remote protocols, and then running modules on those systems to perform tasks such as installing packages, modifying configuration files, and starting or stopping services. Because it uses a simple, agentless architecture, Ansible is lightweight and easy to use, making it popular among IT teams of all sizes.
Some common use cases for Ansible include:
- Automating server and application configuration management
- Deploying and managing applications and services across multiple servers
- Managing network infrastructure and devices
- Performing software testing and development tasks
- Provisioning and managing cloud infrastructure and services.
Overall, Ansible can help IT teams streamline their workflows, reduce errors, and improve efficiency by automating repetitive and time-consuming tasks.
2. What is the difference between Ansible and other configuration management tools?
There are several configuration management tools available in the market, and while they may share some similarities, there are some key differences between Ansible and other tools like Puppet, Chef, and SaltStack. Here are some of the main differences:
- Agentless Architecture: Ansible uses an agentless architecture, which means it does not require any software to be installed on the target machines. Other tools like Puppet and Chef require agents to be installed on the target machines.
- Simplicity and Ease of Use: Ansible has a relatively low learning curve and is known for its simplicity and ease of use, which makes it accessible to IT teams of all sizes. Other tools like Puppet and Chef can be more complex and require a steeper learning curve.
- Configuration Language: Ansible uses YAML to define system configurations, while other tools use their own configuration languages. Some people find YAML easier to read and write than the languages used by other tools.
- Convergence: Ansible uses a push-based model to apply configurations to target machines, which means that changes are pushed out immediately. Other tools use a pull-based model, where target machines periodically pull configuration changes from a central server.
- Community Support: Ansible has a large and active community of users and contributors, which means there are many resources available for troubleshooting and getting help. Other tools also have active communities, but Ansible's community is particularly large and vibrant.
Overall, Ansible's agentless architecture, simplicity, and ease of use make it a popular choice among IT teams. However, other tools like Puppet, Chef, and SaltStack may be better suited for certain use cases or for organizations with specific requirements.
3. What is an inventory in Ansible?
In Ansible, an inventory is a file that contains a list of target systems that Ansible should manage. The inventory is used to define groups of hosts and assign variables to those groups or individual hosts. It can be a simple text file, an executable script, or a dynamic inventory generated by a third-party tool.
The inventory file typically contains a list of IP addresses or hostnames for the target systems, grouped by logical categories such as web servers, database servers, or development environments. Hosts can be members of multiple groups, allowing for flexible and dynamic configuration.
Here is an example of a simple Ansible inventory file:
[webservers]
web1.example.com
web2.example.com
[databases]
db1.example.com
db2.example.com
[dev]
dev1.example.com
dev2.example.com
[prod:children]
webservers
databases
In this example, there are three groups defined: webservers, databases, and dev. The prod group is a special group that is used to define
child groups (webservers and
databases) and can be used
as a shorthand to refer to all hosts in those groups.
The inventory file can also include variables, which are used to configure the target systems. Variables can be defined at the group or host level, and can be used to set things like usernames, passwords, IP addresses, and other configuration options.
Overall, the inventory is a fundamental component of Ansible, as it allows you to define the scope and targets of your automation tasks, and configure them in a flexible and dynamic way.
4. What is a playbook in Ansible?
In Ansible, a playbook is a YAML file that contains a series of tasks to be executed on target systems defined in the inventory. A playbook is essentially a blueprint for a set of automation tasks, which allows you to define what should happen on each target system, in what order, and with what parameters.
A playbook consists of a series of plays, where each play consists of a list of tasks to be executed on a set of hosts. Each task is a single action to be performed on the target system, such as installing a package, modifying a configuration file, or starting a service.
Here is an example of a simple Ansible playbook:
- name: Install Apache web server
hosts: webservers
become: true
tasks:
- name: Install Apache package
yum:
name: httpd
state: latest
- name: Start Apache service
service:
name: httpd
state: started
In this example, the playbook has one play, which is defined to run on the webservers group of hosts. The become directive indicates that the
tasks should be executed with elevated privileges (using sudo or a similar
mechanism). The tasks themselves consist of two actions: installing the Apache
package using the yum
module, and starting the Apache service using the service module.
Playbooks can be used to automate a wide variety of tasks, from simple package installations to complex multi-tier application deployments. They can also include conditional statements, loops, and other control structures, allowing you to create dynamic and flexible automation workflows.
Overall, playbooks are a powerful tool in Ansible that allow you to define complex automation tasks in a clear and structured way, making it easier to manage and scale your infrastructure.
5. What are the basic concepts defined in Ansible playbook?
In Ansible, a playbook is a file that defines a series of tasks to be executed on a set of target systems. Playbooks consist of several basic concepts, including:
- Plays: A play is a set of tasks that are executed on a specific set of hosts. A playbook can contain one or more plays, and each play can target a different group of hosts.
- Tasks: A task is a single action to be performed on a target system, such as installing a package, copying a file, or starting a service.
- Modules: Modules are reusable units of code that perform specific actions on a target system. Ansible includes a large library of modules for common tasks, such as managing files, packages, services, and users.
- Variables: Variables are used to store data that can be accessed and manipulated by tasks and plays. Variables can be defined at the playbook, play, or task level, and can be set to specific values or dynamically generated using templates or other mechanisms.
- Conditionals: Conditionals are used to control the flow of a playbook based on the results of previous tasks or other factors. Ansible supports a variety of conditional statements, including when, failed_when, and changed_when.
- Handlers: Handlers are special tasks that are triggered by other tasks when certain conditions are met. For example, a handler might be used to restart a service after a configuration file has been modified.
- Roles: Roles are a way to organize playbooks and reuse code across multiple playbooks. A role is a collection of tasks, templates, files, and other resources that can be included in one or more playbooks.
By combining these basic concepts, you can create complex and flexible automation workflows in Ansible, which can be used to manage everything from simple server configurations to large-scale cloud deployments.
6. What is a role in Ansible? How do we create roles in Ansible ?
In Ansible, a role is a way to organize and package a collection of related tasks, variables, files, and templates into a reusable unit of code. Roles provide a convenient way to share and reuse common automation tasks across multiple playbooks and projects.
Roles are defined in a specific directory structure that includes subdirectories for tasks, variables, files, templates, and other resources. The directory structure looks like this:

The tasks directory
contains one or more YAML files that define the tasks to be executed by the
role. The files and templates directories contain any static
files or templates that are required by the role. The vars directory contains any variables
that are used by the role, and the defaults
directory contains default values for those variables.
The meta directory
contains a main.yml file
that defines metadata about the role, such as its name, description, and
dependencies.
To use a role in a playbook, you can include it using the roles directive, like this:

This will execute the tasks defined in the tasks/main.yml file of the my_role directory on the hosts in the my_host_group group.
Roles are a powerful feature of Ansible that allow you to modularize and reuse code across multiple projects, making it easier to manage and scale your automation tasks.
Templates in Ansible are files that use a special syntax to define dynamic content that can be generated based on variables and other data. Templates are typically used to generate configuration files, scripts, or other text files that need to be customized for each target system.
Templates are written in a markup language called Jinja2, which allows you to use variables, loops, conditionals, and other constructs to generate dynamic content. Jinja2 syntax is similar to other template languages like Django templates, but with some Ansible-specific extensions.
To use a template in Ansible, you first create the template file in the templates directory of your role or playbook. Then, you can use the template module to generate the output file based on the template and any variables or other data that you pass in.
For example, here is a simple template file that generates an Apache configuration file:

In this example, the template file includes several variables, including server_name, listen_port, document_root, and log_dir, which will be replaced with
actual values when the template is rendered.
To render this template in an Ansible playbook, you could use the following task:

7. What are the different modules in Ansible? Name a few?
Ansible provides a large number of built-in modules that you can use to perform a wide variety of tasks. These modules are organized into categories based on their functionality, including:
- System modules: These modules provide access to system-level resources such as users, groups, files, directories, and services. Examples of system modules include user, group, file, copy, command, and service.
- Network modules: These modules provide access to network-level resources such as routers, switches, firewalls, and load balancers. Examples of network modules include ios_command, ios_config, nxos_command, nxos_config, net_lag, and bigip_pool.
- Cloud modules: These modules provide access to cloud infrastructure resources such as virtual machines, storage, and networking. Examples of cloud modules include ec2, azure_rm, gcp, digital_ocean, and openstack.
- Database modules: These modules provide access to databases and database-related tasks such as creating and managing database users, executing SQL queries, and managing database backups. Examples of database modules include mysql_db, postgres_db, sqlite3, and oracle.
- Web modules: These modules provide access to web-related resources and tasks such as managing web servers, deploying web applications, and configuring web proxies. Examples of web modules include apache2_module, nginx_module, tomcat, jetty, and haproxy.
- Other modules: These modules provide access to a wide variety of other resources and tasks such as email, monitoring, and security. Examples of other modules include mail, zabbix_host, vault, selinux, and win_updates.
These are just a few examples of the many modules available in Ansible. Each module has its own specific options and parameters that you can use to customize its behavior. You can find more information about the modules and their options in the Ansible documentation.
Idempotence is a key concept in Ansible that refers to the ability to run the same playbook multiple times without changing the system state beyond the desired configuration. In other words, running the playbook repeatedly should have the same effect as running it once.
Ansible ensures idempotence in several ways:
- Module design: Ansible modules are designed to be idempotent by default. Each module checks the current state of the system and only makes changes if they are necessary to achieve the desired state. For example, if a module is used to create a file, it checks if the file already exists before creating it again.
- Idempotent tasks: Ansible tasks are designed to be idempotent as well. This means that you can safely run a task multiple times without changing the system state beyond the desired configuration. For example, if a task is used to ensure that a package is installed, it checks if the package is already installed before attempting to install it again.
- Check mode: Ansible includes a "check mode" feature that allows you to test the effect of a playbook without making changes to the system. This can help you identify any potential problems or unintended consequences of running the playbook.
- Idempotent data: Ansible uses variables and data structures that are designed to be idempotent. For example, variables are used to store the current state of the system, and Ansible only modifies these variables when necessary to achieve the desired state.
By following these best practices and design principles, Ansible ensures that playbooks are idempotent and can be safely run multiple times without changing the system state beyond the desired configuration. This makes it easier to manage complex systems and configurations and reduces the risk of unintended consequences or errors.
8. What is a task in Ansible?
A task in Ansible is a unit of work that performs a specific action, such as installing a package, copying a file, or restarting a service. Tasks are defined within a playbook and are executed in the order in which they are listed.
Each task in Ansible typically consists of three parts:
- Name: A descriptive name that identifies the task.
- Module: The Ansible module that performs the action. Ansible provides a large number of built-in modules that can be used to perform a wide variety of tasks, such as the apt module for installing packages on Debian-based systems or the service module for managing services.
- Arguments: The arguments that are passed to the module to configure its behavior. These can include options such as the name of the package to install or the name of the service to restart.
Here's an example of a simple task that uses the apt module to install a
package:

In this task, the name is "Install Apache web server," the module
is apt, and the arguments
specify that the package apache2
should be installed and that it should be in the present state.
Tasks are a fundamental building block of Ansible playbooks, and they can be combined in many ways to perform complex configurations and operations.
Comments
Post a Comment