Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[COOK-1077] - error handling if data bag is missing

* Use begin/rescue in case the data bag does not [yet] exist.
* Convert the item to a hash instead of a data_bag_item.
  • Loading branch information...
commit 2727bcfc37e7488892b66f75b231e7de189ca206 1 parent 130baa9
@jtimberman jtimberman authored
Showing with 20 additions and 21 deletions.
  1. +1 −9 ssh_known_hosts/README.md
  2. +19 −12 ssh_known_hosts/recipes/default.rb
View
10 ssh_known_hosts/README.md
@@ -22,7 +22,7 @@ Adding custom host keys
-----------------------
If you want to add custom host keys for hosts not in your Chef deployment (such
-as github.com, for example), create a data bag called "ssh_known_hosts" and add
+as github.com, for example), create a data bag called "`ssh_known_hosts`" and add
an item for each host to it that looks like this:
{
@@ -37,14 +37,6 @@ You can also specify the following optional values in the data bag:
* hostname : Short hostname form of the host without domain name
* dsa : If the host has a dsa host key, specify it as "dsa" instead of "rsa"
-Changes
-=======
-
-## v0.4.0:
-
-* COOK-493: include fqdn
-* COOK-721: corrected permissions
-
License and Author
==================
View
31 ssh_known_hosts/recipes/default.rb
@@ -23,18 +23,25 @@
nodes = search(:node, "keys_ssh:* NOT name:#{node.name}")
nodes << node
-other_hosts = data_bag('ssh_known_hosts')
-require 'resolv'
-r = Resolv.new
-other_hosts.each do |h|
- host = data_bag_item('ssh_known_hosts', h)
- host['ipaddress'] ||= r.getaddress(host['fqdn'])
- host['keys'] = {
- 'ssh' => {}
- }
- host['keys']['ssh']['host_rsa_public'] = host['rsa'] if host.has_key?('rsa')
- host['keys']['ssh']['host_dsa_public'] = host['dsa'] if host.has_key?('dsa')
- nodes << host
+begin
+ other_hosts = data_bag('ssh_known_hosts')
+rescue
+ Chef::Log.info("Could not load data bag 'ssh_known_hosts', this is optional, moving on...")
+end
+
+if other_hosts
+ require 'resolv'
+ r = Resolv.new
+ other_hosts.each do |h|
+ host = data_bag_item('ssh_known_hosts', h).to_hash
+ host['ipaddress'] ||= r.getaddress(host['fqdn'])
+ host['keys'] = {
+ 'ssh' => {}
+ }
+ host['keys']['ssh']['host_rsa_public'] = host['rsa'] if host.has_key?('rsa')
+ host['keys']['ssh']['host_dsa_public'] = host['dsa'] if host.has_key?('dsa')
+ nodes << host
+ end
end
template "/etc/ssh/ssh_known_hosts" do
Please sign in to comment.
Something went wrong with that request. Please try again.