Sagar Ghuge
Sagar Ghuge

Reputation: 241

In chef how to catch execute resource's command output in variable?

I want to use private ip address later in my Chef recipe for that how can I catch it in a variable. My code looks like...

execute 'privateip' do
command 'curl http://169.254.169.254/latest/meta-data/local-ipv4'
action :run
end

I want to catch the output of curl command in a variable something like..

privip = 'curl http://169.254.169.254/latest/meta-data/local-ipv4'

and then use that value in configuration file OR is there any way to get the private ipaddresses of aws instance, because ohai doesn't support the privateip attribute. Any help will highly appreciated.

Upvotes: 0

Views: 2186

Answers (1)

zuazo
zuazo

Reputation: 5748

You can read the local-ipv4 value in the node['ec2']['local_ipv4'] attribute. IIRC all the EC2 meta-data is included under node['ec2'].

If you still prefer to run a command, you can use the shell_out! helper:

output = shell_out!('mycommand --some --arguments').stdout

As they said in the comments, for this particular case you can also use the Chef::HTTP class:

http = Chef::HTTP.new('http://169.254.169.254')
privip = http.get('/latest/meta-data/local-ipv4')

Upvotes: 1

Related Questions