0% found this document useful (0 votes)
0 views5 pages

Ansible

The document outlines a series of problem statements related to Ansible automation, including tasks for installing and configuring a website, dynamic inventory scripts, task assignments, file encryption with Vault, using tags in playbooks, creating roles with Ansible Galaxy, Docker setup, and custom scripts for pinging. Each problem statement provides specific tasks and commands to accomplish various automation objectives. Additionally, it includes examples of debugging failures and creating custom filter plugins.

Uploaded by

1ms22ci403
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)
0 views5 pages

Ansible

The document outlines a series of problem statements related to Ansible automation, including tasks for installing and configuring a website, dynamic inventory scripts, task assignments, file encryption with Vault, using tags in playbooks, creating roles with Ansible Galaxy, Docker setup, and custom scripts for pinging. Each problem statement provides specific tasks and commands to accomplish various automation objectives. Additionally, it includes examples of debugging failures and creating custom filter plugins.

Uploaded by

1ms22ci403
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/ 5

Practice Test

✅ Problem Statement 01: Install & Configure Project


Website
# site.yml
- name: Install and configure website
hosts: webservers
become: yes
tasks:
- name: Install Apache
apt:
name: apache2
state: present
update_cache: yes

- name: Copy website files


copy:
src: /path/to/index.html
dest: /var/www/html/index.html

- name: Start and enable Apache


service:
name: apache2
state: started
enabled: yes

✅ Problem Statement 02
1. Dynamic Inventory Script (minimal Python)
#!/usr/bin/env python3
import json
inventory = {
"webservers": {
"hosts": ["192.168.56.101"]
}
}
print(json.dumps(inventory))
Save as dynamic_inventory.py and run with:

ansible-playbook site.yml -i dynamic_inventory.py

2. Assign Task to Specific Hosts


- name: Run only on DB servers
hosts: dbservers
tasks:
- name: Install MySQL
apt:
name: mysql-server
state: present

3. Create and Re-encrypt File with Vault


- name: Create and encrypt a file
hosts: all
tasks:
- name: Create secret file
copy:
content: "SuperSecretPassword"
dest: /tmp/secret.txt

Encrypt it with:

ansible-vault encrypt /tmp/secret.txt


ansible-vault rekey /tmp/secret.txt

✅ Problem Statement 03
1. Use Tags in Playbook
- name: Tagged playbook
hosts: all
tasks:
- name: Install git
apt:
name: git
state: present
tags: install_git

- name: Debug message


debug:
msg: "Tag execution"
tags: debug_tag

Run with:

ansible-playbook tags.yml --tags install_git

2. Create Role using Ansible Galaxy


ansible-galaxy init webserver

Folder structure:

roles/
└── webserver/
├── tasks/main.yml
├── templates/
└── files/

Use in playbook:

- hosts: webservers
roles:
- webserver

✅ Problem Statement 04
1. Configure Docker and Pull Images
- name: Setup Docker
hosts: docker
become: yes
tasks:
- name: Install Docker
apt:
name: docker.io
state: present

- name: Pull nginx image


community.docker.docker_image:
name: nginx
source: pull

2. Deploy Custom Docker Container


- name: Run custom Docker container
hosts: docker
tasks:
- name: Create nginx container
community.docker.docker_container:
name: my_nginx
image: nginx
state: started
ports:
- "8080:80"

✅ Problem Statement 05
1. Shell to Python Ping Module

Shell version:
- name: Shell ping
hosts: all
tasks:
- name: Ping via shell
shell: ping -c 1 google.com

Python version (custom script):


# ping_script.py
import os
response = os.system("ping -c 1 google.com")
print("Success" if response == 0 else "Failed")
Use in playbook:

- name: Python ping


hosts: all
tasks:
- name: Run ping script
script: ping_script.py

2. Debug Failures
- name: Debug with failure
hosts: all
tasks:
- name: Failing task
command: /bin/false
register: result
ignore_errors: yes

- name: Show debug


debug:
var: result

3. Create Custom Filter Plugin

File: filter_plugins/custom_filter.py
def reverse_string(value):
return value[::-1]

class FilterModule(object):
def filters(self):
return {
'reverse_string': reverse_string
}

You might also like