To get IP addres of the specific host machine in rub

436 views Asked by At

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:
2

There are 2 answers

0
Keshav Kishor On BEST ANSWER
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

8
Draco Ater On

You tagged the question with [chef] and [chef-recipe], so I understand you are trying to get another machine's IP address inside recipe. If that another machine is also registered with Chef Server, the easiest would be search. You can search for any machine registered on the Chef Server by some attribute, in your case - hostname.

nodes = search(:node, 'hostname:<another_vm_hostname>')
p nodes.first['ipaddress']

Update:

You have an error in your 3rd line. Don't surround nodes.first['ipaddess'] with quotes.

 node.default['aem_dispatcher_cookbook']['ip_address'] = nodes.first['ipaddress']