Loop through each items
# Define a list of names
names = ["Alice", "Bob", "Charlie"]
# Use enumerate to loop through the list with both index (i) and value (name)
# The second argument '1' makes the index start from 1 instead of 0
for i, name in enumerate(names, 1):
# Print the index and the name in a formatted string
print(f"{i}. Name is {name}")
# List of server IP addresses
servers = ["10.0.0.1", "10.0.0.2", "10.0.0.3"]
# Loop with enumerate to get both index and IP
for i, ip in enumerate(servers):
if i == 1: # Only target the second server (index starts from 0)
subprocess.run(f"ssh ubuntu@{ip} 'sudo systemctl restart nginx'", shell=True)
Last updated