Hey guys,
I have a custom redis recipe where I need to use the IP addresses of host names from an array.
The array looks like this
default['redis']['masters'] = ["hostname1", "hostname2"]
default['redis']['slaves'] = ["hostname3", "hostname4", "hostname5"]
The recipe looks like this:
package "redis" do
version "#{node['redis']['version']}"
flush_cache [ :before ]
allow_downgrade false
action :install
end
template "redis.conf" do
path "/etc/redis.conf"
source "redis.conf.erb"
owner node['redis']['user']
group node['redis']['group']
mode "0644"
variables(
:master => node['redis']['master'],
:slave => node['redis']['slave'],
:hostname => node.name.split('.')[0]
)
end
template "redis-sentinel.conf" do
path "/etc/redis-sentinel.conf"
source "redis-sentinel.conf.erb"
owner node['redis']['user']
group node['redis']['group']
mode "0644"
variables(
:master => node['lc-redis']['master'],
:masterip => node['lc-redis']['master']['ipaddress'],
:masterport => node['lc-redis']['masterport'],
:masterauth => node['lc-redis']['masterauth']
)
end
service "redis" do
supports :restart => true
action [:enable, :start]
end
service "redis-sentinel" do
supports :restart => true
action [:enable, :start]
end
In the end, what I need is to be able to deploy the recipe to all 5 nodes (the 2 masters and the 3 slaves)
The master hostname1
should have as slaves hostname3
and hostname4
and master hostname2
should have as slave hostname5
The template file for redis.conf looks like this:
daemonize yes
pidfile /var/run/redis.pid
dbfilename dump.rdb
dir /var/lib/redis/
<% if @hostname != @master -%>
slaveof <%= node['redis']['master'] %> <%= node['redis']['defaultport'] %>
<% end -%>
<% if @hostname != @master -%>
masterauth <%= node['redis']['masterpass'] %>
<% else %>
requirepass <%= node['redis']['masterpass'] %>
<% end -%>
loglevel notice
logfile /var/log/redis/redis.log
And the template file for redis-sentinel looks like this:
pidfile "/var/run/redis/sentinel.pid"
daemonize yes
logfile "/var/log/redis/sentinel.log"
port 26379
dir "/tmp"
sentinel monitor <%= node['lredis']['master']%> <%= node['redis']['master']['ipaddress']%> <%= node['redis']['masterport']%> 2
sentinel down-after-milliseconds <%= node['redis']['master']%> 10000
sentinel failover-timeout <%= node['redis']['master']%> 30000
sentinel auth-pass <%= node['redis']['master']%> <%= node['redis']['masterauth']%>
sentinel parallel-syncs <%= node['redis']['master']%> 1
Obviously, is wrong because when I have two masters i need to have the sentinel commands for each of them. I should use each
method for all the masters and have all sentinel commands for for every master, but i cannot get the ip address of all the masters.
What I’ve tried is something like this:
masters = node['redis']['master']
masters.each do |node|
Chef::Log.info("#{node["name"]} has IP address #{node["ipaddress"]}")
end
How do I store the IP address of each master in a variable?
How can I improve this recipe and make it work?
I know it’s a long shot, but I dare asking.
Thank you,
Gabriel