Ansible is an open-source automation tool used for application deployment, configuration management, and cloud provisioning. It automates the entire IT environment to simplify complex tasks and make developers’ jobs easy and manageable.

It also works on IT orchestration, where we can run tasks in sequence and create a chain of events that appears on different servers or devices.

Ansible is growing faster than other automation tools. Let’s see some of the advantages of using Ansible:

 

  • It is a free and open-source tool
  • It is a simple tool to set up and use
  • It helps to model highly complex workflows
  • We can customize the whole application according to the need

 

This article lists some of the most popularly asked Ansible interview questions. Let’s take a look –

 

Q1. How does Ansible work?

Ans. Ansible does not break its process into steps or divisions. To understand how Ansible works, we have to follow stages:

 

Setting up the architecture:

Ansible requires nodes and release small programs called “Ansible Modules.” These released modules act as a resource to the desired state of the system. It executes these modules and removes them after the execution.

 

SSH Keys: There is an inventory file in the controlling machine that occupies the location of the node system. It helps modules to locate on the node system by running the playbook on the controlling system.

Example:

ssh-agent bash

ssh-add ~/.ssh/id_rsa

 

Managing Inventory: Ansible keeps all the machines that it is operating on the system in the INI file. The machines in the INI file are grouped as per the user’s choice. New machines are also added without any SSL signing. It is also plugging to sources such as Openstack, EC2, Rackspace, and others. Here, how inventory looks like:

 

[webservers]

www01.example.com

www02.example.com

[dbservers]

db01.example.com

Db02.example.com

 

Using Ansible: We can use Ansible, once the instance is available. All the requirements in the Ansible running commands and resource module are taken care of. Ansible already consists of the arsenal module.

Example:

ansible all -m ping

ansible foo.ex.com -m yum -a “name=httpd state=installed”

ansible foo.ex.com -a “/usr/sbin/reboot1”

Q2. What are the requirements of Ansible server?

Ans. If you are a windows user then it is required to have virtual machine in which Linux can be installed.

 

Q3. Explain different components of Ansible?

Ans. Following are some components:

  • Inventory
  • Playbooks
  • Plays
  • Tasks
  • Modules
  • Roles
  • Handlers
  • Facts
  • Templates
  • Variables

 

Q4. Is Ansible an open-source tool?

Ans. Yes, Ansible is an open-source tool because we can rewrite the modules. An open-source automated engine provides a source to automate apps as per the need.

 

Q5. Name the language in which Ansible is written?

Ans. Ansible is written in Python and Powershell.

 

Q6. Differentiate between Ansible Playbooks and Roles.

Ans. Following are the differences between Roles and Playbooks:

Roles

Playbooks

●       It is a set of tasks and additional files to configure the host

●       Roles are considered as group tasks into one container. We  can use role for setting up MySQL, and for setting up postfix

●       Example: Common, Webservers, etc.

 

●       It is a mapping between roles and hosts

●       A playbook identifies what is happening where. Sometimes playbook only contains single play, but we can get the number as required

●       Example: fooservers.yml, site.yml, webservers.yml, etc.

 

 

 

Q7. Define Ansible Galaxy.

Ans. Ansible Galaxy refers to the Galaxy website used to find, share, and download modules and roles by the Ansible users. These command-line tool is available with Ansible, it is used to install roles from Galaxy or the source management system. It can also be used to create new roles, remove old ones, and perform tasks on the Galaxy website. We can boost our project using Galaxy.

The syntax used to download roles from Galaxy website:

$ansible-galaxy install username.role_name

 

Q8. What are the variables in Ansible?

Ans. Variables are alike with the variables in another programming language. These are assigned a value that is used to determine playbooks. We can also use conditions by using variables:

– hosts: your host01

vars:

port_Tomcat: 2050

Here, a variable port Tomcat is defined, and the value assigned to the port number is 2020.

 

Q9. What are the differences between a variable name and an environment variable?

Ans. Following are the difference between a Variable Name and an Environment Variable:

Variable Name

Environment Variable

●       Strings need to be added to create variable names

●       We can define multiple variable names by adding strings

●       Ipv4 address is used for variable names

●       An existing variable is needed to access the environment variable

●       We have to use Ansible playbook to create an environment variable

●       Use {{ansible_env.SOME_VARIABLE_A }} for remote environment variables

 

 

Q10. What are the Ansible Modules? Explain the different types.

Ans. Modules are the small units of code that perform a specific task. It can be used from the command line or in a playbook task. It helps to automate a wide range of tasks.

ansible webservers -m service -a “name=httpd state=installed”

ansible webservers -m ping

ansible webservers -m command -a “/sbin01/reboot -t now”

There are two types of modules:

  • Core modules: The core team maintains these modules, and it will ship with Ansible only. They receive slightly high priority for the requests than extras modules.

 

  • Extras modules: Ansible itself ships these modules, but it can be shipped separately in the future. Non-core modules receive a lower response as compare to core modules.

 

Q11. How can you access a variable of the first host in a group?

Ans. By using the following command, we can access a variable:

{{ hostvars[groups[‘webservers’][0]][‘ansible_eth0’][‘ipv4’][‘address’] }}

In the given code, we’re accessing the hostname of the first machine in the webservers group. To use a template to do this, we can use Jinja2 ‘#set’ or set_fact, like written below:

