Day 5: Environment Variables and Command Line Arguments in Python for DevOps
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 likeos.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