Hosts in users databags

Hi.

I need to improve my cookbook with use more attributes from databags. If anyone
can help me, I would appreciate it very much.

I have some structure in users databags like this:

{
“id”: “stpik”,
“ssh_keys”: “ssh-rsa
AAAAB3NzaC1yc2EAAAABIwAAAQEA07GRR4vlBauJPDyEZhBg7l/YXRafbEWcH+fAa5SjKph4bJUaJiHv3WRETTTcnGElWPPQyQPBU7vQpUtcassV6gF2/EeFAeBsRReBfroNWGzqD63fFjwgS3CwewPlm5y3dhpy9vNUfPcG1ekbnPCnl66xSazBYI4j4SmCn5iGhZ8tt7tHP+IT8QVpHpVU9qUsBueSGNU68JzNmVQwH/5rh8ItFGrQfKfU3pYi7DaGP4Gj4r05jFlI+AkVwvTHg6amtjSMX2+7CRNqK2VGQrCWIaRX2PR6NEZVqBpTi/C0sdrtewU1oRhuqOV9YJnSw==
va@desktop”,
“groups”: [“admins”],
“hosts”: [“prod1”, “prod2.example.com”]
}

and some ruby at recipe:

users = Array.new
ssh_array = Array.new

search(:ssh_users, ‘groups:us’) do |u|
users << u[‘id’]
ssh_array << u[‘ssh_keys’] + ’ >> ’ + u[‘id’]

template “/home/us/.ssh/authorized_keys” do
source "authorized_keys.erb"
owner "ess"
group "ess"
mode "0600"
variables :ssh_keys => ssh_array
end
end

The question is how I can use hosts to deliver users only to defined hosts or
if there empty then to all?

ssh_array << u['ssh_keys'] + ' >> ' + u['id'] if u["hosts"].include?(
node.name)

btw i think you should do the filtering based on roles , instead of host
names

ssh_array << (u['ssh_keys'] + ' >> ' + u['id'] ) if u["roles"].any?{|r|
node.roles.include?(r)}
or
ssh_array << (u['ssh_keys'] + ' >> ' + u['id'] ) unless (u["roles"] &
node.roles).empty?

On Tue, Jul 9, 2013 at 1:13 PM, nowarry@gmail.com wrote:

Hi.

I need to improve my cookbook with use more attributes from databags. If
anyone
can help me, I would appreciate it very much.

I have some structure in users databags like this:

{
"id": "stpik",
"ssh_keys": "ssh-rsa

AAAAB3NzaC1yc2EAAAABIwAAAQEA07GRR4vlBauJPDyEZhBg7l/YXRafbEWcH+fAa5SjKph4bJUaJiHv3WRETTTcnGElWPPQyQPBU7vQpUtcassV6gF2/EeFAeBsRReBfroNWGzqD63fFjwgS3CwewPlm5y3dhpy9vNUfPcG1ekbnPCnl66xSazBYI4j4SmCn5iGhZ8tt7tHP+IT8QVpHpVU9qUsBueSGNU68JzNmVQwH/5rh8ItFGrQfKfU3pYi7DaGP4Gj4r05jFlI+AkVwvTHg6amtjSMX2+7CRNqK2VGQrCWIaRX2PR6NEZVqBpTi/C0sdrtewU1oRhuqOV9YJnSw==
va@desktop",
"groups": ["admins"],
"hosts": ["prod1", "prod2.example.com"]
}

and some ruby at recipe:

users = Array.new
ssh_array = Array.new

search(:ssh_users, 'groups:us') do |u|
users << u['id']
ssh_array << u['ssh_keys'] + ' >> ' + u['id']

template "/home/us/.ssh/authorized_keys" do
source "authorized_keys.erb"
owner "ess"
group "ess"
mode "0600"
variables :ssh_keys => ssh_array
end
end

The question is how I can use hosts to deliver users only to defined hosts
or
if there empty then to all?