Day 5: Environment Variables and Command Line Arguments in Python for DevOps

CHANDRESH PATLECHANDRESH PATLE
2 min read

Welcome back to our Python for DevOps series! Today, we'll explore the critical topics of handling environment variables and command-line arguments. These skills are essential for configuring and customizing DevOps automation tasks efficiently.

🔶 Environment Variables in Python 🔶

🔶 Reading and Writing Environment Variables:

  • Python's os module provides functions like os.environ to access and modify environment variables.

  • Use os.getenv("variable_name") to retrieve a specific environment variable.

🔶 Securing Sensitive Information:

  • Store sensitive information, like API keys, in environment variables to keep them secure.

  • Never hardcode sensitive information in your code.

🔶 Command Line Arguments in Python

🔶 Handling Command Line Arguments:

  • Python's sys.argv provides access to command line arguments.

  • The argparse module offers a more structured and user-friendly approach for handling command line arguments.

🔶 Practice Exercises and Examples

Example: Customizing DevOps Automation Tasks:

import sys

def addition():
    add = num1 + num2
    print(add)

def subtraction():
    sub = num1 - num2
    print(sub),

num1 = float(sys.argv[1])
operation = sys.argv[2]
num2 = float(sys.argv[3])

if operation == 'add':
    addition()

if operation == 'sub':
    subtraction()

import os

print(os.getenv("password"))

🔶 Conclusion

Understanding how to work with environment variables and command line arguments is crucial for building flexible and secure DevOps scripts. Stay tuned for Day 6, where we'll explore Operators in Python for DevOps.

Note: I am following Abhishek Verraamalla's YouTube playlist for learning.

GitHub Repo: https://github.com/Chandreshpatle28/python-for-devops-av


Happy Learning :)

Stay in the loop with my latest insights and articles on cloud ☁️ and DevOps ♾️ by following me on Hashnode, LinkedIn (https://www.linkedin.com/in/chandreshpatle28/), and GitHub (https://github.com/Chandreshpatle28).

Thank you for reading! Your support means the world to me. Let's keep learning, growing, and making a positive impact in the tech world together.

#Git #Linux Devops #Devopscommunity #PythonforDevOps #python

0
Subscribe to my newsletter

Read articles from CHANDRESH PATLE directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

CHANDRESH PATLE
CHANDRESH PATLE

Hi, I'm Chandresh Patle, an aspiring DevOps Engineer with a diverse background in field supervision, manufacturing, and service consulting. With a strong foundation in engineering and project management, I bring a unique perspective to my work. I recently completed a Post Graduate Diploma in Advanced Computing (PG-DAC), where I honed my skills in web development, frontend and backend technologies, databases, and DevOps practices. My proficiency extends to Core Java, Oracle, MySQL, SDLC, AWS, Docker, Kubernetes, Ansible, Linux, GitHub, Terraform, Grafana, Selenium, and Jira. I am passionate about leveraging technology to drive efficient and reliable software delivery. With a focus on DevOps principles and automation, I strive to optimize workflows and enhance collaboration among teams. I am constantly seeking new opportunities to expand my knowledge and stay up-to-date with the latest industry trends. If you have any questions, collaboration ideas, or professional opportunities, feel free to reach out to me at patle269@gmail.com. I'm always open to connecting with fellow tech enthusiasts and exploring ways to contribute to the DevOps community. Let's build a better future through innovation and continuous improvement!