Skip to main content

Command Palette

Search for a command to run...

Day 96: Functions & Arguments in Python

Using Python Functions and Arguments in DevOps Automation

Published
β€’2 min read
Day 96:  Functions & Arguments in Python

Today, I learned how to create functions in Python β€” the building blocks of reusable and modular scripts.
In DevOps, this helps break down complex tasks (like provisioning, deployments, log parsing) into clean, reusable components.


βš™οΈ What are Functions?

A function is a reusable block of code that performs a specific task.

def greet():
    print("Hello DevOps World!")

greet()

βœ… This helps avoid repetition and makes scripts easier to manage.


πŸ“₯ Functions with Arguments

Functions can take arguments to make them dynamic.

def deploy_app(server_name):
    print(f"Deploying app to {server_name}")

deploy_app("web01")

βœ… Useful when automating tasks for multiple servers or different environments.


πŸ“¦ Functions with Multiple / Lengthy Arguments

We can pass multiple arguments or use *args and **kwargs to handle variable-length arguments.

# Using *args (non-keyword arguments)
def configure_servers(*servers):
    for s in servers:
        print(f"Configuring {s}")

configure_servers("web01", "web02", "db01")


# Using **kwargs (keyword arguments)
def create_vm(**details):
    for key, value in details.items():
        print(f"{key} : {value}")

create_vm(name="devVM", region="us-east-1", size="t2.micro")

βœ… *args is useful for looping through a list of servers.
βœ… **kwargs is useful for passing key-value configuration like environment variables, tags, and metadata.


πŸ’‘ Key Takeaways

  • Functions help write clean, modular scripts.

  • Arguments make them dynamic and reusable.

  • *args and **kwargs give flexibility for automation tasks.

This is a crucial step in writing scalable DevOps automation scripts.

DevOps overview as a beginner

Part 1 of 50

Sharing my journey of learning DevOps as a beginner β€” covering essential tools, cloud setup, CI/CD, Docker, monitoring, and more, step by step with practical examples.