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

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

ยท

2 min read

Play this article

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

Did you find this article valuable?

Support CHANDRESH PATLE by becoming a sponsor. Any amount is appreciated!

ย