0% found this document useful (0 votes)
18 views

ansible_qna

The document provides 15 real-world Ansible scenario questions and answers, focusing on troubleshooting, automation, playbook writing, and inventory management. Key topics include handling playbook failures, managing sensitive data, using dynamic inventories, and ensuring playbooks run on specific hosts. It also covers best practices for testing, rolling back changes, and optimizing Ansible for large-scale infrastructure.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

ansible_qna

The document provides 15 real-world Ansible scenario questions and answers, focusing on troubleshooting, automation, playbook writing, and inventory management. Key topics include handling playbook failures, managing sensitive data, using dynamic inventories, and ensuring playbooks run on specific hosts. It also covers best practices for testing, rolling back changes, and optimizing Ansible for large-scale infrastructure.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 92

Here are 15 real-world Ansible work scenario questions with answers in the first person:

1. How do you handle a situation where an Ansible playbook fails midway?

If an Ansible playbook fails, the first thing I do is check the error message to identify the root
cause. I use the -v, -vv, or -vvv flags for detailed logs. If the issue is with a particular task, I use --
start-at-task to resume execution from that point instead of rerunning everything. If the playbook
modifies critical configurations, I ensure idempotency so it can be safely re-run without causing
issues.

2. How do you manage sensitive data like passwords in Ansible?

I use Ansible Vault to encrypt sensitive data such as passwords, API keys, and SSH keys. I create
an encrypted file using:

ansible-vault create secrets.yml

To use the encrypted variables in a playbook, I reference them like this:

vars_files:
- secrets.yml

If multiple users need access, I share the vault password securely or use Ansible AWX/Tower
for centralized credential management.

3. How do you handle dynamic inventories in cloud environments?

For AWS, I use the AWS EC2 inventory plugin. I configure aws_ec2.yml and retrieve dynamic
hosts with:

ansible-inventory -i aws_ec2.yml --list

For other clouds like Azure or GCP, I use their respective dynamic inventory plugins. This
ensures that new instances are automatically included without manual updates.

4. How do you ensure playbooks run only on specific hosts?


I use the limit flag (-l) to restrict execution to a specific host or group:

ansible-playbook site.yml -l web1

Alternatively, I define conditions in the playbook using when statements, so tasks only run when
certain conditions are met.

5. How do you check which hosts are reachable before running a playbook?

Before executing any playbook, I use the ping module to verify connectivity:

ansible all -m ping

If a host is unreachable, I check the SSH configuration, firewall settings, and network
connectivity.

6. How do you roll back changes if a playbook causes issues?

If a playbook modifies configurations, I create a backup before making changes:

- name: Backup existing configuration


copy:
src: /etc/nginx/nginx.conf
dest: /etc/nginx/nginx.conf.bak

For package installations, I ensure the previous version is available and use the state: absent
option to remove faulty installations.

For complex rollbacks, I maintain version-controlled infrastructure using Git and Ansible roles.

7. How do you manage multiple environments (dev, staging, production)?

I use inventory directories to separate environments:

inventories/
├── dev/hosts
├── staging/hosts
├── prod/hosts

Then, I specify the inventory when running a playbook:


ansible-playbook deploy.yml -i inventories/prod/hosts

This prevents accidental changes in the wrong environment.

8. How do you debug an Ansible module not working as expected?

I use the debug module to print variables and outputs:

- name: Debugging variables


debug:
var: my_variable

For deeper insights, I enable verbosity:

ansible-playbook myplaybook.yml -vvv

If a specific module fails, I manually run the corresponding command on the remote machine to
check for syntax or permission issues.

9. How do you schedule playbooks to run automatically?

I use cron jobs or Ansible AWX/Tower for scheduling. With cron:

crontab -e

Then, I add:

0 2 * * * ansible-playbook /path/to/playbook.yml

For better control, I prefer AWX/Tower, which provides a web UI for scheduling and
monitoring.

10. How do you handle different user privileges in Ansible?

If a task requires root privileges, I use:

- name: Install Nginx


become: yes
apt:
name: nginx
state: present

For specific users, I define them explicitly:

- name: Execute as different user


become: yes
become_user: deploy
command: whoami

11. How do you use Ansible to deploy an application?

I create a playbook that:

1. Installs dependencies (apt, yum, pip).


2. Pulls the latest code from a Git repository.
3. Configures necessary environment variables.
4. Restarts the application service.

Example:

- name: Deploy Web App


hosts: webservers
tasks:
- name: Install dependencies
apt:
name: nginx
state: present

- name: Pull latest code


git:
repo: "https://github.com/myorg/myapp.git"
dest: /var/www/myapp

12. How do you test Ansible playbooks before applying them to production?

I use Ansible Dry Run (Check Mode):

ansible-playbook site.yml --check --diff

For testing infrastructure changes, I use Molecule and Vagrant.

13. How do you use Ansible to update a fleet of servers without downtime?
I use rolling updates with serial:

- name: Rolling update


hosts: webservers
serial: 2
tasks:
- name: Restart Nginx
service:
name: nginx
state: restarted

This ensures only two servers restart at a time, preventing complete downtime.

14. How do you handle host-specific configurations in a playbook?

I use host_vars and group_vars:

inventory/
├── host_vars/
│ ├── web1.yml
│ ├── db1.yml
├── group_vars/
│ ├── webservers.yml
│ ├── dbservers.yml

Inside web1.yml:

nginx_port: 8080

Then, in my playbook:

- name: Configure Nginx


template:
src: nginx.conf.j2
dest: /etc/nginx/nginx.conf

This applies unique configurations per host.

15. How do you optimize Ansible for large-scale infrastructure?

1. Use Forking
Increase parallel execution:
2. ansible-playbook -f 10 myplaybook.yml
3. Enable Pipelining
In ansible.cfg:
4. [ssh_connection]
5. pipelining = True
6. Use Fact Caching
Store facts to reduce SSH overhead:
7. [defaults]
8. fact_caching = jsonfile
9. fact_caching_connection = /tmp/ansible_facts
10. Use Async Tasks
For long-running tasks, use:
11. - name: Run task in the background
12. shell: sleep 300
13. async: 300
14. poll: 0

Would you like me to refine or add more scenario-based questions?

Great! Here are 15 real-world Ansible work scenario questions, with answers in the first
person to help you handle interviews like a pro.

1. You need to quickly check if all servers in your inventory are reachable. What do
you do?

I use an ad-hoc command with the ping module to test connectivity:

ansible all -m ping

If a server fails to respond, I check SSH access, ensure the host is online, and verify my inventory
file.

2. A playbook you ran caused a service outage. How do you troubleshoot?

First, I check the Ansible logs and output to identify which task caused the issue. Then, I verify
the affected servers using:

ansible webservers -m service -a "name=nginx state=status"

If needed, I roll back changes by running a corrective playbook or manually restoring the
service.
3. How do you install software on multiple servers using Ansible?

I write a playbook using the apt or yum module depending on the OS. Example:

---
- name: Install Nginx
hosts: webservers
become: yes
tasks:
- name: Install Nginx
apt:
name: nginx
state: present

Then, I run:

ansible-playbook install_nginx.yml

4. Your SSH connection to a remote server is failing in Ansible. What do you check?

I first test SSH manually:

ssh user@remote_host

If it fails, I check:

 SSH key authentication (~/.ssh/id_rsa.pub)


 Ansible’s SSH settings in ansible.cfg
 Whether the user has sudo privileges

If everything looks fine, I run:

ansible all -m ping -vvv

The -vvv option provides detailed debug logs.

5. How do you ensure that an Ansible playbook only runs on specific OS types?

I use conditionals with ansible_facts. Example:

- name: Install Apache only on Ubuntu


hosts: all
become: yes
tasks:
- name: Install Apache
apt:
name: apache2
state: present
when: ansible_os_family == "Debian"

This prevents the task from running on incompatible OS types.

6. You need to copy a configuration file to multiple servers. How do you do it?

I use the copy module in a playbook:

- name: Copy config file


hosts: webservers
tasks:
- name: Copy file
copy:
src: /local/path/config.conf
dest: /etc/myapp/config.conf

Alternatively, I can use an ad-hoc command:

ansible webservers -m copy -a "src=/local/path/config.conf dest=/etc/myapp/config.conf"

7. How do you manage sensitive credentials in Ansible?

I use Ansible Vault to encrypt passwords and sensitive files:

ansible-vault encrypt secrets.yml

To use the vault in a playbook:

- name: Deploy app


vars_files:
- secrets.yml

To run the playbook:

ansible-playbook deploy.yml --ask-vault-pass


8. How do you test an Ansible playbook before deploying it?

I use Dry Run mode with --check:

ansible-playbook deploy.yml --check

This simulates the execution without making actual changes. I also use:

ansible-lint deploy.yml

To check for syntax errors and best practices.

9. You need to restart a service only if a configuration file changes. How do you do it?

I use handlers in my playbook:

- name: Deploy config


hosts: webservers
tasks:
- name: Copy new config
copy:
src: config.conf
dest: /etc/nginx/nginx.conf
notify: Restart Nginx

handlers:
- name: Restart Nginx
service:
name: nginx
state: restarted

The notify keyword ensures the restart only happens if the file changes.

10. How do you fetch logs from multiple servers using Ansible?

I use the fetch module:

- name: Get logs


hosts: webservers
tasks:
- name: Fetch log files
fetch:
src: /var/log/nginx/access.log
dest: ./logs/
This pulls logs from remote servers to my local machine.

11. How do you execute a command on multiple servers but ignore failures?

I use the ignore_errors: yes option:

- name: Run a risky command


hosts: all
tasks:
- name: Execute command
shell: some_command
ignore_errors: yes

For an ad-hoc command:

ansible all -m shell -a "some_command" --ignore-errors

12. How do you roll back changes made by Ansible?

If I anticipate a rollback, I create a recovery playbook. Example:

- name: Rollback Nginx


hosts: webservers
become: yes
tasks:
- name: Revert config
copy:
src: /backup/nginx.conf
dest: /etc/nginx/nginx.conf
- name: Restart Nginx
service:
name: nginx
state: restarted

If necessary, I use Git-based configuration management to track and restore changes.

13. You need to execute different tasks based on hostname. How do you do it?

I use the inventory_hostname variable:

- name: Conditional execution


hosts: all
tasks:
- name: Task for Server A
debug:
msg: "This is Server A"
when: inventory_hostname == "serverA"

14. How do you dynamically create users on multiple servers?

I use a playbook with a loop:

- name: Create multiple users


hosts: all
become: yes
tasks:
- name: Add users
user:
name: "{{ item }}"
state: present
loop:
- alice
- bob
- charlie

15. How do you handle package installation for different Linux distributions?

I use the ansible_os_family variable:

- name: Install a package


hosts: all
become: yes
tasks:
- name: Install on Debian
apt:
name: vim
state: present
when: ansible_os_family == "Debian"

- name: Install on RedHat


yum:
name: vim
state: present
when: ansible_os_family == "RedHat"

This ensures the correct package manager (apt or yum) is used.


These 15 real-world scenarios cover troubleshooting, automation, playbook writing, security,
and best practices—making you interview-ready. Want me to create a mock interview with
follow-up questions?

Here are 15 real-world Ansible Inventory Management scenarios, answered in first person, as
if I were in an interview.

1. How do you organize inventory for a multi-environment setup (dev, staging,


production)?

I structure my inventory using a directory-based approach. I create separate inventory files for
each environment:

inventories/
├── dev/
│ ├── hosts.yml
│ ├── group_vars/
│ │ ├── all.yml
│ │ ├── web.yml
│ │ ├── db.yml
├── staging/
│ ├── hosts.yml
│ ├── group_vars/
│ │ ├── all.yml
│ │ ├── web.yml
│ │ ├── db.yml
├── production/
│ ├── hosts.yml
│ ├── group_vars/
│ │ ├── all.yml
│ │ ├── web.yml
│ │ ├── db.yml

Each environment has its own inventory file and group variables to customize configurations. I
can switch environments using -i inventories/dev/hosts.yml.

2. How do you handle different SSH users for different groups in inventory?

I define group-specific SSH users in group_vars:

