Created
May 2, 2014 21:57
-
-
Save chuckg/8a52f21dfedfb6a7ffd1 to your computer and use it in GitHub Desktop.
Add Chef node name to the IP address during knife-ssh commands
This file contains 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
# Tested with: | |
# * Ruby 2.0 | |
# * Chef 11.6.2 | |
require 'chef/knife' | |
require 'chef/knife/ssh' | |
module Nodename | |
def self.prepended(base) | |
base.option :nodename, | |
:short => "-n", | |
:long => "--nodename", | |
:description => "Show node name and IP address", | |
:boolean => true, | |
:default => false | |
end | |
def configure_session | |
session = super | |
configure_nodename | |
session | |
end | |
# Taken from chef/knife/ssh#print_data | |
def print_data(host, data) | |
if data =~ /\n/ | |
data.split(/\n/).each { |d| print_data(host, d) } | |
else | |
# Use the nodename if the user has requested it | |
host = @ip_to_nodenames[host] if config[:nodename] | |
padding = @longest - host.length | |
str = ui.color(host, :cyan) + (" " * (padding + 1)) + data | |
ui.msg(str) | |
end | |
end | |
def configure_nodename | |
if config[:nodename] | |
@ip_to_nodenames = {} | |
@action_nodes.each do |node| | |
node_string = "#{node.name} (#{node.ipaddress})" | |
@ip_to_nodenames[node.ipaddress] = node_string | |
# Recompute the @longest variable so our columns line up properly | |
@longest = node_string.length if node_string.length > @longest | |
end | |
end | |
end | |
end | |
Chef::Knife::Ssh.send :prepend, Nodename |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment