Report abuse

Function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# Accepts: Keepalived node hostname to search for
# Returns: Keepalived prio value for searched node
module Puppet::Parser::Functions
   newfunction(:keepalived_prio, :type => :rvalue) do |args|
      hostname = args[0]
      instances = lookupvar('ec_lb::lb::instances')

      ############################################################
      # get_instance will return the path to a value in a
      # nested hash, so for the following hash structure:
      #
      # $instances {
      #   'EC_LB_VIP' => {
      #     hosts => ['node1', 'node2'],
      #     vrid  => '51',
      #   },
      #   'EC_LB_VIP_TEST' => {
      #     hosts => ['node1-test', 'node2-test'],
      #     vrid  => '52',
      #   }
      # }
      #
      # ...using get_instance(instances, node1) returns:
      # 
      # ['EC_LB_VIP', 'host', ['node1', 'node2']]
      ############################################################

      def get_instance(hash, patt, trail=[])
         hash.each_pair do |key, val|
            if val == patt
               trail << key
               return true
            elsif val.is_a?(Hash)
               return trail if get_instance(val, patt, trail << key)
            end
         end
         false
      end

      instance_hosts = get_instance(instances, hostname).at(2)

      host_prios = Hash.new
      vrid_start = 101

      instance_hosts.each do |host|
         host_prios[host] = vrid_start
         vrid += 1
      end

      return host_prios[hostname]
   end
end

Manifest

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class ec_lb::lb($instance) {
  include keepalived

  $instances = {
    "EC_LB_VIP" => {
      hosts => ['node1', 'node2'],
      vrid  => "123",
    },
    "EC_LB_VIP_TEST" => {
      hosts => ['node1-test', 'node2-test'],
      vrid  => "122",
    }
  }

  ....snip....

  keepalived::conf::vrrp_instance {
    "EC_LB_VIP":
      instance_if      => "eth1",
      instance_state   => "BOTH",
      instance_vrid    => $instances[$instance][vrid],
      instance_prio    => keepalived_prio($hostname),
      instance_vip     => "1.2.3.4",
      instance_scripts => ["chk_haproxy","chk_sshd"];
  }
}

irb error

1
2
3
4
5
6
7
8
9
NoMethodError: undefined method `environment' for nil:NilClass
  from /usr/lib/ruby/1.8/puppet/parser/scope.rb:104:in `environment'
  from /usr/lib/ruby/1.8/puppet/resource/type_collection_helper.rb:5:in `known_resource_types'
  from /usr/lib/ruby/1.8/puppet/parser/scope.rb:113:in `find_hostclass'
  from /usr/lib/ruby/1.8/puppet/parser/scope.rb:222:in `lookup_qualified_var'
  from /usr/lib/ruby/1.8/puppet/parser/scope.rb:242:in `lookupvar'
  from /etc/puppet/modules/custom/lib/puppet/parser/functions/keepalived_prio.rb:6:in `function_keepalived_prio'
  from (irb):6
  from :0

puppet error

1
err: Could not retrieve catalog from remote server: Error 400 on SERVER: undefined method `at' for false:FalseClass at /etc/puppet/modules/ec_lb/manifests/lb.pp:44 on node node1.example.com