Issue with a simple Bash Script For Adding an iRule to a list of F5 BIG-IP Virtual Servers

94 Views Asked by At

I am quite new to Bash scripting. Though I know the platform I intend the script to run on is not entirely relevant to most contributors and topics on here, I would appreciate if you could at least elevate my concern that the problem I am encountering is with my Bash script itself and not the environment I'm running it on. I am having an issue with a bash script intended to run on a UNIX device, an F5 BIG-IP Load Balancer. The script is intended to:

  • Take as input a .txt file named "vs_list.txt" containing a list of Virtual Server Names, each on a separate row.

  • Prompt the user for the name of the iRule that needs to be added to the Virtual Servers.

  • Iterate over the VS Names from the input list and retrieves the partition in which each VS resides (building on F5 Support Solution K59493724: How to get details of the virtual servers in all partitions via TMSH? )

  • Check for and retrieves the iRules which are currently configured for the given VS

  • Adds the new iRule to the given VS and conserves pre-existing iRules if any.

When running the script I am only hitting the outermost 'else' statement for being unable to find the partition and VS name.

#!/bin/bash

# Prompt the user for the iRule name and read it into the 'new' variable
echo "Please enter the iRule name:"
read new

noneRules='rules none'

while IFS= read -r vs_name; do
  # Retrieve the partition and virtual server name
  full_vs_info=$(tmsh -c "cd /; list ltm virtual recursive" | grep "$vs_name" | grep -m1 "^ltm virtual")
  echo "Full VS Info Debug: $full_vs_info"

  # Extract the partition and virtual server name from the retrieved information
  if [[ $full_vs_info =~ ltm\ virtual\ (.+)/(.+) ]]; then
    partition="${BASH_REMATCH[1]}"
    vs_name="${BASH_REMATCH[2]}"
    
    # Format the tmsh command to include the partition
    rule=$(tmsh list ltm virtual /$partition/$vs_name rules | egrep -v "\{|\}" | xargs)
    
    if [[ "$rule" == "$noneRules" ]]; then
      tmsh modify ltm virtual /$partition/$vs_name rules { $new }
      echo "iRule $new was added to $vs_name in partition $partition"
    else#
      tmsh modify ltm virtual /$partition/$vs_name rules { $rule $new }
      echo "iRules $rule were conserved and added $new to $vs_name in partition $partition"
    fi
  else
    echo "Could not find partition and virtual server name for $vs_name"
  fi
done < /shared/tmp/test_list.txt

tmsh save sys config

As far as I was able to troubleshoot, the problem I am encountering appears to be with line 11 of my script where I attempt to assign the string "ltm virtual SomePartition/VS_Example.com {" to the "full_vs_info" variable using:

full_vs_info=$(tmsh -c "cd /; list ltm virtual recursive" | grep "$vs_name" | grep -m1 "^ltm virtual")

When I run the tmsh command [tmsh -c "cd /; list ltm virtual recursive" | grep "VS_Example.com" | grep -m1 "^ltm virtual"] on its own, from the devices Bash shell, I am getting the output I expect:

"ltm virtual SomePartition/VS_Example.com {"

However, when I run the script with the debug echo , it only outputs "Full VS Info Debug:", and ends the script with "Could not find partition and virtual server name for $vs_name" and a sys config save.

In case it's relevant: I am attempting to run this on a BIG-IP, version 15.1.10.2, build 0.44.2.

All feedback and criticism is highly appreciated! Thanks in advance!

0

There are 0 best solutions below