How to find a node ip chef by role

I have a problem. I'm trying to get the IP address of the node that starts postgresql and is the replication master. So I can inject the IP into the cookbook repmgr so that it automatically sets it to the SSH config file. I need this because I am not running SSH on port 22, so I need to automate the process.

This is the configure.rb file:

template File.join(node[:repmgr][:pg_home], '.ssh/config') do
  source 'ssh_config.erb'
  mode 0644
  owner node[:repmgr][:system_user]
  group node[:repmgr][:system_user]
  variables( :hosts => Array(master_node[:ipaddress]) )
end

directory File.dirname(node[:repmgr][:config_file_path])

template node[:repmgr][:config_file_path] do
  source 'repmgr.conf.erb'
  mode 0644
end

      

Master node IP address is taken from attributes (default.rb):

default[:repmgr][:addressing][:master] = nil

      

I need to change nil to something else so that I can get the IP of the master server so that it is slave aka. the standby server can add it to the SSH config for SSH so that it replicates over my SSH port and not the default.

I hope someone can help, because I am really new to Ruby and I only know the basics.

Thank. I hope you understand my question.

+3


source to share


1 answer


If you are using chef-server , you can find other highlighted nodes via search in the recipe. You can search for nodes by various properties: for example, role, recipe, environment, name, etc. I hope your node replication master has some attribute that makes it unique, like postgres recipe or replication master role in run_list.

nodes = search( :node, 'recipes:postgres' )

      

or

nodes = search( :node, 'role:replication-master' )

      



The search returns an array of nodes with matching attributes. And then:

node.default[:repmgr][:addressing][:master] = nodes.first[:ipaddress]

      

The code should be written in the recipe file, not in the attributes.

+3


source







All Articles