Browse Source

Merge "Fix interface_for_ip for newer facter versions" into stable/queens

tags/8.5.1
Zuul 2 months ago
parent
commit
0aaa58e094
1 changed files with 50 additions and 4 deletions
  1. 50
    4
      lib/puppet/parser/functions/interface_for_ip.rb

+ 50
- 4
lib/puppet/parser/functions/interface_for_ip.rb View File

@@ -9,22 +9,68 @@ module Puppet::Parser::Functions
9 9
     if arg[0].class == String
10 10
       begin
11 11
         ip1 = IPAddr.new(arg[0])
12
+        network_facts = lookupvar('networking')
12 13
         Dir.foreach('/sys/class/net/') do |interface|
13 14
           next if interface == '.' || interface == '..'
14
-          iface_no_dash = interface.gsub('-', '_')
15
+          # puppet downcases fact names, interface names can have capitals but
16
+          # in facter 2.x they were lower case. In facter 3.x they can have
17
+          # capitals
18
+          iface_no_dash = interface.gsub('-', '_').downcase
15 19
 
16 20
           if ip1.ipv4?
17 21
             ipaddress_name = "ipaddress_#{iface_no_dash}"
18 22
             netmask_name   = "netmask_#{iface_no_dash}"
23
+            facter_ip      = 'ip'
24
+            facter_netmask = 'netmask'
19 25
           else
20 26
             ipaddress_name = "ipaddress6_#{iface_no_dash}"
21 27
             netmask_name   = "netmask6_#{iface_no_dash}"
28
+            facter_ip      = 'ip6'
29
+            facter_netmask = 'netmask6'
22 30
           end
23 31
 
24
-          interface_ip = lookupvar(ipaddress_name)
25
-          netmask = lookupvar(netmask_name)
26
-          unless interface_ip.nil? then
32
+          if network_facts.nil? or network_facts['interfaces'].nil? then
33
+            # facter 2 facts
34
+            interface_ip = lookupvar(ipaddress_name)
35
+            next if interface_ip.nil?
27 36
             ip2 = IPAddr.new(interface_ip)
37
+            netmask = lookupvar(netmask_name)
38
+            return interface if ip1.mask(netmask) == ip2.mask(netmask)
39
+          else
40
+            # facter 3+ syntax:
41
+            # networking => {
42
+            #   ...
43
+            #   interfaces => {
44
+            #     br-ctlplane => {
45
+            #       bindings => [
46
+            #         {
47
+            #           address => "192.168.24.1",
48
+            #           netmask => "255.255.255.0",
49
+            #           network => "192.168.24.0"
50
+            #         }
51
+            #       ],
52
+            #       bindings6 => [
53
+            #         {
54
+            #           address => "fe80::5054:ff:fe22:bac3",
55
+            #           netmask => "ffff:ffff:ffff:ffff::",
56
+            #           network => "fe80::"
57
+            #         }
58
+            #       ],
59
+            #       ip => "192.168.24.1",
60
+            #       ip6 => "fe80::5054:ff:fe22:bac3",
61
+            #       mac => "52:54:00:22:ba:c3",
62
+            #       mtu => 1500,
63
+            #       netmask => "255.255.255.0",
64
+            #       netmask6 => "ffff:ffff:ffff:ffff::",
65
+            #       network => "192.168.24.0",
66
+            #       network6 => "fe80::"
67
+            #     },
68
+            #   },
69
+            #   ...
70
+            # }
71
+            next if network_facts['interfaces'][interface].nil? or network_facts['interfaces'][interface][facter_ip].nil?
72
+            ip2 = IPAddr.new(network_facts['interfaces'][interface][facter_ip])
73
+            netmask = network_facts['interfaces'][interface][facter_netmask]
28 74
             return interface if ip1.mask(netmask) == ip2.mask(netmask)
29 75
           end
30 76
         end

Loading…
Cancel
Save