[web]
web1 ansible_host=192.168.1.10
web2 ansible_host=192.168.1.11
[database]
db1 ansible_host=192.168.1.20
db2 ansible_host=192.168.1.21

[web:vars]
ansible_user=webadmin

[database:vars]
ansible_user=dbadmin

Now, Ansible will automatically use webadmin for web servers and dbadmin for database servers.

3. How do you filter and run a playbook only on hosts with a specific variable?

I use host patterns and filters. If I need to run a playbook only on servers where
app_role=backend, I use:

ansible-playbook site.yml -i inventory.yml --limit 'app_role_backend'

Alternatively, I use group_by to dynamically create groups based on a variable.

4. How do you deal with a scenario where some hosts have different package
managers?

I use per-host variables to define the package manager:

[all:vars]
ansible_python_interpreter=/usr/bin/python3

[debian_servers]
server1 ansible_pkg_mgr=apt
server2 ansible_pkg_mgr=apt

[redhat_servers]
server3 ansible_pkg_mgr=yum
server4 ansible_pkg_mgr=yum

Then, in my playbook, I use conditionals:

- name: Install packages


package:
name: httpd
state: present
when: ansible_pkg_mgr == "yum"

This ensures the correct package manager is used.

5. How do you work with AWS dynamic inventory?

I use the AWS EC2 inventory plugin. I configure aws_ec2.yml:

plugin: amazon.aws.aws_ec2
regions:
- us-east-1
filters:
instance-state-name: running
keyed_groups:
- key: tags.Environment
prefix: env_

Then, I run:

ansible-inventory -i aws_ec2.yml --list

This dynamically fetches instances from AWS.

6. How do you exclude specific hosts from a playbook execution?

I use the --limit flag with a negation (!):

ansible-playbook site.yml --limit '!db1'

This runs the playbook on all hosts except db1.

7. How do you create a custom inventory plugin?

I write a Python script that outputs JSON.

import json

inventory = {
"web": {
"hosts": ["web1", "web2"]
},
"_meta": {
"hostvars": {
"web1": {"ansible_host": "192.168.1.10"},
"web2": {"ansible_host": "192.168.1.11"}
}
}
}

print(json.dumps(inventory))

I save this as custom_inventory.py and use it as:

ansible-inventory -i custom_inventory.py --list

This allows me to dynamically generate inventory.

8. How do you verify your inventory before running a playbook?

I use:

ansible-inventory -i inventory.yml --list

This outputs the full inventory in JSON format. If I want a specific host’s details:

ansible-inventory -i inventory.yml --host web1

9. How do you organize inventory when working with multiple cloud providers?

I use multiple dynamic inventory files:

inventories/
├── aws_ec2.yml
├── azure_rm.yml
├── gcp.yml

I combine them in a merged inventory:

ansible-inventory -i aws_ec2.yml -i azure_rm.yml --list

This ensures I can manage multi-cloud infrastructure efficiently.

10. How do you dynamically create groups based on host variables?


I use group_by:

- hosts: all
tasks:
- group_by:
key: "os_{{ ansible_distribution }}"

Now, all Debian-based servers are in os_Debian, and Red Hat-based servers are in os_RedHat.

11. How do you use patterns to target multiple groups?

I use:

ansible-playbook site.yml --limit "web:database"

This runs the playbook on hosts in both groups.

12. How do you configure an inventory for a HAProxy load balancer setup?

[load_balancers]
haproxy1 ansible_host=192.168.1.100

[web_servers]
web1 ansible_host=192.168.1.101
web2 ansible_host=192.168.1.102

[database]
db1 ansible_host=192.168.1.200

[load_balancers:vars]
ansible_user=haproxy

This ensures HAProxy is separate but can be managed.

13. How do you manage secrets in inventory?

I use Ansible Vault to encrypt secrets in group_vars/all.yml:

ansible-vault encrypt group_vars/all.yml

Now, Ansible prompts for a password when running a playbook.


14. How do you handle inventory for a rolling update?

I use serial in my playbook:

- hosts: web
serial: 2
tasks:
- name: Update packages
apt:
name: "*"
state: latest

This updates two servers at a time.

15. How do you manage inventory for a Kubernetes cluster?

I define a structured inventory:

all:
children:
masters:
hosts:
master1:
master2:
workers:
hosts:
worker1:
worker2:

Then, I use:

ansible-playbook -i inventory.yml k8s_setup.yml

This ensures I can manage Kubernetes nodes efficiently.

Final Thoughts

These are real-world inventory management scenarios. In an interview, I would explain my


thought process clearly and confidently, demonstrating practical experience.

Would you like me to create mock interview questions for you to practice?
15 Real-World Ansible Work Scenario Questions & Answers

Here are some practical work scenarios with first-person answers that you can use to prepare
for an interview.

1. How do I define and use variables in a playbook?

In my playbook, I define variables in the vars section or use external variable files for better
organization. For example, if I need to install a package dynamically, I do this:

- name: Install a package


hosts: all
vars:
package_name: "nginx"
tasks:
- name: Install package
ansible.builtin.yum:
name: "{{ package_name }}"
state: present

This ensures flexibility, allowing me to change package_name without modifying the task itself.

2. How do I set up host and group variables in inventory?

I structure my inventory using host_vars/ and group_vars/ directories. If I want all webservers to use
port 8080, I create group_vars/webservers.yaml:

web_port: 8080

Now, every webserver in this group will use this port in my tasks. This method helps me
organize configurations cleanly.

3. How do I handle different variables for different environments?

I create environment-specific variable files like group_vars/prod.yaml and group_vars/dev.yaml. When


running the playbook, I specify the environment:

ansible-playbook site.yml -e "env=prod"

Inside group_vars/prod.yaml:
database_host: db-prod.example.com

This ensures my playbooks deploy correctly in different environments.

4. How do I register a variable and use it in another task?

I use register to store the output of a command and reference it in another task.

- name: Check disk space


command: df -h /
register: disk_info

- name: Show disk usage


debug:
msg: "Disk space available: {{ disk_info.stdout }}"

This helps me dynamically capture and reuse command outputs.

5. How do I check if a file exists before performing an action?

I use the stat module and a when condition.

- name: Check if log file exists


stat:
path: /var/log/app.log
register: log_file

- name: Delete log if it exists


file:
path: /var/log/app.log
state: absent
when: log_file.stat.exists

This prevents errors if the file isn’t there.

6. How do I conditionally execute a task based on a variable?

I use the when keyword.

- name: Restart service if running


service:
name: nginx
state: restarted
when: ansible_facts['os_family'] == "RedHat"

This ensures that the service restart only happens on RedHat-based systems.

7. How do I store and use sensitive variables?

I use Ansible Vault to encrypt sensitive data:

ansible-vault encrypt secrets.yml

Then, I reference it in my playbook:

vars_files:
- secrets.yml

I decrypt it while running the playbook:

ansible-playbook site.yml --ask-vault-pass

This ensures sensitive data like passwords remain secure.

8. How do I access a fact in a playbook?

I use ansible_facts to access system information.

- name: Display OS details


debug:
msg: "This server is running {{ ansible_facts['os_family'] }}"

This helps me write OS-specific tasks.

9. How do I disable fact gathering if not needed?

If I don’t need facts, I disable gathering for performance improvement:

- name: Run without facts


hosts: all
gather_facts: no
tasks:
- name: Ping
ping:

This speeds up execution in cases where facts aren’t required.

10. How do I cache facts to improve performance?

I enable fact caching in ansible.cfg:

[defaults]
fact_caching = jsonfile
fact_caching_connection = /tmp/facts

This reduces the overhead of gathering facts on every run.

11. How do I pass extra variables at runtime?

I use the -e flag:

ansible-playbook deploy.yml -e "app_version=1.2.3"

Inside my playbook, I reference:

- name: Deploy application


debug:
msg: "Deploying version {{ app_version }}"

This allows me to dynamically specify values without modifying the playbook.

12. How do I override inventory variables dynamically?

I pass variables via the command line or use host_vars/. Command-line variables override
everything:

ansible-playbook site.yml -e "db_host=new-db.example.com"

This lets me override settings without changing files.

13. How do I conditionally set a variable?


I use set_fact based on conditions:

- name: Set web port


set_fact:
web_port: 8080
when: ansible_facts['os_family'] == "RedHat"

This ensures variables are set dynamically based on the system’s properties.

14. How do I create and use custom facts?

I create a custom fact file /etc/ansible/facts.d/custom.fact:

[custom]
env=staging

I access it in my playbook:

- name: Print custom fact


debug:
msg: "Environment: {{ ansible_facts['custom']['env'] }}"

This helps me define persistent system-specific variables.

15. How do I troubleshoot when a variable is not working as expected?

First, I check if the variable exists by using:

- name: Debug variables


debug:
var: my_variable

If it’s undefined, I verify:

 If it’s correctly defined in group_vars or host_vars


 If another variable is overriding it
 If I need to pass it via -e
 If there’s a typo in the variable name

If it’s still an issue, I run the playbook in verbose mode:

ansible-playbook myplaybook.yml -e "my_variable=test" -vvv


This provides detailed output to diagnose the problem.

Final Thoughts

With these real-world scenarios, I’m confident in handling variables, facts, and troubleshooting
issues in Ansible. By practicing these situations, I ensure smooth automation and deployment
workflows.

Here are 15 real-world Ansible playbook interview scenarios, answered in first person as if I
were responding in an interview.

1. How do you structure an Ansible playbook for managing multiple servers with
different roles?

I structure my playbooks using roles to keep things modular and organized. For example, I
might have roles like webserver, database, and load_balancer. Each role has its own tasks, handlers,
variables, and templates. A typical site-wide playbook looks like this:

- name: Configure Web Servers


hosts: webservers
roles:
- webserver

- name: Configure Database Servers


hosts: databases
roles:
- database

This ensures better reusability and separation of concerns across different server types.

2. A service restart is required after updating a configuration file. How do you


handle this efficiently?

I use handlers to ensure the service only restarts if the configuration file actually changes.

- name: Update Nginx configuration


template:
src: nginx.conf.j2
dest: /etc/nginx/nginx.conf
notify: Restart Nginx
handlers:
- name: Restart Nginx
service:
name: nginx
state: restarted

This avoids unnecessary restarts, improving efficiency.

3. How do you ensure a task runs only if certain conditions are met?

I use the when statement to apply conditionals based on host variables or facts.

Example: Only install Apache on Ubuntu servers.

- name: Install Apache on Ubuntu


apt:
name: apache2
state: present
when: ansible_facts['os_family'] == "Debian"

This ensures tasks execute only when appropriate.

4. How do you repeat a task for multiple items in Ansible?

I use loops to iterate over multiple values.

Example: Creating multiple users.

- name: Create users


user:
name: "{{ item }}"
state: present
loop:
- alice
- bob
- charlie

This efficiently handles repetitive tasks.

5. A task needs to retry until a certain condition is met. How do you do it?
I use the until directive to retry tasks until the condition succeeds.

Example: Waiting for a service to be active.

- name: Ensure service is running


shell: systemctl is-active nginx
register: result
until: result.stdout == "active"
retries: 5
delay: 10

This ensures robust task execution in unstable environments.

6. How do you handle errors in a playbook?

I use blocks with rescue and always to manage errors gracefully.

Example: If a task fails, take corrective action.

- block:
- name: Attempt risky command
command: /bin/false
rescue:
- name: Handle failure
debug:
msg: "The previous task failed, but we handled it!"
always:
- name: Ensure cleanup
debug:
msg: "This runs no matter what."

This makes playbooks more resilient.

7. How do you run a task on a different machine than the target host?

I use delegate_to to execute a task elsewhere.

Example: Copying a file from the Ansible control node to a remote host.

- name: Copy file from control node


copy:
src: /tmp/file.txt
dest: /home/user/file.txt
delegate_to: localhost
This ensures better control over where tasks execute.

8. How do you store sensitive data securely in Ansible?

I use Ansible Vault to encrypt sensitive files like passwords and keys.

Example: Encrypting a variables file.

ansible-vault encrypt vars.yml

Then, I reference it in the playbook:

vars_files:
- vars.yml

