Python Script Issue: Assigning Unique Attributes to Each F5 Pool Member

20 views Asked by At

I'm attempting to update a configuration file from an older F5 system to a newer format by parsing a text file containing pool configurations. I'm reading the file line by line and using the first word of each line as an attribute. However, within each pool, there are multiple members with different attribute values. Unfortunately, my script assigns the last value of an attribute to all members within a pool instead of assigning each member its respective value. How can I adjust my script to assign unique attribute values to each member within a pool?

My input data

pool BASE {
    members {
        10.250.x.x:http { }
        10.250.x.x:http { }
    }
    monitor http
}
pool 10YEARS {
    members {
        10.250.x.x:http {
            session disabled
        }
        10.250.x.x:http { }
        10.250.x.x:http { }
    }
    monitor http
}
pool ADVICE {
    members {
        10.250.x.x:http {
            priority-group 70
            session disabled
        }
        10.250.x.x:http {
            priority-group 80
            session disabled
        }
        10.250.x.x:http {
            priority-group 90
        }
    }
    min-active-members 1
    monitor http
}

My python script

import json


def transform_pool(pool_name, attributes, ip_name_dict):
    transformed_line = f"ltm pool /Common/{pool_name}{{\n"
    if "monitor" in attributes:
        transformed_line += f"\tmonitor /Common/{attributes['monitor']}\n"
    if "min-active-members" in attributes:
        transformed_line += f"\tmin-active-members {attributes['min-active-members']}\n"
    if "load-balancing-mode" in attributes:
        transformed_line += (
            f"\tload-balancing-mode {attributes['load-balancing-mode']}\n"
        )
    transformed_line += "\tmembers {\n"
    for member in attributes.get("members", []):
        ip_address, port = member
        name = ip_name_dict.get(
            ip_address, ip_address
        )  # Get name from dictionary, fallback to IP if not found
        transformed_line += f"\t\t{name}:{port} {{\n"
        if "priority-group" in attributes:
            transformed_line += f"\t\tpriority-group {attributes['priority-group']}\n"
        if "state" in attributes:
            transformed_line += f"\t\tstate {attributes['state']}\n"
        if "session" in attributes:
            transformed_line += f"\t\tsession user-{attributes['session']}\n"
        transformed_line += "\t\t}\n"
    transformed_line += "\t}\n"
    transformed_line += "}\n"
    return transformed_line


def create_dict_from_file(file_name):
    with open(file_name, "r") as file:
        lines = file.readlines()

    dictionary = {}
    key = ""
    for line in lines:
        if "/Common/" in line:
            key = line.split("/Common/")[1].strip().split(" ")[0]
        if "address" in line:
            ip_address = line.split(" ")[-1].strip()
            dictionary[ip_address] = key

    return dictionary


# Use the functions
file_name = "nodes_config_full.txt"
dictionary = create_dict_from_file(file_name)


# Read original data from pool_data.txt
with open("input.txt", "r") as file:
    lines = file.readlines()

# Transform the data
transformed_lines = []
pool_name = None
attributes = {"members": []}

for line in lines:
    parts = line.strip().split()
    if len(parts) == 0:
        continue
    elif parts[0] == "pool":
        if pool_name is not None:
            transformed_lines.append(transform_pool(pool_name, attributes, dictionary))
            attributes.clear()
            attributes["members"] = []
        pool_name = parts[1]
    elif parts[0] == "monitor":
        attributes["monitor"] = parts[1]
    elif parts[0] == "min-active-members":
        attributes["min-active-members"] = parts[1]
    elif parts[0] == "load-balancing-mode":
        attributes["load-balancing-mode"] = parts[1]
    elif parts[0].count(".") == 3:  # Assume IP address format
        ip_address, port = parts[0].split(":")
        attributes["members"].append((ip_address, port))
    elif parts[0] == "priority-group":
        attributes["priority-group"] = parts[1]
    elif parts[0] == "state":
        attributes["state"] = parts[1]
    elif parts[0] == "session":
        attributes["session"] = parts[1]

# Append the last pool entry
if pool_name is not None:
    transformed_lines.append(transform_pool(pool_name, attributes, dictionary))

# Write transformed string to pools_config.txt
with open("pools_config.txt", "w") as file:
    file.writelines(transformed_lines)

print("Transformation complete. Check pools_config.txt for the output.")

The output

ltm pool /Common/BASE{
    monitor /Common/http
    members {
        WEBF03N0101:http {
        }
        WEBF03N0102:http {
        }
    }
}
ltm pool /Common/10YEARS{
    monitor /Common/http
    members {
        DMZ0103:http {
        session user-disabled
        }
        DMZ0104:http {
        session user-disabled
        }
        DMZ0107:http {
        session user-disabled
        }
    }
}
ltm pool /Common/ADVICE{
    monitor /Common/http
    min-active-members 1
    members {
        DMZ0103:http {
        priority-group 90
        session user-disabled
        }
        DMZ0104:http {
        priority-group 90
        session user-disabled
        }
        DMZ0107:http {
        priority-group 90
        session user-disabled
        }
    }
}
0

There are 0 answers