rubyruby-on-rails-3ruby-on-rails-5chef-infrachef-recipe

To get IP addres of the specific host machine in rub


I want to find the IP address of other system. For example, I am executing my code from server wevrs1234 and I want the IP address of server apvrs1234 and store it in variable. Please help me to get this.

ip = IPSocket.getaddress(Socket.gethostname)

is the code I have so far.

AS per suggestion i have made this code but getting error. Please find my code

 publish_vm = node['aem_dispatcher_cookbook']['publish'].to_s
  nodes = search(:node, 'hostname:publish_vm')
 node.default['aem_dispatcher_cookbook']['ip_address'] = 'nodes.first['ipaddress']'

  template node['aem_dispatcher_cookbook']['owner']['home'] + '/conf.d/publish_farm.any' do
    source   'publish_farm.any.erb'
    owner    node['aem_dispatcher_cookbook']['owner']['user']
    group    node['aem_dispatcher_cookbook']['owner']['group']
    mode     '0755'
    variables(
      publish_host: node['aem_dispatcher_cookbook']['publish'],
      publish_port: node['aem_dispatcher_cookbook']['publish_port'],
      ip_addr: node['aem_dispatcher_cookbook']['ip_address']
    )
  end

Error

[2020-05-20T06:09:52-05:00] DEBUG: Node wevrd64501.uhc.com loading cookbook aem_dispatcher_cookbook's attribute file /root/.chef/local-mode-cache/cache/cookbooks/aem_dispatcher_cookbook/attributes/default.rb

================================================================================
Recipe Compile Error in /root/.chef/local-mode-cache/cache/cookbooks/aem_dispatcher_cookbook/recipes/default.rb
================================================================================

SyntaxError
-----------
/root/.chef/local-mode-cache/cache/cookbooks/aem_dispatcher_cookbook/recipes/default.rb:333: syntax error, unexpected tIDENTIFIER, expecting keyword_end
...ess'] = 'nodes.first['ipaddress']'
...                      ^~~~~~~~~

System Info:

Solution

  • publish_vm = node['aem_dispatcher_cookbook']['publish'].to_s
      ruby_block 'get_ip_from_publish' do
        block do
          Chef::Resource::RubyBlock.send(:include, Chef::Mixin::ShellOut)
          command1 = "nslookup #{publish_vm} |grep '^Address' | awk '{print $2}'| tail -1"
          command_out = shell_out(command1)
          node.run_state['master_ip'] = command_out.stdout
        end
        action :run
      end
    

    This piece of code helped me to get ip address of desired host machine