Skip to content

Instantly share code, notes, and snippets.

@scopenco
Created August 11, 2017 11:24
Show Gist options
  • Save scopenco/3d5786c2209b0f693b1fcdd45e463ebf to your computer and use it in GitHub Desktop.
Save scopenco/3d5786c2209b0f693b1fcdd45e463ebf to your computer and use it in GitHub Desktop.
An Ohai plugin assigns the IP address of second interface to node.ipaddress attribute (for virtualbox vagrant boxes linux/windows)
#
# Author:: Andrei Skopenko (<andrei@skopenko.net>)
#
# Copyright 2017, Andrei Skopenko
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
Ohai.plugin(:Vboxipaddress) do
provides "ipaddress"
depends "ipaddress", "network/interfaces", "virtualization/system", "etc/passwd"
collect_data(:default) do
# linux
if virtualization["system"] == "vbox" && etc["passwd"].any? { |k,v| k == "vagrant"}
interface_key = network["interfaces"].keys.last
network["interfaces"][interface_key]['addresses'].each do |ip, params|
ipaddress ip if params['family'] == ('inet')
end
# windows
elsif kernel['cs_info'] && kernel['cs_info']['model'] == 'VirtualBox'
ipaddress network["interfaces"][interface_key]['configuration']['ip_address'][0]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment