DEV Community

Kanav Gathe
Kanav Gathe

Posted on

Day 15/90: Python Basics for DevOps Engineers 🐍 #90DaysOfDevOps

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
Enter fullscreen mode Exit fullscreen mode

Python Data Types 📝

1. Numbers

# Integer
server_count = 42

# Float
cpu_usage = 85.5

# Complex
complex_number = 1 + 2j
Enter fullscreen mode Exit fullscreen mode

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('-'))
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

4. Tuples (Immutable)

# Version info
version = ('Ubuntu', 20, 'LTS')
os_name, version_num, release_type = version
Enter fullscreen mode Exit fullscreen mode

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'))
Enter fullscreen mode Exit fullscreen mode

6. Sets

# Unique ports
active_ports = {80, 443, 22, 3306}
active_ports.add(8080)
Enter fullscreen mode Exit fullscreen mode

7. Boolean

is_running = True
has_backup = False
Enter fullscreen mode Exit fullscreen mode

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}")
Enter fullscreen mode Exit fullscreen mode

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 🔨

  1. Script automation
  2. Log parsing
  3. API interactions
  4. Configuration management
  5. Monitoring and alerts
  6. Data processing
  7. Infrastructure automation

Python #DevOps #Automation #Programming #90DaysOfDevOps


This is Day 15 of my #90DaysOfDevOps journey. Keep coding and automating!

Top comments (0)