Every automation script you write, from the simplest to the most complex, needs to work with data. Data is the raw material, the lumber and wiring of your project. But raw data is messy. To build anything useful, you need to organize it.
In Python, we use special containers called data structures to bring order to chaos. Think of them as the shelves, bins, and labeled drawers of your programming workshop. Learning how to choose the right container and how to read data from the outside world is the next crucial step in your DevOps journey.
Organizing Items with Lists and Tuples
Let’s start with the most fundamental way to store a collection of items: in a sequence.
Lists: The All Purpose, Changeable Container
A list in Python is an ordered collection of items that can be changed. It’s like your daily to do list or a shopping list; you can add items, remove them, and reorder them as you please. Lists are defined with square brackets [].
# A list of server hostnames
servers = ["web-server-01", "db-server-01", "api-server-01"]
print(servers)
You can access any item in the list using its index, which is its position in the sequence. The catch? Indexing starts at 0!
# Get the first server
first_server = servers[0] # This will be "web-server-01"
print(f"The first server is: {first_server}")
Because lists are changeable, or mutable, you can modify them after they are created. The two most common operations are adding an item to the end with .append() and removing an item with .remove().
# Add a new server to the list
servers.append("cache-server-01")
print(f"Updated server list: {servers}")
# Decommission the database server
servers.remove("db-server-01")
print(f"Final server list: {servers}")
Tuples: The Protected, Unchangeable Container
A tuple is the sibling of a list. It is also an ordered collection of items, but with one critical difference: it is immutable, meaning it cannot be changed after it is created. Tuples are defined with parentheses ().
# A tuple for RGB color values
primary_color_red = (255, 0, 0)
You can access items in a tuple using an index, just like a list. But if you try to change it, Python will give you an error.
# This would cause an error:
# primary_color_red[0] = 200
So why would you ever want this limitation? Protection. When you have data that should absolutely not change by accident, like coordinates, configuration constants, or database records, using a tuple ensures its integrity.
Key-Value Power: Mastering Dictionaries for Configuration Data
Lists and tuples are great for simple sequences, but what if your data has more structure? What if each piece of data has a label? This is where dictionaries come in, and for DevOps, they are arguably the most important data structure of all.
A dictionary is an unordered collection of key value pairs. Think of a real world dictionary: you look up a word (the key) to find its definition (the value). This structure is perfect for holding configuration data.
Dictionaries are defined with curly braces {}.
# A dictionary representing a server's configuration
server_config = {
"hostname": "web-server-01",
"ip_address": "192.168.1.10",
"os": "Ubuntu 22.04",
"is_active": True,
"ram_gb": 32
}
Instead of accessing items with a numeric index, you access values using their unique key. This makes your code incredibly readable.
# Get the server's operating system
operating_system = server_config["os"]
print(f"The server is running: {operating_system}")
You can easily add new key value pairs or modify existing ones.
# Add a location to the config
server_config["location"] = "us-east-1"
# Upgrade the RAM
server_config["ram_gb"] = 64
print(server_config)
Working with Files: Reading from and Writing to .txt and .csv Files
Your scripts don’t exist in a vacuum. They need to read data from files and write results back out. Python makes file operations clean and safe, especially when using the with open() statement, which automatically handles closing the file for you.
Reading from a Text File
Let’s say you have a file ip_addresses.txt. To read its contents, you open it in read mode ('r').
# ip_addresses.txt contains one IP address per line
# 10.0.0.1
# 10.0.0.2
try:
with open("ip_addresses.txt", "r") as file:
for line in file:
# .strip() removes any leading/trailing whitespace, including the newline character
print(f"Pinging IP address: {line.strip()}")
except FileNotFoundError:
print("Error: The file ip_addresses.txt was not found.")
Writing to a Text File
To write to a file, you open it in write mode ('w'). Be careful, as this will completely overwrite the file if it already exists. If you want to add to the end of a file, use append mode ('a').
server_names = ["server1", "server2", "server3"]
with open("server_report.txt", "w") as report_file:
report_file.write("Server Deployment Report\n")
report_file.write("========================\n")
for server in server_names:
report_file.write(f"Deployed {server} successfully.\n")
A Practical Task: Parsing a CSV of Server Information
CSV (Comma Separated Values) is a standard format for storing tabular data. Python has a built in csv module that makes reading these files a joy.
Let's create a file named servers.csv with the following content:
Code snippet
hostname,ip_address,location,os
web01,192.168.1.20,us-east-1,Ubuntu
db01,192.168.1.30,us-east-1,CentOS
api01,192.168.2.40,eu-west-2,Ubuntu
Now, let’s write a Python script to parse this file and print a clean summary for each server. We will use csv.DictReader, which cleverly reads each row not as a list, but as a dictionary, using the header row for the keys.
import csv
FILENAME = "servers.csv"
try:
with open(FILENAME, mode='r') as csv_file:
# Use DictReader to read the CSV as a list of dictionaries
csv_reader = csv.DictReader(csv_file)
print("--- Server Inventory ---")
for row in csv_reader:
# Now we can access data by column name!
hostname = row["hostname"]
ip = row["ip_address"]
location = row["location"]
print(f"Hostname: {hostname}, IP: {ip}, Location: {location}")
except FileNotFoundError:
print(f"Error: Could not find the file {FILENAME}")
This script is readable, efficient, and robust. By using a dictionary for each row, you make the code's intent perfectly clear. This is the essence of writing professional automation scripts.
You are now equipped to handle the most common forms of data you'll encounter. You can organize data in lists, protect it in tuples, label it in dictionaries, and read and write it from the files that are the lifeblood of any IT system ! 🎉