Updated April 29, 2023
Introduction to Ansible Interview Questions and Answers
Ansible is an open-source automation platform. Ansible helps in task automation, Application deployment, and managing configuration. Ansible is developed in Python language. Ansible consists of two servers controlling machines and Nodes. SSH contains these two servers without any downtime. Using SSH protocol, Ansible deploys modules to nodes mainly stored temporarily in remote nodes. Ansible can handle over 100 nodes in a single system over an SSH connection.
So you have finally found your dream job in Ansible but are wondering how to crack the Ansible Interview and what could be the probable 2023 Ansible Interview Questions. Every interview is different, and the job scope is different too. Keeping this in mind, we have designed the most common Ansible Interview Questions and answers to help you get success in your interview.
Below is the list of common 2023 Ansible Interview Questions and Answers:
Ansible Interview Questions And Answers
The following Interview Questions and Answers are mentioned below.
Q1. What is Ansible?
Answer:
Ansible is an open-source automation platform. It can help us with configuration management, application deployment, or task automation. It can deploy an application using SSH without any downtime. The developers of Ansible wrote it in the Python programming language.
Q2. What are the advantages and use of Ansible?
Answer:
Ansible has a huge number of benefits:
- No Agent: An agent is not required to set up Ansible. If Box can support ssh and it has Python, then no issue setting up Ansible.
- Idempotent: The architecture of Ansible is structured around the concept of idempotency. The main or core idea is that only those things that need to be added are needed, and those things will be repeatable without side effects.
- Declarative, not procedural: A Normal attitude of other configuration tools of following a procedural process, which means do this, then do that, and so on. But Ansible writes typically the description of the state of the machine that we want, and it takes proper steps toward fulfilling that description.
- Very easy to learn and has low overhead
Q3. How does Ansible Works?
Answer:
Ansible is mainly categorized into two types of servers: controlling machines and Nodes.
The controlling machine installs Ansible and manages nodes inside it using SSH. Node locations are specified by controlling a machine through its inventory. Ansible deploys modules to nodes using SSH protocol; these modules are mainly stored temporarily on remote nodes and communicate with the Ansible machine through a JSON connection over the standard output. Agent installation in remote nodes is not required for Ansible as it is agentless.
So any background or daemon process is not running for Ansible when it’s not managing any nodes. Ansible can handle more than 100 nodes in a single system over an SSH connection, and only one single command, ‘ansible,’ can handle the entire operation. But in some scenarios, we can build ‘playbooks’ where we must execute multiple commands for deployment. Playbooks hold all the sequential commands that must be executed for performing multiple tasks. Playbooks are in the YAML file format.
Q4. What’s the use of Ansible?
Answer:
IT infrastructure professionals mainly use Ansible to manage or deploy applications to remote nodes. Suppose we want to deploy one application in 100 nodes by executing one command. Ansible is coming into the picture, but you should know the Ansible script to understand or execute the same.
Q5. When should I use {{ }}? Also, How do I Interpolate Variables or Dynamic Variable Names?
Answer:
One of the standard rules is ‘always use {{}} except when:.’ Conditionals are always run through Jinja2 to resolve the expression. So when:failed_when: and changed_when: are always templated, we should avoid adding {{}}. In other cases, except when clause, we have to use brackets; otherwise, distinguishing between an undefined variable and a String will be very difficult.
Q6. How do I generate Crypted Passwords for the User Module?
Answer:
A normal mkpasswd utility in a normal Linux system is an excellent option for generating crypted passwords.
mkpasswd –method=sha-512
If somehow this utility is not available in our system, suppose we are using OS X, then also we can quickly generate this password using Python. But for the same, we need to install the Passlib password hashing library in our system.
Once installed, execute a command that will generate SHA512 password values.
python -c “from passlib.hash import sha512_crypt; import getpass; print sha512_crypt.encrypt(getpass.getpass())”
Q7. What is the best way to make Content Reusable/ Redistributable?
Answer:
There have 3 ways to reuse files in the playbooks of Ansible. 3 ways include imports and roles.
Include and Imports are helping to create multiple small files of a large playbook or break up a large playbook of multiple small files. Those small files can be used across parent playbooks or multiple times within the same playbook. Roles are mainly used to manage multiple tasks in a package together. It can include variables, handlers, or even modules and other plugins. Ansible Galaxy enables users to upload and share roles.
Q8. How do I copy files recursively onto a Target Host?
Answer:
The copy module of Ansible has a recursive parameter. But most efficient to use synchronize module, which wraps rsync. For the synchronize module, we can use the below command:
-name: Copy over h5bp configuration
synchronize: mode=pull src=/tmp/server-configs-nginx/{{ item }} dest=/etc/nginx/{{ item }}
with_items:
– “mime. types”
– “h5bp/”
To copy remote to remote, use the same command, delegate_to (as remote source) and current inventory_host (as remote dest).
Q9. Ansible Playbooks vs Roles and Ansible vs Puppet.
Answer:
Ansible Playbook is a straightforward configuration management and multi-machine deployment system. If we compare it with another, it is very popular and well-suited for deploying a complex application.
And Roles in Ansible are automatically loading certain var_files, tasks, and handlers based on a known file structure. Roles mainly group all the contents and packages. It also allows easy sharing of roles with other users.
Ansible | Puppet |
Excellent Performance, Agentless installation, and deployment. | Performance is a little questionable compared to Ansible, and it’s an agent-based install. |
Based on the Python language. | Based on the Ruby language. |
CLI accepts commands in almost all languages. | All customers must learn Puppet DSL. |
It is very and has not tested well, so it can’t give a proper and mature solution for every situation. | It always provides a mature solution compared to Ansible. |
GUI is in work in progress. | Good GUI. |
No support for Windows. | Support for all major OS. |
Q10. What is the difference between a variable name and environment variables?
Answer:
Variable Name | Environment Variable |
The variable name can be built by adding String. | To access the environment variable need to access the existing variable. |
{{ hostvars[inventory_hostname][‘ansible_’ + which_interface][‘ipv4’][‘address’] }} | # … vars: local_home: “{{ lookup(‘env’,’HOME’) }}” |
We can add Strings. | If we want to add the variable, we must open the advanced playbooks section. |
For variable name, we use the IPV4 address. | For Remote environment variables, use {{ ansible_env.SOME_VARIABLE }} |
Recommended Articles
This has been a guide to the List of Ansible Interview Questions and Answers. Here we have listed the most beneficial 10 interview sets of questions so that the jobseeker can easily crack the interview. You may also look at the following articles to learn more –