Skip to content

Commit e4625ca

Browse files
author
Jeff Goldschrafe
committed
Add group option to ssh_config resource/provider
1 parent 0a6e059 commit e4625ca

2 files changed

Lines changed: 12 additions & 9 deletions

File tree

providers/config.rb

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,36 @@
11
include Chef::SSH::PathHelpers
22
action :add do
33
ssh_user = new_resource.user || 'root'
4+
ssh_group = new_resource.group || ssh_user
45
ssh_config_path = default_or_user_path(node['ssh']['config_path'], ssh_user)
56

6-
remove_entry(ssh_config_path, ssh_user)
7-
add_entry(ssh_config_path, ssh_user)
8-
set_rights_proper(ssh_config_path, ssh_user)
7+
remove_entry(ssh_config_path, ssh_user, ssh_group)
8+
add_entry(ssh_config_path, ssh_user, ssh_group)
9+
set_rights_proper(ssh_config_path, ssh_user, ssh_group)
910
end
1011

1112
action :remove do
1213
ssh_user = new_resource.user || 'root'
14+
ssh_group = new_resource.group || ssh_user
1315
ssh_config_path = default_or_user_path(node['ssh']['config_path'], ssh_user)
1416

1517
remove_entry(ssh_config_path, ssh_user)
1618
end
1719

18-
def remove_entry(config_file, ssh_user)
20+
def remove_entry(config_file, ssh_user, ssh_group)
1921
execute "remove #{new_resource.host} from #{config_file}" do
2022
command "ruby -e 'x = $<.read; x.gsub!(/^[\n]{0,1}Host #{new_resource.host.strip}.*#End Chef SSH for #{new_resource.host.strip}\n/m,\"\"); puts x' #{config_file} > #{config_file}.new && mv #{config_file}.new #{config_file}"
2123
user ssh_user
22-
group ssh_user
24+
group ssh_group
2325
only_if "grep \"#{new_resource.host}\" #{config_file}"
2426
end
2527
end
2628

27-
def add_entry(config_file, ssh_user)
29+
def add_entry(config_file, ssh_user, ssh_group)
2830
execute "add #{new_resource.host} to #{config_file}" do
2931
command "echo '#{config_fragment}' >> #{config_file}"
3032
user ssh_user
31-
group ssh_user
33+
group ssh_group
3234
umask 600
3335
end
3436
end
@@ -42,10 +44,10 @@ def config_fragment
4244
return x
4345
end
4446

45-
def set_rights_proper(config_file, ssh_user)
47+
def set_rights_proper(config_file, ssh_user, ssh_group)
4648
file "#{config_file}" do
4749
owner ssh_user
48-
group ssh_user
50+
group ssh_group
4951
mode "0600"
5052
action :create
5153
end

resources/config.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
attribute :host, :kind_of => String, :name_attribute => true
55
attribute :options, :kind_of => Hash
66
attribute :user, :kind_of => String
7+
attribute :group, :kind_of => String
78
attribute :path, :kind_of => String
89

910
def initialize(*args)

0 commit comments

Comments
 (0)