DevOps - Ansible Interview Questions and Answers

Q1. Explain the steps involved in automating software installation using Ansible on Linux and Windows servers.  

Ansible core

  • Create a playbook for the installation of the application. We can use yum module for Linux and win_package module for windows.
- hosts: all
  - name: Installation on linux
     - httpd
     - memcached
     state: present
    when: ansible_facts['distribution'] == "Redhat"
  - name: Installation on windows
     path: C:\temp\rdcman.msi
     state: present
    when: ansible_facts['distribution'] == "Windows"
  • Create an inventory file that contains the servers, we can create a group Linux for Linux servers and windows for windows servers. Provide the IP/hostname, details of the servers, and ansible connection parameters.
[linux] ansible_connection=ssh ansible_user=myuser
[windows] ansible_connection=winrm ansible_winrm_transport=basic ansible_user=admin

Ansible Tower  

  • Create a playbook in GIT for the installation of the application. Refer above for the playbook.
  • Create a project in Ansible Tower and specify the GIT URL.
  • Create two inventories(one for Linux and one for windows) with the required servers in Inventory and ansible connection parameters.
  • Create two credentials(one for Linux and one for windows) for these servers in Credentials.
  • Create two job templates (one for Linux and one for windows) and provide the project, inventory, and credentials.
  • Launch the job templates.

Q2. What are roles in Ansible?  

Roles allow to group the vars, files, handlers, templates, tasks, etc, and reuse them or share it with others.

Q3. What are handlers in Ansible?  

Handlers are tasks that are executed only when triggered using notify.
For example, if we need to restart the service after its installation, then we use handlers.

Q4.What are templates in Ansible?  

Templates are configuration files with .j2 extension. It contains dynamic variables. The values of these variables are passed during the execution of the playbook.

Q5. How to use a loop in Ansible tasks. Give an example.

To use a loop in ansible tasks we use loop in a task.
eg: loop: "{{ values }}"

Q6. How to use a loop in Ansible template. Give an example.

To use a loop in ansible template we use for in a template.
eg: {% for val in values%}

Q7. What is Ansible galaxy?  

Ansible galaxy consists of Ansible roles that are available for use in a repository.
Users can install or remove the roles using ansible-galaxy command.

Q8. What is the version of Ansible/ Ansible Tower which you are using?  

Ansible version is 2.9 and Ansible Tower version is 3.8. (These are the latest version while writing this article)

Q9. Name a few Ansible modules which you have worked on.  

yum, service, file, command, shell, lineinfile etc. (Make sure you can explain whatever module you mention.)

Q10. What is the use of lineinfile module.  

This module checks if a particular line is present in a file, or replaces an existing line using regex.

Q11. What is the protocol and port that ansible uses for Linux and windows connections?  

Linux - ssh port 22
Windows - winrm port 5985

Q12. Explain and give an example of REST APIs that Ansible Tower provides.  

Ansible Tower REST API can be viewed in a web browser using http://towerservername/api/
To launch a Job template we can use as below using the uri module.
Where id is the id of the job template.

Refer to the below link for more examples.

Q13. How to create a CSV file and write contents to it using Ansible.  

We can create a csv file using the file module.

 path: /var/myfile.csv

Use copy module to add contents to it.

 content: 'Name, ID, Position'
 dest: /var/myfile.csv

Q14. How to save the output of a task to a variable.  

To save the output of a task we can use register variable.

Q15. How to hide sensitive information in a task from appearing in the job output.  

We can use no_log: true in the task so that the output will be hidden.

Q16. How to set environment variable in Ansible. Will the variable be set in the entire playbook or in the specific task alone?  

We can use the environment module in ansible. The environment variable will be set just for the specific task.


Q17. How to create a vault in Ansible Tower.  

We cannot create the vault in Ansible Tower. We need to login to any server where Ansible is installed and then create the vault using ansible-vault command.
Once the vault is created, navigate to credentials in Ansible tower and create a credential by choosing the type as vault.


Q18. How to create a user in Linux using Ansible?  

We can use the user module in ansible to create user.

 name: Paul
 uid: 7777
 group: guest

Q19. What is the module for windows domain join using Ansible.  

We can use win_domain_membership together with win_reboot for windows domain join.

Q20. How to check if a server is reachable using ansible.  

We can use the ping module in Ansible to check if the server is reachable.

Q21. Can we use if else condition in Ansible?  

Yes, we can use if-else condition in Ansible variables.
eg: port: "{{ '22' if protocol == 'ssh' else '21' if protocol == 'ftp'}}"

Q22. Suppose we have 10 tasks in a playbook and we need to execute only a few of them. How can this be achieved?  

We can use tags in each task and mention the tag name during the playbook execution.

- name: Installation
    - httpd
    - memcached
    state: present
  - linux
  - install

To call the playbook, execute the below command

ansible-playbook example.yml --tags "linux, install"

ansible-playbook example.yml --skip-tags "linux"

You have an option to add or skip the tags in Ansible tower job template as well.

Q23. Suppose we have 10 tasks in a playbook. The second task has a reboot command which will reboot the remote server. In this scenario, will the third task be executed or the connection will be lost. Is there a way to execute all the remaining tasks as well

When we use the  reboot module to reboot a server, the server goes down, comes up, and then executes the commands. So all the tasks will be executed.

Q24. Suppose there are 10 tasks in a playbook and 10 servers in the inventory.  You need to execute the first task only on the first server and skip it for the remaining servers.  How can this be achieved?  

We can use the run_once module with the task so that the task is executed only on the first server.

Q25. What is delegate_to in Ansible?  

Ansible playbook is generally executed on the servers which are mentioned in the hosts line of the playbook. If we need to execute any tasks on a different server, we can use delegate_to.
eg: delegate_to: localhost

Q26. Difference between static inventory and dynamic inventory.

Static inventory consists of hosts which are constant and doesn't change.
Dynamic inventory consists of hosts which keep on changing. New hosts gets added and old hosts get removed. It can be managed using scripts.

Q27. What is a custom module in Ansible?  

At times the existing modules in Ansible might not cover the functionality required. In this scenario, we might need to write custom module using python script or any other language.

Q28. Explain server provisioning using Ansible.

Ansible can provision servers in cloud, virtualised server, hypervisors etc.

vmware_guest module is used to create virtual machines in VCenter

ec2 module is used to create ec2 instance in AWS

Q29. Are nested loops available in Ansible?  

Yes, nested loops are available in Ansible.

Q30. What is loop_control and loop_var?  

The loop_control and loop_var is used to define your own variable name for each item in a list.

 loop_var: testing


I am a dreamer. I admire the web. I admire anything about the web.