This ensures security best practices.

9. You need to execute a task on localhost but apply changes remotely. How do
you do it?

I use run_once: true with delegate_to: localhost.

Example: Generating a config file locally but copying it remotely.

- name: Generate config file


template:
src: config.j2
dest: /tmp/config
delegate_to: localhost
run_once: true

- name: Distribute config file


copy:
src: /tmp/config
dest: /etc/myapp/config

This avoids unnecessary repetitions.

10. How do you handle different OS distributions in a single playbook?


I use conditionals (when) based on ansible_facts.

Example: Installing the correct package manager.

- name: Install package based on OS


package:
name: nginx
state: present
when: ansible_facts['os_family'] in ["Debian", "RedHat"]

This ensures cross-platform compatibility.

11. How do you dynamically assign values to variables?

I use Jinja2 templating within playbooks.

Example: Assigning a hostname dynamically.

- name: Set hostname dynamically


hostname:
name: "{{ inventory_hostname }}"

This enhances automation flexibility.

12. How do you limit playbook execution to a subset of hosts?

I use the --limit flag when running the playbook.

Example: Running only on webservers.

ansible-playbook site.yml --limit webservers

This ensures targeted execution.

13. How do you fetch and use data from a previous task?

I use register to store task output and reference it later.

Example: Getting a command output and using it.


- name: Get system uptime
command: uptime
register: uptime_output

- name: Display uptime


debug:
msg: "System uptime is {{ uptime_output.stdout }}"

This improves automation control.

14. How do you make a playbook idempotent?

I ensure tasks don’t run unnecessarily by using modules instead of raw commands.

Example: Instead of running apt install nginx, I use:

- name: Ensure Nginx is installed


apt:
name: nginx
state: present

This avoids unnecessary changes.

15. How do you debug an Ansible playbook effectively?

I use verbosity flags, debug tasks, and check mode.

1. Verbose output:

ansible-playbook site.yml -vvv

2. Debug a variable:

- name: Print debug info


debug:
var: ansible_facts

3. Test without applying changes:

ansible-playbook site.yml --check

This ensures smooth debugging.


These are real-world interview scenarios that show deep Ansible understanding. Let me know
if you want more insights!

Here are 15 real-world Ansible work scenarios with answers in the first person, as if I were
responding in an interview.

1. You need to create a new role for setting up a web server. How do you proceed?

I start by creating the role structure using Ansible Galaxy:

ansible-galaxy init web_server

Then, I define tasks in tasks/main.yml to install and configure the web server, using
handlers/main.yml to restart the service when needed. I store configurations in templates/ and files/,
ensuring variables are parameterized in defaults/main.yml. I also document the role in README.md
for clarity.

2. A teammate hardcoded variables in tasks/main.yml. How do you fix this?

Hardcoding variables reduces flexibility, so I move them to defaults/main.yml or vars/main.yml. If


they are environment-specific, I use extra vars (-e flag) or inventory variables. For sensitive data,
I use Ansible Vault. This ensures the role is reusable and maintainable.

3. How do you handle dependencies between roles in a playbook?

If a role depends on another, I define it in meta/main.yml like this:

dependencies:
- role: common
- role: database
vars:
db_name: "app_db"

This ensures the common and database roles execute before the current role. I also verify that
dependencies are installed using requirements.yml.
4. How do you install and manage roles from Ansible Galaxy?

To install a role, I run:

ansible-galaxy install geerlingguy.nginx

For multiple roles, I define them in requirements.yml:

- name: geerlingguy.nginx
version: 3.0.0

Then, I install them with:

ansible-galaxy install -r requirements.yml

This keeps role management consistent across teams.

5. A role needs an update but you don’t want to break existing setups. What do
you do?

I create a new branch in Git and update the role there. I test the updated role in a staging
environment before merging it into the main branch. If it's an external role from Ansible Galaxy,
I specify a version in requirements.yml and gradually test updates before upgrading production
systems.

6. How do you make a role reusable across multiple environments?

I avoid hardcoding environment-specific values by using:

 defaults/main.yml for common defaults


 group_vars/ and host_vars/ for environment-specific configurations
 extra_vars (-e) when running a playbook
For example, in defaults/main.yml:

app_port: 8080

Then override it in group_vars/production.yml:

app_port: 80
7. How do you debug an issue where a role is failing?

I start by running the playbook with verbose mode:

ansible-playbook site.yml -vvv

I check logs and use the debug module to print variable values. If the issue is with a role variable,
I check precedence using:

ansible-playbook site.yml --list-vars

If needed, I use ansible-lint to check for errors in the role.

8. A role modifies a configuration file but doesn’t restart the service. How do you
fix this?

I add a handler in handlers/main.yml:

- name: Restart web server


service:
name: nginx
state: restarted

Then, in tasks/main.yml, I notify the handler:

- name: Update nginx config


template:
src: nginx.conf.j2
dest: /etc/nginx/nginx.conf
notify: Restart web server

This ensures the service restarts only if the config file changes.

9. How do you publish a role to Ansible Galaxy?

I ensure the role has a meta/main.yml with proper metadata, commit it to a public GitHub repo,
and then run:

ansible-galaxy role import my-github-username my-role

This makes it publicly available for others to install using:


ansible-galaxy install my-github-username.my-role

10. You need to use a role stored in a private Git repository. How do you install it?

I define the private role in requirements.yml:

- name: my_private_role
src: git+ssh://git@github.com/myorg/my_private_role.git
version: master

Then install it with:

ansible-galaxy install -r requirements.yml

For authentication, I ensure my SSH key is configured for Git access.

11. How do you manage role updates across multiple servers?

I version-control roles and use a CI/CD pipeline to test updates before deployment. In
requirements.yml, I pin role versions:

- name: my_role
version: "1.2.0"

Before updating, I test in a staging environment. Once validated, I roll out updates gradually
using Ansible’s serial execution:

- hosts: web_servers
serial: 2
roles:
- my_role

12. A role is taking too long to execute. How do you optimize it?

I check for inefficiencies by enabling profiling:

ANSIBLE_CALLBACKS_ENABLED=profile_tasks ansible-playbook site.yml

If tasks are redundant, I use conditionals (when:) and check mode (--check) to avoid unnecessary
changes. For loops, I replace with_items with loop for better performance.
13. How do you ensure role security when handling sensitive data?

I use Ansible Vault to encrypt sensitive variables:

ansible-vault encrypt secrets.yml

Then reference them in vars_files:

vars_files:
- secrets.yml

For passwords and API keys, I also consider Ansible lookup plugins like ansible.builtin.env to fetch
secrets from environment variables.

14. How do you test a role before using it in production?

I use Molecule, an Ansible testing framework. First, I initialize tests:

molecule init scenario --role-name my_role --driver-name docker

Then, I run tests:

molecule test

This ensures my role works correctly before deployment.

15. How do you organize multiple roles in a large project?

I structure my project like this:

ansible/
│── group_vars/
│── host_vars/
│── roles/
│ ├── common/
│ ├── web_server/
│ ├── database/
│── site.yml
│── inventory.ini

 common/ contains system-wide configurations.


 web_server/ and database/ are modular and reusable.
 Variables are stored in group_vars/ and host_vars/ to avoid duplication.
This makes managing multiple roles easier and more scalable.

Conclusion

These scenarios cover real-world Ansible challenges you might face at work. Mastering them
ensures you can confidently explain, troubleshoot, and optimize Ansible roles in any
environment.

15 Work Scenario Questions and Answers on Jinja2 and Flask Templates

1. How do you pass data from a Flask route to a Jinja2 template?

In my Flask route, I use render_template() to pass variables to the template. For example:

@app.route('/profile')
def profile():
user = {"name": "Alice", "age": 30}
return render_template("profile.html", user=user)

In profile.html, I access the data like this:

<p>Name: {{ user.name }}</p>


<p>Age: {{ user.age }}</p>

2. How do you loop through a list in a Jinja2 template?

If I pass a list from Flask:

fruits = ["Apple", "Banana", "Cherry"]


return render_template("index.html", fruits=fruits)

In the template, I iterate through it using {% for %}:

<ul>
{% for fruit in fruits %}
<li>{{ fruit }}</li>
{% endfor %}
</ul>
3. How do you conditionally display content in Jinja2?

I use {% if %} statements. If a user is an admin, I show an admin panel:

{% if user.is_admin %}
<a href="/admin">Go to Admin Panel</a>
{% else %}
<p>You do not have admin privileges.</p>
{% endif %}

4. How do you use filters in Jinja2?

Filters modify data before rendering. If I need to capitalize a name:

<p>Hello, {{ user.name|capitalize }}</p>

For lists, I count elements:

<p>Number of items: {{ items|length }}</p>

5. How do you handle missing variables in Jinja2?

I use the default filter to provide fallback values:

<p>Username: {{ user.username|default("Guest") }}</p>

If user.username is missing, it displays "Guest".

6. How do you reuse templates with inheritance?

I create a base.html with {% block %} placeholders:

<html>
<head>
<title>{% block title %}Default Title{% endblock %}</title>
</head>
<body>
<header>Site Header</header>
<main>
{% block content %}{% endblock %}
</main>
</body>
</html>
Then, I extend it in home.html:

{% extends "base.html" %}
{% block title %}Home Page{% endblock %}
{% block content %}
<h1>Welcome Home</h1>
{% endblock %}

7. How do you use macros to avoid repetition?

I create a macro in macros.html:

{% macro greet(name) %}
<p>Hello, {{ name }}!</p>
{% endmacro %}

Then, I import and use it:

{% import "macros.html" as macros %}


{{ macros.greet("Alice") }}

8. How do you use static files in Jinja2?

I link static files using url_for():

<link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}">

My folder structure is:

/static
styles.css
/templates
index.html

9. How do you prevent HTML injection (XSS) in Jinja2?

By default, Jinja2 escapes data. But if I need raw HTML, I use safe:

{{ user.bio|safe }}

Otherwise, if user.bio contains <script>, it’s rendered as plain text, preventing XSS.
10. How do you pass multiple values into a template?

I pass multiple variables like this:

return render_template("dashboard.html", username="Alice", notifications=5)

And access them in the template:

<p>Welcome, {{ username }}!</p>


<p>You have {{ notifications }} new notifications.</p>

11. How do you check if a list is empty in Jinja2?

I use {% if list %} because an empty list evaluates to False:

{% if users %}
<p>There are users.</p>
{% else %}
<p>No users found.</p>
{% endif %}

12. How do you break or skip an iteration inside a loop?

Using {% continue %} to skip and {% break %} to exit:

{% for user in users %}


{% if user == "Bob" %}
{% continue %}
{% endif %}
<p>{{ user }}</p>
{% endfor %}

If "Bob" is in users, he’s skipped.

13. How do you include one template inside another?

I use {% include %} for reusable components like a navbar:

{% include "navbar.html" %}

Now, navbar.html can be used in multiple pages.


14. How do you display the current year dynamically?

I use the now function with a filter:

<p>Copyright &copy; {{ now().year }}</p>

If Flask doesn’t have now(), I pass it from Python:

from datetime import datetime


return render_template("index.html", year=datetime.now().year)

Then, in Jinja2:

<p>Copyright &copy; {{ year }}</p>

15. How do you debug issues in Jinja2 templates?

I enable Flask’s debugging mode:

app.run(debug=True)

I also use {% set %} to inspect values:

{% set debug_value = user %}


<p>Debug: {{ debug_value }}</p>

For errors, Flask shows the traceback, which helps me pinpoint issues.

Final Thoughts

If I can answer these types of questions fluently, I’m well-prepared for real-world Flask and
Jinja2 challenges!

Here are 15 real-world Ansible work scenarios, answered in the first person as if I were
responding in an interview.

1. How would you ensure a specific package is installed on multiple servers?


I use the appropriate package module based on the OS, such as apt for Debian-based systems or
yum for RHEL-based systems. I ensure idempotency so that if the package is already installed,
Ansible won’t reinstall it unnecessarily.

Example Playbook:

- name: Ensure Apache is installed


hosts: web_servers
tasks:
- name: Install Apache on Debian
ansible.builtin.apt:
name: apache2
state: present
when: ansible_os_family == "Debian"

