Last active
February 7, 2018 06:43
-
-
Save natemccurdy/3d5460c7dba2f533d274dcd8844de301 to your computer and use it in GitHub Desktop.
Puppet defined type to create an F5 load Balancer stack
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This defined type is used to create everything needed | |
# in the F5 to load-balance a set of servers. | |
# | |
# Required Parameters: | |
# @param nodes [Array] An array of hashes, where each hash contains a 'name', 'address', 'port', and optional 'monitors' key. | |
# @param pool_name [String] The name of the pool to create and manage. | |
# @param virtualserver_name [String] The name of the virtualserver to create and mange. | |
# @param destination_address [String] The IP address of the VIP destination. | |
# | |
define profile::f5::load_balancer ( | |
Array[Hash] $nodes, | |
String[1] $pool_name, | |
String[1] $virtualserver_name, | |
String[1] $destination_address, | |
Optional[Array[String[1]]] $irules = undef, | |
String[1] $destination_mask = '255.255.255.255', | |
Array[String[1]] $health_monitors = [ '/Common/tcp' ], | |
String[1] $load_balancing_method = 'round-robin', | |
Enum['present','absent'] $ensure = 'present', | |
String[1] $http_profile = '/Common/http', | |
String[1] $service_port = '80', | |
String[1] $source = '0.0.0.0', | |
) { | |
$description = "Managed by Puppet: ${title}" | |
$_pool_name = $pool_name ? { | |
/^\/Common\// => $pool_name, | |
default => "/Common/${pool_name}", | |
} | |
$_virtualserver_name = $virtualserver_name ? { | |
/^\/Common\// => $virtualserver_name, | |
default => "/Common/${virtualserver_name}", | |
} | |
# Iterate over each node and manage it. | |
$nodes.each |Hash $node| { | |
# Check that all required node properties are defined. | |
['name','address','port'].each |$property| { | |
if !(has_key($node, $property)) or ($node[$property] == undef) { | |
fail("${title}: Missing required node property '${property}'") | |
} | |
} | |
# Use the ICMP monitor if none defined. | |
$_monitors = $node['monitors'] ? { | |
undef => [ '/Common/icmp' ], | |
default => $node['monitors'], | |
} | |
f5_node { $node['name']: | |
ensure => $ensure, | |
address => $node['address'], | |
health_monitors => $_monitors, | |
availability_requirement => 'all', | |
description => $description, | |
before => [ | |
F5_pool[$_pool_name], | |
F5_virtualserver[$_virtualserver_name], | |
], | |
} | |
} | |
# Generate the members parameter value to use in the f5_pool. | |
# We are creating an array of hashes with 'name' and 'port' keys. | |
$members = $nodes.map |Hash $node| { | |
{ | |
'name' => $node['name'], | |
'port' => $node['port'], | |
} | |
} | |
f5_pool { $_pool_name: | |
ensure => $ensure, | |
health_monitors => $health_monitors, | |
load_balancing_method => $load_balancing_method, | |
members => $members, | |
description => $description, | |
before => F5_virtualserver[$_virtualserver_name], | |
} | |
f5_virtualserver { $_virtualserver_name: | |
ensure => $ensure, | |
provider => 'standard', | |
default_pool => $_pool_name, | |
destination_address => $destination_address, | |
destination_mask => $destination_mask, | |
http_profile => $http_profile, | |
service_port => $service_port, | |
irules => $irules, | |
source => $source, | |
description => $description, | |
require => F5_pool[$_pool_name], | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment