Day 15: Python Basics for DevOps Engineers 🚀
Hello DevOps enthusiasts! 👋 Welcome to Day 15 of the #90DaysOfDevOps challenge. Today, we're exploring Python fundamentals essential for DevOps.
Python Installation 🔧
Ubuntu Installation
# Install Python
sudo apt update
sudo apt install python3
# Verify installation
python3 --version
Python Data Types 📝
1. Numbers
# Integer
server_count = 42
# Float
cpu_usage = 85.5
# Complex
complex_number = 1 + 2j
2. Strings
# Single line string
hostname = 'web-server-01'
# Multi-line string
description = '''This is a
multi-line description
of our server'''
# String operations
print(hostname.upper())
print(hostname.split('-'))
3. Lists (Mutable)
# Server list
servers = ['web-01', 'app-02', 'db-01']
# List operations
servers.append('cache-01')
servers.remove('db-01')
print(servers[0]) # First server
4. Tuples (Immutable)
# Version info
version = ('Ubuntu', 20, 'LTS')
os_name, version_num, release_type = version
5. Dictionaries
# Server details
server_info = {
'hostname': 'web-01',
'ip': '192.168.1.100',
'status': 'running',
'ram_gb': 16
}
# Access data
print(server_info['hostname'])
print(server_info.get('status'))
6. Sets
# Unique ports
active_ports = {80, 443, 22, 3306}
active_ports.add(8080)
7. Boolean
is_running = True
has_backup = False
DevOps Example 🛠️
def check_server_status(servers):
"""Monitor server status"""
status = {}
for server in servers:
# Simulate status check
status[server] = 'running' if len(server) % 2 == 0 else 'stopped'
return status
# Usage
servers = ['web-01', 'app-02', 'db-01', 'cache-02']
server_status = check_server_status(servers)
for server, state in server_status.items():
print(f"Server {server} is {state}")
Key Takeaways 💡
- Python is essential for DevOps automation
- Multiple data types serve different purposes
- Lists and dictionaries are commonly used in DevOps
- Python's simplicity makes automation easier
- Understanding data types is fundamental
Practical Uses in DevOps 🔨
- Script automation
- Log parsing
- API interactions
- Configuration management
- Monitoring and alerts
- Data processing
- Infrastructure automation
Python #DevOps #Automation #Programming #90DaysOfDevOps
This is Day 15 of my #90DaysOfDevOps journey. Keep coding and automating!