- name: Install Apache on RHEL


ansible.builtin.yum:
name: httpd
state: present
when: ansible_os_family == "RedHat"

2. How do you create a user and ensure it has sudo privileges?

I use the user module to create the user and the copy module to configure sudo access.

Example Playbook:

- name: Create an admin user


hosts: all
tasks:
- name: Create user "john"
ansible.builtin.user:
name: john
state: present
groups: sudo
append: yes

3. How do you copy a file to multiple servers and verify its integrity?

I use the copy module to distribute the file and stat to verify its checksum.

Example Playbook:

- name: Copy and verify a file


hosts: all
tasks:
- name: Copy configuration file
ansible.builtin.copy:
src: /local/path/config.cfg
dest: /etc/app/config.cfg
mode: '0644'

- name: Verify file exists


ansible.builtin.stat:
path: /etc/app/config.cfg
register: file_status

- debug:
msg: "File exists: {{ file_status.stat.exists }}"

4. How do you reboot a server only if a kernel update was applied?

I check the running kernel version against the installed version using shell and reboot only when
necessary.

- name: Reboot if kernel updated


hosts: all
tasks:
- name: Get running kernel version
ansible.builtin.command: uname -r
register: running_kernel
changed_when: false

- name: Get installed kernel version


ansible.builtin.command: rpm -q --last kernel | head -1 | awk '{print $2}'
register: installed_kernel
changed_when: false
when: ansible_os_family == "RedHat"

- name: Reboot if kernel changed


ansible.builtin.reboot:
when: running_kernel.stdout not in installed_kernel.stdout

5. How do you roll back a failed deployment?

I use block, rescue, and always to manage failure scenarios.

- name: Deploy application with rollback


hosts: app_servers
tasks:
- block:
- name: Deploy new version
ansible.builtin.copy:
src: new_version/
dest: /var/www/app
notify: Restart service

rescue:
- name: Rollback to previous version
ansible.builtin.copy:
src: backup_version/
dest: /var/www/app
notify: Restart service

always:
- name: Ensure service is running
ansible.builtin.service:
name: myapp
state: started

6. How do you gather system information from all servers?

I use the setup module to retrieve system facts.

- name: Gather system information


hosts: all
tasks:
- name: Collect facts
ansible.builtin.setup:

- name: Display hostname


debug:
msg: "This server's hostname is {{ ansible_hostname }}"

7. How do you manage multiple environments (dev, staging, prod)?

I use Ansible inventory files and variable precedence with group_vars and host_vars.

# inventory
[dev]
server1 ansible_host=192.168.1.10

[staging]
server2 ansible_host=192.168.1.20

[prod]
server3 ansible_host=192.168.1.30

And define environment-specific variables in group_vars/dev.yml, group_vars/staging.yml, etc.


8. How do you encrypt sensitive data in Ansible?

I use Ansible Vault to encrypt passwords and secrets.

ansible-vault encrypt secrets.yml


ansible-playbook playbook.yml --ask-vault-pass

9. How do you run a task only if a certain condition is met?

I use the when condition.

- name: Restart service if config changed


hosts: all
tasks:
- name: Copy configuration
ansible.builtin.copy:
src: app.conf
dest: /etc/app/app.conf
register: config_result

- name: Restart service


ansible.builtin.service:
name: myapp
state: restarted
when: config_result.changed

10. How do you use a loop to create multiple users?

I use the with_items or loop syntax.

- name: Create multiple users


hosts: all
tasks:
- name: Add users
ansible.builtin.user:
name: "{{ item }}"
state: present
loop:
- alice
- bob
- charlie

11. How do you fetch logs from a remote server?

I use the fetch module.


- name: Fetch logs from remote servers
hosts: all
tasks:
- name: Get log files
ansible.builtin.fetch:
src: /var/log/app.log
dest: ./logs/
flat: yes

12. How do you handle dynamic inventory?

I use inventory scripts or plugins like AWS EC2 inventory.

ansible-inventory -i aws_ec2.yml --list

13. How do you manage large numbers of servers efficiently?

I use Ansible Tower/AWX or delegate tasks with serial.

- name: Deploy in batches


hosts: all
serial: 10
tasks:
- name: Deploy application
ansible.builtin.shell: echo "Deploying"

14. How do you troubleshoot a failed playbook run?

I use -vvv for verbose output and check logs.

ansible-playbook playbook.yml -vvv

15. How do you write a custom Ansible module?

I write a Python script using AnsibleModule.

#!/usr/bin/python
from ansible.module_utils.basic import AnsibleModule

def main():
module = AnsibleModule(argument_spec={})
module.exit_json(changed=True, message="Custom module executed")

if __name__ == '__main__':
main()

Then use it in a playbook:

- name: Run custom module


hosts: localhost
tasks:
- name: Execute module
custom_module:
register: result

- debug:
msg: "{{ result.message }}"

Final Thoughts

These scenarios cover core Ansible concepts and real-world challenges. If you can confidently
explain these, you'll be well-prepared for any interview. Do you want me to simulate a live
interview with more complex follow-ups?

Here are 15 real-world work scenarios with first-person answers that will help you prepare for
an Ansible Vault-related interview:

1. A teammate accidentally committed a file with plaintext passwords to Git. How


would you fix this?

I would immediately remove the file from Git history using git filter-branch or git rebase to ensure
the secrets don’t persist. Then, I’d rotate the exposed credentials and re-encrypt them using
Ansible Vault. Going forward, I’d enforce Git hooks or a .gitignore rule to prevent secrets from
being committed.

2. You need to securely store API keys for multiple environments (dev, staging,
production). How would you handle this in Ansible?

I would create separate vault-encrypted files for each environment, such as:

group_vars/
├── dev/vault.yml
├── staging/vault.yml
├── production/vault.yml
Then, I’d reference them dynamically in the playbook using vars_files, ensuring only the relevant
secrets are used based on the environment.

3. How do you ensure that Ansible Vault passwords aren’t stored in plaintext on
disk?

I’d store the vault password in an environment variable instead of a plaintext file. For CI/CD
pipelines, I’d use a secure secret manager like AWS Secrets Manager or HashiCorp Vault to
dynamically inject passwords at runtime.

4. Your manager asks you to share an encrypted Ansible Vault file with another
team without exposing the secret. How do you do it?

I would rekey the vault file to use a new password that I share securely with the other team:

ansible-vault rekey vault.yml

This allows them to access the secrets without knowing our main vault password.

5. How would you automate Ansible Vault decryption in a Jenkins pipeline?

I’d store the vault password in Jenkins credentials and pass it as a variable:

ANSIBLE_VAULT_PASSWORD=$(cat /path/to/jenkins/secret)
ansible-playbook deploy.yml --vault-password-file <(echo "$ANSIBLE_VAULT_PASSWORD")

This prevents the password from being exposed in logs.

6. What if a junior engineer forgets the Ansible Vault password and can’t run
playbooks?

If they don’t have access, I’d guide them to request it from a secure password manager like
Bitwarden or 1Password. If the password is completely lost, we’d need to recreate the
encrypted files, as Ansible Vault does not provide recovery options.
7. A playbook fails because it can’t decrypt a vault file. What are the possible
reasons?

I’d check the following:

1. Ensure the correct vault password is being used.


2. Verify that the file was encrypted with ansible-vault and not corrupted.
3. Check if the playbook references the correct vars_files containing the encrypted data.

8. How do you handle multiple Ansible Vault passwords in a playbook?

I’d use vault IDs to assign different passwords to different files:

ansible-playbook deploy.yml --vault-id dev@dev_vault_pass.txt --vault-id prod@prod_vault_pass.txt

This allows fine-grained control over which secrets are used.

9. What security measures would you take to protect Ansible Vault files?

I would:

 Restrict access using file permissions (chmod 600 vault.yml).


 Store vault passwords securely in secret management tools.
 Limit user access to only those who need it.
 Implement audit logging to track who accesses secrets.

10. You are onboarding a new team member. How do you securely share the
Ansible Vault password?

I’d use a secure password manager (e.g., Bitwarden, 1Password, or HashiCorp Vault) to share
the password. I’d also ensure they understand best practices and policies around handling
secrets.

11. What happens if two engineers encrypt the same file with different passwords?
The file becomes unreadable unless you have the exact password used for encryption. To
prevent this, I’d enforce a shared vault password file or use a vault rekeying process.

12. Your team uses both Ansible Vault and HashiCorp Vault. When would you use
each?

I’d use Ansible Vault for encrypting static data like configuration files. I’d use HashiCorp Vault
for dynamic secrets that need on-demand access (e.g., ephemeral database credentials).

13. How would you migrate existing plaintext secrets into Ansible Vault?

I’d:

1. Create an encrypted vault file (ansible-vault create secrets.yml).


2. Move the plaintext secrets into it and encrypt them.
3. Verify decryption using ansible-vault view secrets.yml.
4. Remove the old plaintext files and ensure they are wiped from Git history.

14. A senior engineer suggests using GPG instead of Ansible Vault for secrets. How
would you respond?

I’d acknowledge that GPG is a good alternative, but I’d explain that Ansible Vault is built-in,
integrates well with playbooks, and provides simpler management. If the organization already
uses GPG, we could explore ansible-vault --vault-id gpg@... for integration.

15. How would you handle a situation where a vault-encrypted playbook is running
in production but fails due to decryption errors?

I’d:

1. Check logs to see if the correct vault password was used.


2. Ensure the vault file is not corrupted by trying to decrypt it manually.
3. Verify access permissions on the encrypted files.
4. Re-encrypt the vault file if necessary (ansible-vault rekey).
5. If needed, I’d roll back to a previous working version while troubleshooting.
Final Thoughts

These scenarios cover a broad range of challenges you might face when handling secrets with
Ansible Vault in the real world. If you can confidently explain these, you’ll be well-prepared for
any interview!

Would you like me to add more complex troubleshooting cases?

15 Work Scenarios on Ansible Collections (With First-Person Answers)

1. A team member installed a collection, but Ansible still says the module is
missing. What do you do?

I first check if the collection is installed by running:

ansible-galaxy collection list

If it’s not listed, I confirm the installation path using ansible-config dump | grep COLLECTIONS_PATHS.
If the module is still not recognized, I check if it requires FQCN (Fully Qualified Collection Name)
in the playbook.
For example, instead of:

- name: Use a module


my_module:

I ensure it’s called properly:

- name: Use a module


my_namespace.my_collection.my_module:

2. You need to install multiple collections for a project. How do you do it


efficiently?

Instead of installing collections manually, I create a requirements.yml file:

collections:
- name: community.general
- name: ansible.utils
version: ">=1.0.0,<2.0.0"
Then, I install them all at once using:

ansible-galaxy collection install -r requirements.yml

3. You are developing a custom module inside a collection. How do you test it?

I place my module in plugins/modules/ inside my collection. To test, I use:

ansible-test sanity --docker

For functionality, I create a test playbook:

- hosts: localhost
tasks:
- name: Run custom module
my_namespace.my_collection.my_module:
param1: "test"

Then, I execute it with:

ansible-playbook test.yml -vvv

4. Your team wants to distribute a collection internally without Ansible Galaxy.


What do you do?

I first build the collection:

ansible-galaxy collection build

Then, I host it on a private repository (like an Artifactory or GitHub). To install, I update


requirements.yml:

collections:
- name: my_namespace.my_collection
source: https://internal-repo.com/my_collection.tar.gz

Then install it with:

ansible-galaxy collection install -r requirements.yml

5. A new collection update breaks your playbooks. How do you fix it?

I check the installed version:


ansible-galaxy collection list

Then, I specify a stable version in requirements.yml:

collections:
- name: community.general
version: "5.5.0"

I uninstall the latest version and reinstall the stable one:

ansible-galaxy collection uninstall community.general


ansible-galaxy collection install -r requirements.yml

6. How do you ensure all playbooks use a specific collection version?

I enforce version control by specifying versions in requirements.yml and locking them in ansible.cfg:

[defaults]
collections_paths = ./collections

7. How do you use a custom filter plugin from a collection in a playbook?

