added facter facts to show netmask in cidr notation for each interface
authorvarac <varacanero@zeromail.org>
Wed, 28 Aug 2013 12:41:39 +0000 (14:41 +0200)
committervarac <varacanero@zeromail.org>
Wed, 28 Aug 2013 12:41:39 +0000 (14:41 +0200)
lib/facter/netmask_cidr_interface.rb [new file with mode: 0644]

diff --git a/lib/facter/netmask_cidr_interface.rb b/lib/facter/netmask_cidr_interface.rb
new file mode 100644 (file)
index 0000000..d628d08
--- /dev/null
@@ -0,0 +1,22 @@
+# adds netmask facts for each interface in cidr notation
+# i.e.:
+# ...
+# netmask_cidr_eth2 => 24
+# netmask_cidr_lo => 8
+# netmask_cidr_tun0 => 32
+# netmask_cidr_virbr0 => 24
+# ...
+
+require 'facter/util/ip'
+
+Facter::Util::IP.get_interfaces.each do |interface|
+  netmask = Facter.value("netmask_#{interface}")
+  if netmask != nil
+    Facter.add("netmask_cidr_" + interface ) do
+      setcode do
+        cidr_netmask=IPAddr.new(netmask).to_i.to_s(2).count("1")
+        cidr_netmask
+      end
+    end
+  end
+end