– set_fact: headnode={{ groups[[‘webservers’][0]] }}

– debug: msg={{ hostvars[headnode].ansible_eth0.ipv6.address }}

 

Q12. How to write an Ansible Handler with multiple tasks?

Ans. If you want to create a handler that restarts a service when it is running

Ansible Handlers. It can listen to the general topics, and those topics are shown below. This task makes it easier to trigger multiple handlers. It also decouples handler from their name and makes it simpler to share handlers in Playbooks and roles:

– name: Check if restarted

shell: check_is_var.sh

register: result

listen: Restart processes_a

– name: Restart conditionally step 1

service: name=service state=restarted

when: result

listen: Restart processes_a

 

Q13. How to keep secret data in the playbook?

Ans. If we have a task and we don’t want to show the output or command given in it when using -v (verbose) mode, the following code is used:

– name: secret task

shell: /usr/bin_a/do_something –value={{ secret_value01 }}

no_log: True

It can be used to keep verbose output

– hosts: all

no_log: True

 

Q14. What is Ansible Tower?

Ans. Ansible Tower is a web-based solution used by IT teams for managing an organization with a very easy user interface. It is mainly designed for solving all sorts of automation tasks. It provides a dashboard with the state summaries of all the hosts, monitors each configuration, and allows fast deployments.

 

It allows to share the SSH credentials, manage inventories graphically, logs all the jobs, and syncs them with a variety of cloud providers.

 

Q15. Mention the features of Ansible Tower.

Ans. Following are the features:

  • Ansible Tower Dashboard – It presents all sorts of things available on their environments such as the hosts, the recent job activity, inventory status, and more.
  • Real-Time Job Updates – An Ansible can automate the whole infrastructure so that we can see the real-time job updates, like tasks and plays divided by each machine. By this, we can check our automation status and easily track what’s next in the queue.
  • Multi-Playbook Workflows – It allows to connect any number of playbooks, in spite of using different inventories, run various users, or utilize multiple credentials.
  • Scale Capacity With Clusters – With this feature of Ansible tower, we can connect various nodes of it into the Ansible tower cluster. It creates redundancy that allows scaling Ansible automation over the enterprise.
  • Integrated Notifications – This feature notifies if the job succeeds or fails in the whole organization or customize on a per-job basis.
  • Remote Command Execution – On this platform, we can run simple tasks like restart malfunctioning service, reset passwords, and add users in the inventory with Ansible Tower’s remote command execution.
  • Schedule Ansible Jobs – Ansible Tower provides this feature for various kinds of jobs like Playbook runs, source control, and cloud inventory updates that can be scheduled to run across the platform according to the need.
  • Who Ran What Job When – It provides us the option to know who ran what job when and whereas all the automation activity is securely logged in the Ansible tower.
  • Manage & Track Inventory – It provides the benefit of managing the entire infrastructure by allowing us to use inventory from public cloud providers like Microsoft Azure, Amazon web services, and others.
  • Self-Service – It allows us to launch Playbooks with just a single click. It also helps to choose from the available credentials and record the available deployments.

 

Q16. Write a code to set the PATH or any environment variable for a task?

Ans. Environment variables can be set by using the keyword “Environment.” This variable can be set for the entire Playbook or only for a particular task.

Following code shows, how to set a path:

Environment:

PATH: “{{ ansible_env.PATH }}:/thing_a/bin”

SOME: value01

Q17. How can you handle various machines needing different user accounts or ports to log in with?

Ans. It can be handled by setting inventory variables into the inventory file.

These hosts consist of different username and ports:

[webservers]

www.example.com ansible_port=2050 ansible_user=tom

jkl.examplein ansible_port=5002 ansible_user=jack

We can also specify the connection type:

[testcluster]

localhost ansible_connection=local

/path/to_a/chroot1 ansible_connection=xyz

www.example.com ansible_connection=abc

To make the task easier, we can compile it into a file group_vars/<group-name> file.

 

Q18. Does Ansible support AWS?

Ans. Ansible has multiple modules that support AWS; some of them are:

  • Route53
  • Elastic Cloud Compute (EC2)
  • Virtual Private Cloud (VPC)
  • Relational Database Service (RDS)
  • CloudWatch
  • Identity Access Manager (IAM)
  • DynamoDB
  • ElastiCache
  • CloudTrail
  • CloudFormation
  • Simple Storage Service (S3)
  • Autoscaling groups

 

Q19. Can you copy files to remote locations recursively onto a target host? If yes, how?

Ans. Yes, we can copy files to remote locations repetitively by using a copy module. It can copy files from the directory recursively, and the other module called synchronize that is specially used for this process.

– synchronize:

   src: /first/absolute01/path

   dest: /second/absolute02/path

   delegate_to: “{{ inventory_hostname_xyz }}”

Q20. How can you access a variable of the first host in a group?

Ans.  We can access a variable by executing the below command:

{{ hostvars[groups[‘webservers’][0]][‘ansible_eth0’][‘ipv4’][‘address’] }}

In the given command, we are obtaining the hostname of the first machine in the webservers group. We can use Jinja2 ‘#set’ or set_fact that is shown below:

– set_fact: headnode={{ groups[[‘webservers’][1]] }}

– debug: msg={{ hostvars[headnode].ansible_eth0.ipv6.address }}

5.00 avg. rating (96% score) - 1 vote