I place the plugin in plugins/filter/ and reference it in a Jinja2 template:

- hosts: localhost
tasks:
- name: Use custom filter
debug:
msg: "{{ 'hello' | my_namespace.my_collection.custom_filter }}"

8. Your collection needs dependencies from another collection. How do you handle
it?

I define dependencies in galaxy.yml:

dependencies:
ansible.utils: ">=1.0.0"

Then, when the collection is installed, it pulls dependencies automatically.


9. You need to use an older Ansible version, but your collection requires a newer
one. What do you do?

I check meta/runtime.yml inside the collection to see the required version. If upgrading Ansible
isn’t an option, I look for an older collection version that supports my Ansible version:

ansible-galaxy collection list | grep my_namespace

Then, I install a compatible version.

10. How do you automate collection installation for CI/CD pipelines?

I add requirements.yml to the repository and install it in the CI/CD pipeline before running
playbooks:

ansible-galaxy collection install -r requirements.yml

This ensures all dependencies are present in every deployment.

11. Your collection has a bug. How do you debug it?

I run the playbook in verbose mode:

ansible-playbook test.yml -vvv

If it’s a module issue, I insert debug messages in the Python code using:

module.warn("Debug: Variable value is " + str(var))

Then, I test with ansible-test to isolate the issue.

12. How do you migrate from roles to collections?

I move roles into roles/ inside the collection structure, update galaxy.yml, and reference them
using FQCN:

roles:
- my_namespace.my_collection.my_role
13. A module from a collection is not working as expected. How do you fix it?

I check the documentation:

ansible-doc -t module my_namespace.my_collection.my_module

If the issue persists, I enable debugging (-vvv), check logs, and review module source code.

14. How do you structure a collection to be reusable across projects?

I follow best practices:

 Keep custom modules, roles, and plugins well-documented.


 Use meta/runtime.yml for compatibility.
 Include example playbooks in examples/.

15. How do you handle breaking changes when upgrading collections?

I test upgrades in a staging environment before production. If a new version breaks


compatibility, I pin the older version in requirements.yml until a fix is implemented.

This covers real-world troubleshooting and best practices. Let me know if you want more!

15 Real-World Ansible Performance Optimization Scenarios

Here are 15 practical work scenarios where I might need to optimize Ansible’s performance,
with answers in the first person to reflect how I would handle them in a real job.

Scenario 1: Slow Playbook Execution with Many Hosts

Q: My Ansible playbook takes too long when running on 500+ hosts. How can I speed it up?

A: The first thing I check is the number of forks in my Ansible configuration. By default, Ansible
only runs 5 hosts in parallel. I increase this by modifying ansible.cfg:

[defaults]
forks = 50

This allows me to execute tasks on 50 hosts at a time, reducing the overall execution time.

Scenario 2: Long-Running Tasks Blocking Other Tasks

Q: Some tasks, like database backups, take a long time and block the playbook. How do I
prevent this?

A: I use asynchronous execution so that Ansible doesn't wait for the task to finish. For example:

- name: Run database backup in the background


command: /usr/local/bin/db_backup.sh
async: 3600
poll: 0

This lets the task run in the background while the playbook moves on.

Scenario 3: Need to Check Status of an Async Task Later

Q: How do I check if an asynchronous task has completed?

A: I store the job ID when running the task and later check its status using async_status:

- name: Start a long-running process


command: /usr/local/bin/big_task.sh
async: 1800
poll: 0
register: async_job

- name: Check task status


async_status:
jid: "{{ async_job.ansible_job_id }}"
register: job_result
until: job_result.finished
retries: 10
delay: 30

This way, I can periodically check if the task is complete.

Scenario 4: Too Many SSH Connections Slowing Playbook


Q: My playbook takes a long time due to repeated SSH connections. How can I optimize this?

A: I enable Accelerated Mode in Ansible by modifying ansible.cfg:

[defaults]
transport = accelssh

This keeps persistent SSH connections, reducing handshake overhead.

Scenario 5: Reducing Fact Gathering Overhead

Q: My playbook is slow because it gathers facts every time. How do I optimize this?

A: I enable fact caching so that Ansible doesn’t repeatedly gather facts:

[defaults]
fact_caching = jsonfile
fact_caching_connection = /tmp/ansible_facts
fact_caching_timeout = 600

This significantly speeds up execution, especially on large inventories.

Scenario 6: Avoiding Playbook Failures Due to Slow Hosts

Q: Some of my servers are slower than others and cause playbook timeouts. How do I handle
this?

A: I increase the SSH timeout in ansible.cfg to allow slow hosts more time to respond:

[defaults]
timeout = 60

I also use async to let slower tasks run in the background.

Scenario 7: Optimizing Performance for a Large Inventory

Q: I have thousands of hosts. How do I optimize playbook execution?

A: I combine multiple techniques:


 Increase forks (e.g., forks = 100).
 Use fact caching to avoid redundant fact gathering.
 Use accelerated mode for persistent connections.
 Use batch execution to limit the number of simultaneous hosts:
 serial: 50

Scenario 8: Avoiding Overloaded Ansible Control Node

Q: My Ansible control node is hitting CPU limits. What do I do?

A: I reduce the number of forks if it's too high.


I also offload intensive tasks to remote hosts instead of running them on the control node.

Scenario 9: Ensuring Playbooks Don’t Overwhelm Remote Hosts

Q: I want to limit how many hosts Ansible runs on at once.

A: I use the serial keyword to process hosts in smaller batches:

- hosts: all
serial: 20

This ensures Ansible processes only 20 hosts at a time instead of overloading them.

Scenario 10: Parallel Execution of Independent Tasks

Q: How do I run multiple tasks in parallel on the same host?

A: I use background execution with async:

- name: Run two commands in parallel


command: my_task_1.sh
async: 300
poll: 0

- name: Run another command in parallel


command: my_task_2.sh
async: 300
poll: 0
Scenario 11: Handling High-Latency Networks

Q: Some remote servers are in high-latency environments. How do I optimize for this?

A: I increase timeout and use persistent connections (accelerated mode or persistent SSH).
I also use fact_caching to avoid unnecessary network calls.

Scenario 12: Preventing Playbook Failures When a Few Hosts Fail

Q: How do I prevent one failing host from stopping the entire playbook?

A: I use ignore_errors: yes:

- name: Run a command even if some hosts fail


command: my_command.sh
ignore_errors: yes

And I use max_fail_percentage to continue execution even if some hosts fail.

Scenario 13: Running Playbooks Only on Changed Hosts

Q: How do I prevent Ansible from running on hosts that don’t need updates?

A: I use the changed_when condition and check if changes occurred before proceeding.

Scenario 14: Debugging a Slow Playbook

Q: How do I debug why my playbook is running slow?

A: I use ANSIBLE_DEBUG=True and -vvv verbosity to check where time is being spent.
I also check if fact gathering, SSH connections, or slow tasks are the bottlenecks.

Scenario 15: Running Playbooks Faster on a Cloud Environment

Q: How do I optimize Ansible for AWS or cloud environments?


A: I use:

 Dynamic inventories instead of static files.


 fact caching to avoid redundant API calls.
 parallel execution (forks) to speed up deployment.

Final Thoughts

By applying these techniques, I ensure my Ansible playbooks run efficiently, scale well, and
avoid unnecessary delays.

Here are 15 real-world Ansible troubleshooting scenarios with answers in first-person to help
you prepare for any interview.

1. A playbook keeps failing, but I don’t understand why. What do I do first?

I run the playbook with -vvv verbosity to get detailed logs.

ansible-playbook myplaybook.yml -vvv

This gives me full JSON output of module execution, helping me pinpoint the issue. If necessary,
I increase verbosity further (-vvvv) to see SSH details.

2. A variable is showing up as ‘undefined’. How do I troubleshoot?

I first check if the variable is actually defined.

- name: Debug my variable


debug:
var: my_var

If it’s missing, I check where it should be set (inventory, vars, group_vars, host_vars). If it's a fact, I
ensure gather_facts: yes is enabled.

3. I get a YAML syntax error. How do I fix it?

I use yamllint to find formatting errors.


yamllint myplaybook.yml

I also verify indentation and colons. YAML is strict, so I make sure everything is properly
formatted.

4. My task is failing, but I need the playbook to continue. What do I do?

I use ignore_errors: yes to allow execution to proceed even if a task fails.

- name: This task might fail


command: /bin/false
ignore_errors: yes

However, I use this cautiously because ignoring failures might hide real issues.

5. How do I fail a task only under specific conditions?

I use failed_when to define a custom failure condition.

- name: Fail if output does not contain "success"


shell: echo "error"
register: result
failed_when: "'success' not in result.stdout"

This ensures the task only fails when needed.

6. A playbook is running a command every time, even when it shouldn't. Why?

It’s not idempotent. I switch to a proper module instead of using command or shell.
Instead of this:

- name: Install nginx


command: apt install nginx -y

I use this:

- name: Ensure nginx is installed


apt:
name: nginx
state: present
Now, Ansible won’t run it if nginx is already installed.

7. I need to print all available variables for a host. How?

I use debug to print hostvars.

- name: Print all host variables


debug:
var: hostvars[inventory_hostname]

This helps me inspect all variables in scope.

8. My playbook fails with "UNREACHABLE!" when connecting to a remote host. How


do I fix it?

I check SSH connectivity manually.

ssh user@remote_host

If SSH fails, I check network/firewall settings. If Ansible is using the wrong user, I specify it in the
inventory:

[servers]
192.168.1.100 ansible_user=myuser ansible_ssh_private_key_file=~/.ssh/id_rsa

9. I need to run a playbook as another user but keep getting permission errors. What
do I do?

I ensure I use become: yes for privilege escalation.

- name: Run as root


become: yes
command: whoami

If sudo requires a password, I add ansible_become_password or use --ask-become-pass.


10. My playbook is taking too long. How do I speed it up?

I use forks to increase parallel execution.


In ansible.cfg:

[defaults]
forks = 50

I also optimize my playbook by limiting gather_facts: no if facts are unnecessary.

11. My playbook works on one server but fails on another. How do I troubleshoot?

I check the environment variables and OS differences.


I run:

ansible all -m setup

This gives me system facts, allowing me to compare differences.

12. A playbook fails with "no action detected in task". How do I fix it?

This usually means a module name is misspelled or missing.


For example, if I wrote:

- name: Install package


apt
name: nginx
state: present

I forgot a colon (:) after apt. Fixing it like this solves the issue:

- name: Install package


apt:
name: nginx
state: present

13. How do I retry failed hosts instead of rerunning the playbook from the start?

I use the --limit and --retry options.


First, I run the playbook:
ansible-playbook myplaybook.yml

If hosts fail, a .retry file is created. I rerun it only on failed hosts:

ansible-playbook myplaybook.yml --limit @/path/to/retry_file

14. How do I ensure a task runs even if the previous ones fail?

I use the always block.

- block:
- name: This might fail
command: /bin/false
rescue:
- name: Handle failure
debug:
msg: "Task failed, but we handled it."
always:
- name: Cleanup
debug:
msg: "This always runs."

This ensures cleanup tasks always execute.

15. I need to debug a Jinja2 template rendering issue. How do I do it?

I use debug to inspect how variables are rendered.

- name: Check variable rendering


debug:
msg: "{{ my_variable }}"

If there's a syntax issue, I check for missing {% or {{ delimiters. I also use:

ansible localhost -m debug -a "msg={{ my_variable }}"

To test it quickly.

Final Thoughts

These scenarios prepare me for real-world Ansible debugging. I now feel confident answering
interview questions and troubleshooting playbooks effectively.
Want more scenarios or deeper explanations on any of them?

Here are 15 real-world work scenarios related to Ansible Tower/AWX, along with first-person
responses demonstrating expertise and problem-solving skills.

1. A Job Template Fails Due to a Missing Inventory. What Do You Do?

Scenario: My job template failed because the inventory is missing.

Answer:
I navigate to Resources > Inventories to verify if the inventory exists. If it’s missing, I recreate it
or check if it was accidentally deleted. If it exists but isn't assigned to the job template, I edit
the template and select the correct inventory. I also ensure that the inventory syncs properly,
especially if it's a dynamic inventory from a cloud provider like AWS.

