The objective here is to create a simple sshd cookbook for Red Hat/CentOS configuration.
Create the cookbook
knife cookbook create sshd
Create the default recipe.
Options like sshd port , banner etc will be pulled from a data bag called base_config. The template file for SSHD configuration would be sshd.erb.
File : cookbooks/sshd/recipes/default.rb
sshd_config = data_bag_item('base_config', 'sshd')
template "/etc/ssh/sshd_config" do
source "sshd.erb"
mode "0644"
variables(
:sshd_port => sshd_config['port'],
:x11_forwarding => sshd_config['x11_forwarding'],
:banner => sshd_config['banner'],
:permit_root => sshd_config['permit_root']
)
end
template "/etc/issue.net" do
source "issue.net.erb"
end
service "sshd" do
action [ :restart ]
end
File : cookbooks/sshd/template/sshd.erb
Port <%= @sshd_port %> Protocol 2 #other SSHD config has been omitted for the sake of the blog post PermitRootLogin <%= @permit_root %> X11Forwarding <%= @x11_forwarding %> Banner <%= @banner %> #other SSHD config has been omitted for the sake of the blog post
In issue.net.erb we are are reading from motd and adding a little blurb after that
File : cookbooks/sshd/template/issue.net.erb
<%= File.read("/etc/motd") %>
*****************************
Use of the Site by unauthorized users is prohibited and
unauthorized users will be prosecuted to the fullest
extent of the law.
*****************************
Data bag
Create the data bag
{
"id": "sshd",
"port": "2222",
"x11_forwarding": "no",
"banner":"/etc/issue.net",
"permit_root": "no"
}
Finishing up
knife data bag create base_config
knife data bag from file base_config data_bags/base_config/config.json
knife cookbook upload sshd
Then add the recipe to a role or node run list and run the chef-client