Ansible
Ansible
✅ 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:
Encrypt it with:
✅ 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
Run with:
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
✅ 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
2. Debug Failures
- name: Debug with failure
hosts: all
tasks:
- name: Failing task
command: /bin/false
register: result
ignore_errors: yes
File: filter_plugins/custom_filter.py
def reverse_string(value):
return value[::-1]
class FilterModule(object):
def filters(self):
return {
'reverse_string': reverse_string
}