2. How Do You Securely Store AWS Credentials for Automation?

Scenario: My team needs to automate AWS resource provisioning without exposing


credentials.

Answer:
I create a credential in Tower/AWX under Resources > Credentials and select Amazon Web
Services (AWS) as the type. I enter the Access Key ID and Secret Access Key securely. Then, I
ensure that only authorized users and job templates have access to these credentials using
RBAC permissions.

3. How Do You Set Up a Scheduled Job to Run Weekly?

Scenario: My team needs to schedule a job to run every Sunday at 2 AM.

Answer:
I go to Templates > Job Templates, select the required job, and navigate to the Schedules tab. I
create a new schedule, name it "Weekly Maintenance," and set it to run every Sunday at 2:00
AM in the appropriate time zone. I verify that the job is enabled and test it with a manual
execution.
4. A Job is Running Too Slowly. How Do You Optimize It?

Scenario: My playbook execution is taking too long.

Answer:
I check the job output logs to identify slow tasks. Then, I optimize it by:

1. Enabling forks (parallel execution) in Settings > Jobs.


2. Using async and poll for long-running tasks.
3. Running tasks conditionally to avoid unnecessary execution.
4. Checking host connectivity to avoid network delays.

5. A User Complains They Can’t Run a Job. How Do You Fix It?

Scenario: A user reports that they can’t launch a job template.

Answer:
I first check the user’s role under Users & Teams to ensure they have the right permissions. If
they lack access, I modify the RBAC settings and grant them the appropriate role (e.g., Job
Executor). If they have access but still can’t run it, I review the job template permissions to
ensure it's not restricted.

6. How Do You Integrate Tower Logs with an External System Like Splunk?

Scenario: The security team wants job logs forwarded to Splunk.

Answer:
I go to Settings > System > Logging and configure external logging by enabling "Enable External
Logging". I enter the Splunk HTTP Event Collector (HEC) URL, choose JSON format, and set an
authentication token. I then test it by executing a job and confirming that logs appear in Splunk.

7. How Do You Deploy a Playbook to Specific Hosts Only?

Scenario: I need to run a playbook on only a subset of hosts.

Answer:
I edit the job template and use the Limit field to specify hostnames or groups (e.g.,
"web_servers"). If hosts aren’t pre-grouped, I use an extra variable like -e
target_hosts="server1,server2", and modify the playbook to respect this variable.

8. A Job Fails Due to a Vault Password Error. What Do You Do?

Scenario: My job fails because it requires an Ansible Vault password.

Answer:
I check if the correct Vault credential is assigned to the job template. If missing, I add it under
Resources > Credentials and associate it with the job. If it's a one-time execution, I enable Vault
password prompting instead of storing it.

9. How Do You Prevent Unauthorized Users from Viewing Sensitive Logs?

Scenario: My logs contain sensitive information like passwords.

Answer:
I ensure that job templates using sensitive credentials have RBAC restrictions to limit log
access. I also configure Ansible no_log: true in playbooks to hide sensitive outputs from logs.

10. How Do You Handle Dynamic Inventory for AWS EC2 Instances?

Scenario: My AWS instances change frequently, and I need an updated inventory.

Answer:
I create a Dynamic Inventory Source under Resources > Inventories and select Amazon EC2 as
the source. I configure authentication using stored AWS credentials and set a sync schedule.
This ensures the inventory updates automatically when new instances are created.

11. How Do You Trigger an Ansible Tower Job from Jenkins?

Scenario: I need to trigger an AWX job from a Jenkins pipeline.

Answer:
I use the Ansible Tower plugin for Jenkins or trigger jobs via the REST API by making a POST
request:
curl -k -X POST https://awx.example.com/api/v2/job_templates/10/launch/ \
-H "Authorization: Bearer mytoken"

I store the API token securely in Jenkins credentials.

12. A Job Fails with an SSH Permission Denied Error. What’s the Fix?

Scenario: My job fails because it can’t SSH into hosts.

Answer:
I verify that the SSH key or password is correctly stored in AWX under Credentials. If using an
SSH key, I ensure it’s added to authorized_keys on the target servers. I also test connectivity
manually:

ssh -i ~/.ssh/id_rsa ansible@remote_host

13. How Do You Notify the Team of Job Failures?

Scenario: My team wants email alerts when a job fails.

Answer:
I configure Notifications in AWX under Settings > Notifications and add an email notification
provider with SMTP details. Then, I link it to job templates so the team gets emails when jobs
fail.

14. How Do You Control Which Users Can Use a Specific Credential?

Scenario: Only the DevOps team should use a specific AWS credential.

Answer:
I go to Resources > Credentials, select the AWS credential, and modify the RBAC settings. I
assign it only to the DevOps team and remove access from other users. This ensures only
authorized users can access it.

15. How Do You Debug a Job That Fails Randomly?

Scenario: A playbook sometimes fails but works other times.


Answer:
I analyze the job logs for error patterns. If the issue is intermittent, I:

 Enable verbosity (-vvv or -vvvv) in the job template for deeper logs.
 Check network connectivity between AWX and target hosts.
 Run the playbook manually with --step to identify failing tasks.
 Use retry files (ansible-playbook playbook.yml --limit @failed_hosts.retry).

Final Thoughts

These real-world troubleshooting scenarios will prepare you for almost any Ansible
Tower/AWX interview or work challenge. Would you like me to generate hands-on lab
exercises for practice?

Here are 15 real-world Ansible network automation scenarios with answers in the first person,
simulating how I'd respond in a work environment or interview.

1. A critical network outage has occurred. How would you use Ansible to diagnose
the issue?

I would first use Ansible to gather real-time data from all affected network devices. I’d run a
show commands playbook to collect interface status, routing tables, and logs.

- name: Collect device status


hosts: network_devices
tasks:
- name: Run diagnostic commands
cisco.ios.ios_command:
commands:
- show ip interface brief
- show logging
- show ip route
register: diagnostics_output

- name: Display results


debug:
var: diagnostics_output.stdout_lines

Once I have the data, I’d analyze it to identify down interfaces, high CPU usage, or routing
issues.
2. You need to configure VLANs on 50 switches. How would you do it efficiently?

I’d write an Ansible playbook to apply VLAN configurations in bulk, ensuring consistency across
all devices.

- name: Configure VLANs


hosts: switches
tasks:
- name: Create VLANs
cisco.ios.ios_config:
lines:
- vlan 10
- name Sales
- vlan 20
- name IT

This ensures all switches get the same VLAN setup without manual intervention.

3. A security team asks you to audit all network devices for SSH configurations.
How would you do it?

I’d use Ansible to check SSH settings on all devices and store the output for auditing.

- name: Audit SSH Configurations


hosts: network_devices
tasks:
- name: Check SSH settings
cisco.ios.ios_command:
commands: show running-config | include ssh
register: ssh_config

- name: Save output


copy:
content: "{{ ssh_config.stdout_lines }}"
dest: "/reports/ssh_audit_{{ inventory_hostname }}.txt"

This allows me to review SSH settings across all devices.

4. A new branch office is opening. How would you provision network devices using
Ansible?

I’d prepare a playbook to configure interfaces, routing, VLANs, and users. Then, I’d deploy it to
the new devices.
- name: Provision New Branch Devices
hosts: new_branch_routers
tasks:
- name: Configure interfaces
cisco.ios.ios_config:
lines:
- interface GigabitEthernet0/1
- ip address 192.168.10.1 255.255.255.0
- no shutdown

After deployment, I’d validate connectivity with ping commands.

5. A firmware update is needed on 100 devices. How would you handle it?

I’d use Ansible to automate firmware uploads and reboots in batches to minimize downtime.

- name: Upgrade Firmware


hosts: network_devices
tasks:
- name: Upload firmware
cisco.ios.ios_command:
commands:
- copy tftp://192.168.1.100/new_firmware.bin flash:
- reload

I’d schedule updates during maintenance windows and verify each device post-upgrade.

6. You need to back up all device configurations. How would you do it?

I’d create a playbook to fetch and store the configurations automatically.

- name: Backup Configurations


hosts: network_devices
tasks:
- name: Fetch running config
cisco.ios.ios_command:
commands: show running-config
register: config_output

- name: Save config


copy:
content: "{{ config_output.stdout_lines }}"
dest: "/backups/{{ inventory_hostname }}_config.txt"

This ensures I always have up-to-date backups.


7. Your team needs to apply an ACL to restrict traffic. How would you do it?

I’d push an ACL configuration using Ansible to enforce security policies.

- name: Apply ACL


hosts: firewalls
tasks:
- name: Configure ACL
cisco.ios.ios_config:
lines:
- access-list 101 deny tcp any any eq 23
- access-list 101 permit ip any any

Then, I’d verify it with show access-lists.

8. A router is missing an OSPF configuration. How would you fix it?

I’d deploy an OSPF configuration playbook to the affected router.

- name: Configure OSPF


hosts: routers
tasks:
- name: Set OSPF
cisco.ios.ios_config:
lines:
- router ospf 1
- network 192.168.1.0 0.0.0.255 area 0

Then, I’d confirm OSPF neighbors with show ip ospf neighbor.

9. You need to check interface errors on all devices. How would you do it?

I’d use Ansible to fetch interface statistics and analyze error counts.

- name: Check Interface Errors


hosts: switches
tasks:
- name: Show errors
cisco.ios.ios_command:
commands: show interfaces | include error
register: error_output
- name: Display results
debug:
var: error_output.stdout_lines

If errors are high, I’d investigate faulty cables or duplex mismatches.

10. How would you validate network changes before applying them?

I’d use Ansible’s check mode to simulate changes without applying them:

ansible-playbook config.yml --check

This ensures I don’t break anything unintentionally.

11. How would you manage network device credentials securely in Ansible?

I’d use Ansible Vault to encrypt credentials:

ansible-vault encrypt credentials.yml

Then, in my playbooks, I’d reference it:

ansible_user: "{{ vault_username }}"


ansible_password: "{{ vault_password }}"

12. How would you roll back a bad configuration change?

I’d first retrieve the last backup, then restore it using Ansible.

- name: Restore Config


hosts: network_devices
tasks:
- name: Apply previous config
cisco.ios.ios_config:
src: "/backups/{{ inventory_hostname }}_config.txt"

13. How would you verify if all devices have the same NTP configuration?

I’d run a playbook to check NTP settings:


- name: Verify NTP
hosts: network_devices
tasks:
- name: Show NTP status
cisco.ios.ios_command:
commands: show ntp status
register: ntp_status

- name: Display NTP sync


debug:
var: ntp_status.stdout_lines

14. How would you automate user account management on network devices?

I’d create an Ansible playbook to add or remove users.

- name: Add User


hosts: network_devices
tasks:
- name: Create admin user
cisco.ios.ios_config:
lines:
- username admin privilege 15 password mysecurepassword

15. How would you troubleshoot slow network performance using Ansible?

I’d check interface statistics, CPU usage, and routing tables:

- name: Diagnose Performance Issues


hosts: routers
tasks:
- name: Run diagnostics
cisco.ios.ios_command:
commands:
- show interfaces
- show processes cpu
- show ip route
register: diag_output

- name: Display results


debug:
var: diag_output.stdout_lines

This would help identify bottlenecks.


These real-world scenarios will prepare you for any interview question on Ansible for network
automation! Let me know if you want more deep dives.

Here are 15 real-world work scenario questions with detailed first-person answers to help you
master Ansible for Cloud Automation in interviews.

1. You need to provision an AWS EC2 instance and install Apache on it. How would
you do it?

I would use Terraform to provision the EC2 instance and Ansible to configure Apache. First, I’d
write a Terraform script to create the instance. Then, I’d use an Ansible playbook to install
Apache and start the service.

Terraform script (ec2.tf):


provider "aws" {
region = "us-east-1"
}

resource "aws_instance" "web" {


ami = "ami-12345678"
instance_type = "t2.micro"
key_name = "my-key"

tags = {
Name = "web-server"
}
}

Ansible playbook (install_apache.yml):


- name: Install Apache on EC2
hosts: webserver
become: yes
tasks:
- name: Install Apache
yum:
name: httpd
state: present

- name: Start Apache


service:
name: httpd
state: started
enabled: yes

I would then run Terraform to deploy the server and Ansible to configure it.
2. How would you automate the provisioning of an S3 bucket with Ansible?

I would use the amazon.aws.s3_bucket module to create an S3 bucket. Here’s how I’d do it:

- name: Create S3 Bucket


hosts: localhost
tasks:
- name: Create S3 bucket
amazon.aws.s3_bucket:
name: my-ansible-bucket
state: present
region: us-east-1

I would then run the playbook, and it would create the bucket automatically.

3. What would you do if your Ansible playbook fails due to missing AWS
credentials?

I would first check if my AWS credentials are set up correctly in ~/.aws/credentials or as


environment variables. If they are missing, I would configure them:

[default]
aws_access_key_id=YOUR_ACCESS_KEY
aws_secret_access_key=YOUR_SECRET_KEY
region=us-east-1

If credentials are present, I’d verify that boto3 and botocore are installed:

pip install boto3 botocore

Finally, I would enable verbose mode (-vvvv) in Ansible to debug further.

4. You need to deploy a virtual machine on Azure using Ansible. What would you
do?

I would first install the Azure Ansible Collection:

ansible-galaxy collection install azure.azcollection

Then, I would create a playbook like this:

- name: Deploy Azure VM


hosts: localhost
tasks:
- name: Create resource group
azure.azcollection.azure_rm_resourcegroup:
name: myResourceGroup
location: eastus

- name: Create VM
azure.azcollection.azure_rm_virtualmachine:
name: myVM
resource_group: myResourceGroup
vm_size: Standard_B1s
admin_username: azureuser
admin_password: "P@ssword123!"

After running the playbook, a new VM would be created on Azure.

5. How would you configure an IAM role for an EC2 instance using Ansible?

I’d create an IAM role using the amazon.aws.iam_role module:

- name: Create IAM role


hosts: localhost
tasks:
- name: Create IAM role
amazon.aws.iam_role:
name: MyEC2Role
assume_role_policy_document: "{{ lookup('file', 'assume_role.json') }}"
state: present

Then, I’d attach it to an EC2 instance.

6. What’s your approach if an Ansible playbook takes too long to execute?

I would:

1. Enable parallel execution using forks in ansible.cfg.


2. Limit unnecessary tasks by using conditionals.
3. Use async mode for long-running tasks.

- name: Run async task


command: long-running-script.sh
async: 600
poll: 0
7. How do you handle secrets in Ansible for cloud automation?

I use Ansible Vault to encrypt sensitive data:

ansible-vault encrypt aws_secrets.yml

Then, I reference the secrets in playbooks:

aws_access_key: "{{ lookup('file', 'aws_secrets.yml') }}"

8. How would you delete unused AWS resources with Ansible?

I’d use state: absent to remove resources. Example for EC2:

- name: Terminate EC2 Instance


hosts: localhost
tasks:
- name: Terminate instance
amazon.aws.ec2_instance:
instance_ids: i-12345678
state: absent

9. How do you ensure an Ansible playbook is idempotent?

I:

 Use state: present instead of imperative commands.


 Avoid using command: unless necessary.
 Use changed_when: false for non-idempotent tasks.

10. What’s the difference between Terraform and Ansible?

Terraform is for provisioning infrastructure, while Ansible is for configuring it. I use Terraform
to create resources (EC2, S3, VPC) and Ansible to configure them (installing software, updates).

11. How would you scale an application using Ansible on AWS?

I would use Auto Scaling Groups with an Ansible playbook to configure instances dynamically.
12. How do you automate Kubernetes deployments in the cloud with Ansible?

I’d use community.kubernetes.k8s module:

- name: Deploy app to Kubernetes


hosts: localhost
tasks:
- name: Create a deployment
community.kubernetes.k8s:
state: present
definition: "{{ lookup('file', 'deployment.yaml') }}"

13. How do you schedule Ansible playbooks to run automatically?

I’d use cron jobs or Ansible Tower/AWX. Example cron job:

0 2 * * * ansible-playbook deploy.yml

14. What’s your approach to troubleshooting Ansible errors in cloud automation?

I:

1. Use -vvvv verbosity (ansible-playbook deploy.yml -vvvv).


2. Check logs and output messages.
3. Run ansible-lint to detect syntax issues.
4. Debug using debug module:

- debug:
msg: "Variable value is {{ my_var }}"

15. How would you optimize an Ansible playbook for large-scale cloud
environments?

I would:

 Use dynamic inventory for large infrastructure.


 Run tasks in parallel (forks=10 in ansible.cfg).
 Use Ansible roles for modularity.
 Optimize loops with with_items.
These 15 real-world scenarios cover everything from AWS, Azure, GCP automation,
troubleshooting, scaling, and best practices. With these, I can confidently tackle any Ansible
cloud automation interview question!

Here are 15 real-world work scenarios with answers in the first person to help you confidently
answer interview questions about CI/CD with Ansible.

1. How would you integrate Ansible into an existing Jenkins pipeline?

Scenario: A company already has Jenkins pipelines for deployments but wants to introduce
Ansible for automation.

Answer:
I would start by installing the Ansible plugin in Jenkins and ensuring Ansible is installed on the
Jenkins server. Then, I would modify the Jenkins pipeline to include an Ansible playbook
execution step.
For example, I would add a stage like this in the Jenkinsfile:

stage('Run Ansible Playbook') {


steps {
ansiblePlaybook credentialsId: 'ansible-ssh-key',
inventory: 'inventory.ini',
playbook: 'playbook.yml'
}
}

This ensures that Ansible runs automatically after the code is built and tested.

2. What would you do if an Ansible playbook failed during a deployment?

Scenario: A deployment fails because an Ansible playbook throws an error.

Answer:
First, I would check the Jenkins logs or CI/CD logs to identify the exact error message. If it's a
syntax issue, I would validate the playbook using:

ansible-playbook --syntax-check playbook.yml

If it’s a connectivity issue, I would use:

ansible all -m ping -i inventory.ini


For failed tasks, I would use --start-at-task to rerun only from the failure point instead of
executing everything again.

3. How would you securely store SSH keys for Ansible in GitHub Actions?

Scenario: A company wants to avoid exposing SSH keys in GitHub workflows.

Answer:
I would use GitHub Secrets to store the SSH private key securely. In the GitHub Actions
workflow, I would modify the steps like this:

- name: Set up SSH key


run: |
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa

This ensures the key is available only during workflow execution and is securely encrypted.

4. How do you test an Ansible role before deploying it?

Scenario: A new Ansible role is added, and it must be tested before deployment.

Answer:
I would use Molecule for testing. First, I would initialize a Molecule scenario:

molecule init scenario --driver-name docker

Then, I would run molecule test, which does the following:

 Creates a test environment using Docker.


 Executes the playbook to ensure it works.
 Runs assertions to verify correct behavior.

5. How do you handle a situation where an Ansible playbook modifies production


infrastructure by mistake?

Scenario: A playbook was mistakenly applied to production instead of staging.


Answer:
I would immediately check the impact using Ansible’s check mode to simulate the changes:

ansible-playbook playbook.yml --check

Then, I would use rollback mechanisms:

 If using version-controlled infrastructure, I would revert to the last known good state.
 If state drift occurred, I would restore backups or manually fix affected services.

To prevent this in the future, I would add an environment check in the playbook to prevent
accidental production deployments.

6. How do you integrate Ansible with GitLab CI/CD?

Scenario: A team wants to run Ansible playbooks in GitLab CI/CD pipelines.

Answer:
I would define a .gitlab-ci.yml file like this:

deploy:
stage: deploy
image: python:3.9
before_script:
- apt update && apt install -y ansible
script:
- ansible-playbook -i inventory.ini playbook.yml

This ensures Ansible runs automatically on each pipeline execution.

7. What’s your approach to handling sensitive variables in Ansible?

Scenario: Ansible playbooks contain database passwords and API keys.

Answer:
I would use Ansible Vault to encrypt sensitive variables:

ansible-vault encrypt secrets.yml

Then, in the playbook, I would reference the encrypted file:

vars_files:
- secrets.yml

To automate decryption in CI/CD, I would store the vault password in Jenkins/GitHub Secrets.

8. How do you provision cloud infrastructure with Ansible?

Scenario: You need to create AWS EC2 instances using Ansible.

Answer:
I would use the AWS Ansible collection and define a playbook like this:

- name: Launch EC2 instance


hosts: localhost
tasks:
- amazon.aws.ec2_instance:
name: "MyInstance"
instance_type: "t2.micro"
image_id: "ami-12345678"
region: "us-east-1"
state: "present"

Then, I would run:

ansible-playbook aws_provision.yml

to deploy the infrastructure.

9. How do you troubleshoot an Ansible connection timeout issue?

Scenario: Ansible fails with a "timeout" error when connecting to remote servers.

Answer:
I would:

1. Check SSH connectivity:


2. ssh -i key.pem user@remote-host
3. Increase the Ansible timeout in ansible.cfg:
4. [defaults]
5. timeout = 60
6. Verify inventory settings and ensure the correct IP is used.
10. How do you prevent Ansible from breaking production services during
deployment?

Scenario: A deployment must not cause downtime.

Answer:
I would:

 Use rolling updates instead of updating all servers at once.


 Implement Ansible check mode to preview changes:
 ansible-playbook playbook.yml --check
 Use handlers to restart services only when necessary.

11. How would you deploy a Docker container using Ansible?

Scenario: Deploy an Nginx container using Ansible.

Answer:
I would use the Docker module in Ansible:

- name: Start Nginx container


hosts: all
tasks:
- community.docker.docker_container:
name: nginx
image: nginx:latest
state: started

12. How do you enforce idempotency in Ansible?

Scenario: A playbook should only make changes when necessary.

Answer:
I would use Ansible’s declarative modules, which only modify state if needed.
For example, this ensures a package is installed only if missing:

- name: Install Nginx


ansible.builtin.apt:
name: nginx
state: present

13. How do you handle Ansible playbook dependencies?


Scenario: Ansible roles depend on external collections.

Answer:
I would define dependencies in requirements.yml and install them using:

ansible-galaxy install -r requirements.yml

14. How do you trigger an Ansible playbook after a successful build in Jenkins?

Scenario: Ansible should run only if the build succeeds.

Answer:
I would configure a post-build step in Jenkins to trigger Ansible using a shell script:

ansible-playbook deploy.yml

15. How do you roll back changes if an Ansible deployment fails?

Scenario: A new deployment caused issues, and we need to revert.

Answer:
I would:

 Use version control to redeploy the previous version.


 Use Ansible tags to undo specific changes:
 ansible-playbook rollback.yml --tags revert
 Restore backups for critical services.

These scenarios should prepare you for any interview question related to CI/CD with Ansible.
Want more practice?

Here are 15 work scenario questions with 1st-person answers, tailored for interviews on
writing custom Ansible modules and plugins.

1. You need to automate a process that no existing Ansible module covers. What
do you do?
I would write a custom Ansible module in Python. First, I'd define the module’s parameters and
use the AnsibleModule class to handle input validation. Then, I'd write the logic to interact with
the system or API. Finally, I'd ensure it returns structured JSON output. I'd test it locally before
integrating it into a playbook.

2. You wrote a custom module, but it fails. How do you debug it?

I start by running it independently with Python to check for syntax errors. Then, I use ansible-
playbook -vvv for verbose output. If the error isn’t clear, I add module.fail_json(msg="debug message")
in different parts of the script to pinpoint the issue. If needed, I check logs in /var/log/ansible.log
or use debug tasks in the playbook.

3. A team wants a custom Ansible module to interact with an internal API. How do
you approach it?

First, I gather requirements—what endpoints, authentication, and data formats are needed.
Then, I use the requests library in Python within my custom module to send API requests. I'd
handle authentication securely, parse responses, and format the output for Ansible. After
testing, I'd document its usage for the team.

4. You need to modify Ansible’s output format. What do you do?

