Ansible Interview Questions & Answers for Devops

check ansible interview questions

In this guide, we're going to look at Ansible Interview Questions & Answers for DevOps Engineers. This is intended to help you prepare for Ansible Devops Interview and also review your skills. Over the past one year, Ansible adoption has been massive. It is now one of the most used software to automate software provisioning, configuration management, and application deployment on Infrastructures ranging from a single server to multi-server clusters.

If you're reading this guide, I believe you've already gone through the basics of Ansible - How it works, its requirements and its benefits over similar tools like Puppet, Chef, etc...

1) What is Ansible?

Ansible is an open-source IT automation tool written in Python, now powered by Redhat.  It can provision systems, deploy software, configure systems and orchestrate advanced IT tasks like continuous deployments with zero downtime.

2) What are the advantages of Ansible over other automation Tools?

  • Ansible is agentless - Only requires SSH service up on the target machines
  • Only required dependency is Python. Most systems come with Python pre-installed
  • It has low overhead - Minimal resource utilization
  • Ansible tasks are written in YAML syntax - Easy to learn and understand
  • Ansible is Declarative - Unlike other tools which are mostly Procedural. You just need to define the desired state and Ansible will fulfill the requirements to reach that state

3) Which options are available to Install Ansible?

  • Install from distribution package repository
  • Build from source code
  • Install using Python package management tools like pip

4) Explain how Ansible works?

Ansible consists of nodes and controlling machine.
Ansible will be installed on the controlling machine and nodes are managed inside this controlling machine by SSH without any downtime. Nodes locations are specified by controlling machine through it's inventory.
Ansible deploys modules to nodes using SSH protocol, and these modules are stored only temporarily in remote nodes and they communicate with the Ansible machine through a JSON connection over the standard output.

5) Describe one of the practical uses of Ansible?

Ansible is used for managing IT infrastructure and deploy software apps to remote nodes.
Ansible can be used to deploy many software applications to many nodes using a single command. Let’s say you want to deploy one or more applications to more than 100 nodes using a single command, this is what Ansible can help us with.
Here is how we can install packages zlib and vim on all machines:

- hosts: all
  vars:
      package_list:
        - 'zlib' 
        - 'vim'
  tasks:
      - name: Install zlib and vim
        become: "yes"
        package:
            name:"{{ package_list }}"
            state: present

6) How do you secure Passwords in Ansible?

Ansible has a feature called Vault which allows you to keep sensitive data such as passwords or keys in encrypted files, rather than as plaintext in playbooks or roles. It has a command line tool called ansible-vault used to edit files. Common flags used with this command line tool is  --ask-vault-pass or --vault-password-file.  To run a playbook that contains vault-encrypted data files, you must pass one of two flags.

7) What is an Ansible Inventory?

Ansible has a default file on /etc/ansible/hosts which contain the IP Addresses or hostnames for the target systems.
You can specify a different inventory file using the -i <path> option on the command line. Ansible selects a portion of hosts to work on from this file. The file format can be INI or YAML, the default being INI-like. In INI format,  headings in brackets are group names, which are used in classifying systems and deciding what systems you are controlling at what times and for what purpose. e.g.

[db-servers]
192.168.10.4
192.168.10.5

[app-servers]
10.1.5.4
10.1.5.5

Host and Group variables can be defined in inventory file. It is easy to assign variables to hosts that will be used later in a playbook, e.g.

[web-servers:vars]
ntp_server=ntp.server.com
proxy=proxy.server.com

8) What are Ansible Playbooks?

Ansible Playbooks are the building blocks for all the use cases of Ansible. The different YAML tags are classified into four types known as declaration, vars, tasks, and handlers.
Playbooks are a set of commands which can perform multiple tasks and they are written in YAML file format.

9) What is the method to check the inventory vars defined for the host?

Use the following command to check the inventory:

$ ansible -m debug -a "var=hostvars['hostname']" localhost

10) What is Ansible Tower?

Ansible Tower is a web-based solution that makes Ansible even more easy to use for IT teams of all kinds. It’s designed to be the hub for all of your automation tasks. The tower is free for usage of up to 10 nodes.

11) What is Ansible role?

Ansible role is a way of bundling automation content and making it reusable.
The very first step in creating an Ansible role is creating it's directory structure. To create the base directory structure we use a tool bundled with Ansible called Ansible Galaxy.
This command will create the linoxide.packer directory:

$ ansible-galaxy init linoxide.packer
- linoxide.packer was created successfully

This is directory structure created within linoxide.packer directory:

$ ls
README.md	files		meta		templates	vars
defaults	handlers	tasks		tests

12) What is Ansible Galaxy?

Ansible galaxy is a tool bundled with Ansible which is used to create base directory structure. Using the Ansible commands, Ansible communicates with configured clients. You can automate configuration with playbooks run through the ansible-playbook command.

13) Describe types of Ansible modules.

Modules in Ansible are idempotent. From a RESTful service standpoint, for the operation to be idempotent, clients can perform the same result by using modules in Ansible. Multiple identical requests become a single request.
We can also create our own modules within Ansible.
If you know how to code in Python you can start creating your own modules in few hours from scratch and you don't need to have any prior knowledge of the same.

There are two different types of modules in Ansible:

a) Core modules
b) Extras modules

Core Modules:

The Ansible team maintains these types of modules, and they will always ship with Ansible software. They will also give higher priority to these modules than those in the “extras” repos.

Extras Modules:

These modules are currently bundled with Ansible, but might be available separately in the future. They are also mostly maintained by the Ansible community.

14) Explain Ansible facts

You can think of ansible facts as a way for ansible to get information about a host and store them in variables for easy access. This information stored in predefined variables are available for use in the playbook. To generate facts, ansible runs the setup module.

$ ansible- m setup hostname

Once this statement is executed, it will print out a dictionary of all the facts that are available for that particular host. This is the best way to access the list of Ansible_variables.

You can see all the facts via:

$ ansible all- m setup

So if you want to extract only certain part of the information then you can use “setup” module where you will have an option to filter out the output and just get hold of the fact that you are in need of.

15) What are ad-hoc commands?

Ad-hoc commands are the way we can use to take actions on our nodes without having to write new playbooks. If you want, let's say to reboot all of the hosts in a particular group, you can either write a new playbook, or simply run a one-off ad-hoc command.

$ ansible servers -a "/sbin/reboot"

16) How do you access Shell Environment Variables?

Accessing the existing variables in controlling machine is done by using “env” lookup plugin.
For example:
Accessing the value of Home environment variable on management machine:

local_home:”{{lookup(‘env’,’HOME’)}}”

17) Write a task to create the directory ‘/tmp/linoxide’

Creating a new directory on a node is done like this:

- name: Create a new directory
  file:
      path: "/tmp/linoxide"
      state: directory

18) How can we test our Ansible based projects?

There are several ways we can test our Ansible projects:

Manual run: We can simply run the play and check if the system is in the desired state – This is the easiest and laziest method, but it can also be potentially dangerous because results in a test environment and in a production environment might not be the same.

Check mode: Check mode is a good way to test your Ansible project because it will report everything it would have done as if it was run without simulation. So you can easily see if the Ansible run behavior is as you wanted it to be. But check mode doesn’t run scripts and commands used in roles and playbooks. To run them, you would have to disable check mode for specific tasks with “check_mode: no”.

Asserts: Asserts as a method of testing is great because it also resembles how you test in other languages such as Python and more importantly, it makes sure that your system reached the desired state, not as a simulation like in check mode, but as an actual verification that the task changed certain resource to the desired state.

You can also check out Ansible testing tools like Molecule and Test Kitchen.

19) Explain what is Red Hat Ansible?

Ansible and Ansible Tower by Red Hat, are both an end to end complete automation platforms which are capable of providing the following features and functionalities:

    • 1. Provisioning
      2. Deploying applications
      3. Orchestrating workflows
      4. Manage IT systems
      5. Configuration of IT systems
      6. Networks
      7. Applications

20) What is Continuous Delivery?

Continuous delivery is a practice of delivering the software as soon as it has some changes committed to it. Using this method, we need to use some version control system, like GitHub or GitLab. The software is constantly updated in live production systems.

Hopefully, you are now better prepared for an Ansible DevOps engineer position interview at some company. This position is really popular these days, and companies are always on the lookout for new DevOps engineers. Good luck to all of you wishing to dive into Ansible and DevOps field.

Read Also:

Slavisa Milojkovic 5:50 pm

Comments

Your email address will not be published. Required fields are marked *

All comments are subject to moderation.