I would write a custom callback plugin to format the output as needed. I'd extend CallbackBase
and override methods like v2_runner_on_ok() to control success output and v2_runner_on_failed() for
failure messages. After testing, I’d add it to callback_whitelist in ansible.cfg.

5. You need to create a new type of connection for Ansible. How?

I'd write a custom connection plugin by subclassing ConnectionBase. I’d implement exec_command()
for remote execution, and put_file() & fetch_file() for file transfers. Once done, I’d place it in
connection_plugins/ and test it by setting connection: my_custom_connection in a playbook.
6. A colleague asks how to distribute a custom Ansible module. What do you
suggest?

I'd suggest packaging it as a collection using ansible-galaxy init my_collection. This makes it easy to
share and install via ansible-galaxy install. Alternatively, if it's a single module, I’d place it in a
library/ folder inside playbooks or contribute it upstream if it’s widely useful.

7. You need to integrate Ansible into a Python application. How?

I'd use the Ansible SDK or ansible-runner. With the SDK, I’d use PlaybookExecutor to run playbooks
directly. With ansible-runner, I’d call ansible_runner.run() to execute Ansible asynchronously and
collect structured results.

8. Your module must support dry-run mode (--check). How do you implement it?

I set supports_check_mode=True in AnsibleModule. Then, in the logic, I check module.check_mode. If


true, I simulate changes but don’t apply them, ensuring the module is compatible with --check.

9. Your Ansible module runs but doesn’t return expected output. What do you
check?

First, I check if module.exit_json() is returning correctly formatted JSON. Then, I validate the
module’s logic by printing intermediate outputs. If needed, I enable -vvv logging to see what’s
happening.

10. Ansible execution seems slow due to a custom plugin. How do you optimize?

I profile it by adding timing logs. If it’s a callback plugin, I ensure it only processes necessary
events. If it’s a connection plugin, I check if it’s using unnecessary SSH sessions. I’d also avoid
excessive Python loops and optimize API calls with batch requests.

11. How do you test a custom module before using it in production?


I run it locally using python my_module.py with mock parameters. Then, I test it in an isolated
Ansible playbook with --check. If it interacts with external APIs or services, I use mocking in unit
tests with pytest and unittest.mock.

12. Your custom connection plugin must support SSH tunneling. How do you do it?

I'd modify exec_command() to establish an SSH tunnel before executing commands. I could use
paramiko to handle SSH connections manually or wrap ssh commands with subprocess. I’d also
ensure put_file() and fetch_file() support tunneling.

13. Your team wants a logging system for Ansible tasks. What’s your approach?

I’d create a callback plugin that sends logs to a centralized system, like ELK, Splunk, or a
database. I’d override methods like v2_runner_on_ok() to capture task success and
v2_runner_on_failed() for failures. Then, I’d format logs as JSON and send them to the logging
system.

14. You need to make your custom module idempotent. What steps do you take?

I ensure the module first checks the current state before making changes. If the desired state is
already present, it should return changed: false. I’d also implement a diff mode where the module
shows what changes would occur before applying them.

15. Your playbook using a custom module works locally but fails on remote hosts.
How do you troubleshoot?

I check Python dependencies on the remote machine to ensure they match my development
environment. I also inspect the Python interpreter path in ansible_python_interpreter. If the issue
persists, I copy the module manually to /tmp/ and run it with python /tmp/my_module.py to identify
errors.

These scenarios cover real-world issues you might face while writing custom Ansible modules
and plugins. Practicing them will help you confidently tackle interview questions.
Would you like me to generate more complex scenarios?

Here are 15 real-world Ansible work scenarios in a first-person Q&A format to help you
confidently answer any interview question.

1. How do you structure an Ansible project for a large-scale deployment?

I follow a modular approach using roles to separate concerns. Each role contains tasks,
templates, handlers, and variables for better organization. I also use group_vars and host_vars
to manage configurations per environment (staging, production). My inventory files are stored
separately, and I ensure the ansible.cfg file is properly set to optimize execution.

2. How do you use tags effectively in playbooks?

I tag tasks based on functionality to allow selective execution. For example, I might tag package
installations as install and configuration updates as config. If I need to debug something, I add a
debug tag and run only those tasks with ansible-playbook site.yml --tags debug. I also use the never tag
for tasks that should only run when explicitly called.

3. How do you keep Ansible playbooks idempotent?

I use built-in Ansible modules like file, copy, and template instead of shell or command. I ensure
tasks check for the desired state before applying changes, such as using state: present instead of
latest. For commands, I use creates: or removes: to prevent unnecessary re-execution.

4. How do you use include_tasks efficiently?

I use include_tasks for dynamic execution, especially when tasks depend on conditions. For
example, when setting up multiple services, I use a loop with include_tasks to call a task file per
service. This approach keeps my main playbook clean and allows better reusability.

5. Have you ever faced an issue where a playbook was not idempotent? How did
you fix it?
Yes, once a playbook kept restarting a service unnecessarily because of a misconfigured
changed_when condition. I fixed it by modifying the task to compare actual file contents using diff
before restarting. I also added a notify handler so the restart only happens when a configuration
change is detected.

6. How do you handle secret data in Ansible?

I use Ansible Vault to encrypt sensitive data like passwords and API keys. Instead of hardcoding
secrets, I store them in a vault-encrypted file and reference them in playbooks. To manage
vault passwords securely, I use an external vault password file or environment variables.

7. How do you validate input variables in Ansible?

I use the assert module to validate that required variables are defined and meet expected
conditions. For example, before configuring a database, I check that db_password is set and
meets complexity requirements. If an invalid value is detected, the playbook fails early,
preventing misconfiguration.

8. How do you troubleshoot a failing Ansible playbook?

I first check the error message to identify the failing task. If it's unclear, I add -vvvv to the
execution command for detailed logs. If a task behaves unexpectedly, I use the debug module to
print variable values. I also use check_mode to simulate execution without making changes,
helping me pinpoint issues safely.

9. What would you do if an Ansible task fails on only some hosts?

I check the affected hosts for differences in OS, package versions, or connectivity issues. If
necessary, I use run_once for tasks that should only execute on one host and delegate_to for
central execution. I also use failed_when to fine-tune failure conditions if a task isn't truly critical.

10. How do you optimize the performance of Ansible playbooks?


I enable fact caching to reduce repeated data gathering. I use strategy: free when tasks don't
need strict sequential execution. When deploying to many hosts, I increase forks in ansible.cfg. I
also avoid unnecessary loops by using batch processing where possible.

11. How do you ensure Ansible runs only when necessary?

I use conditions (when:) to check for existing states before executing tasks. I also leverage the
changed_when parameter to prevent redundant changes. For example, instead of running a
command unconditionally, I check if the required outcome already exists.

12. How do you handle dynamic inventories?

For cloud environments like AWS, I use Ansible's dynamic inventory plugins. I configure
inventory scripts or use AWS’s EC2 inventory plugin to fetch host details dynamically. This
eliminates manual inventory updates when instances change.

13. How do you make sure an Ansible deployment doesn’t break production?

I always run playbooks in check mode first to see what changes would be made. I also test in a
staging environment before deploying to production. For critical services, I use rolling updates
by applying changes to a subset of hosts first.

14. How do you enforce compliance and validation in Ansible?

I use assert to validate configurations before applying changes. I also integrate Ansible with
tools like Ansible Lint to catch errors early. If a compliance check fails, I use fail: to stop
execution and provide a clear error message.

15. How do you recover from a failed Ansible deployment?

I first analyze logs to identify the cause of failure. If the failure left an incomplete state, I use
ansible-playbook --limit <affected_hosts> to rerun only on failed nodes. If needed, I roll back
by running a previous version of the playbook or restoring from a backup.
These answers will demonstrate your practical experience, problem-solving skills, and best
practices when working with Ansible. Want me to tailor any of these further for your specific
use case?

Great! Here are 15 real-world Ansible work scenario questions, answered in the first person to
reflect how you'd respond in an interview.

1. How do you handle an urgent incident using Ansible Rulebooks?

I set up Ansible Rulebooks to detect incidents automatically. For example, if a critical service
fails, my syslog event source triggers a playbook to restart the service. I also send notifications
to Slack or ServiceNow for visibility. This reduces response time and ensures quick remediation
without manual intervention.

2. You need to integrate Ansible with ServiceNow for ticket automation. How do
you do it?

I use the servicenow.itsm.incident module to create, update, and close incidents based on playbook
results. If a server crashes, my Ansible playbook automatically logs an incident in ServiceNow,
assigns it, and triggers remediation actions. Once resolved, the playbook updates ServiceNow
and closes the ticket.

3. How would you deploy an application to Kubernetes using Ansible?

I use the k8s module to manage Kubernetes resources. My playbook creates a Deployment
with replica settings and a Service to expose the application. I also integrate Helm charts when
managing complex deployments. If scaling is needed, I adjust the replica count dynamically
based on metrics.

4. You need to automate Docker container deployment with Ansible. How would
you do it?
I use the docker_container module to pull images, create containers, and manage networking.
I ensure idempotency by defining container state as present. If a container needs updates, I set
up a rolling restart mechanism to avoid downtime.

5. How do you optimize Ansible execution in a large environment?

I adjust forks in ansible.cfg for parallel execution and use fact caching to reduce data gathering
time. I also distribute workloads using Ansible Automation Controller (formerly Tower) and
enable SSH pipelining for faster task execution.

6. A playbook is running slow. How do you troubleshoot it?

First, I run the playbook with -vvv for verbose output. I check for tasks with long execution
times, then optimize by enabling async tasks, reducing loops, and ensuring handlers are used
efficiently. If latency is an issue, I use Mitogen to accelerate execution.

7. Your Ansible playbook fails at a specific task. How do you debug it?

I use the --step and --start-at-task flags to isolate the issue. Checking logs with -vvv, I analyze if it’s a
syntax issue, module failure, or network timeout. If needed, I run the playbook in check mode
to see potential issues before execution.

8. How do you ensure secure Ansible execution?

I never hardcode passwords; instead, I use Ansible Vault for sensitive data. I restrict SSH access
with role-based permissions and limit playbook execution using scoped inventory and
automation controller RBAC policies.

9. You need to update 500 servers without downtime. How do you do it?

I use rolling updates with serial execution in the playbook, updating servers in small batches.
Example:

serial: 10
This ensures minimal impact. I also add health checks before moving to the next batch.

10. How do you integrate Ansible with a CI/CD pipeline?

I embed Ansible Playbooks in my Jenkins or GitHub Actions workflow. For example, after a
code commit, the pipeline triggers an Ansible playbook to deploy the application in staging, run
tests, and, if successful, proceed to production.

11. Your playbook needs to run different tasks based on OS type. How do you
handle that?

I use conditionals with ansible_facts to check OS type and version. Example:

- name: Install package based on OS


yum:
name: httpd
state: present
when: ansible_os_family == "RedHat"

This ensures tasks execute only where appropriate.

12. You need to automate AWS EC2 provisioning. How do you do it?

I use the amazon.aws.ec2_instance module to launch instances dynamically. The playbook fetches
inventory from AWS using dynamic inventory scripts to target the new instances for
configuration.

13. How do you rollback a failed Ansible deployment?

I implement a fail-safe mechanism by using retries and checkpoints. If a deployment fails, a


rollback playbook restores the previous stable version using stored backups or
Docker/Kubernetes versioning.
14. A junior engineer accidentally ran an Ansible playbook on production. How do
you prevent this in the future?

I enforce role-based access (RBAC) in Ansible Automation Controller. I also add safety checks in
playbooks:

- fail:
msg: "This playbook cannot run on production!"
when: inventory_hostname in groups['production']

This prevents accidental execution in production.

15. You need to set up an event-driven workflow where a security alert triggers
Ansible remediation. How do you do it?

I integrate Ansible Rulebooks with a SIEM tool (e.g., Splunk or ELK). When a security alert (e.g.,
failed SSH attempts) is logged, the Ansible EDA engine triggers a playbook that blocks the
offending IP via firewall rules, preventing further attacks.

Conclusion

These responses cover technical depth while demonstrating real-world problem-solving skills.
Want me to simulate a live interview with follow-ups?

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy