Using variables in recipes?


#1

How can I use the $HOSTNAME variable in a recipe?

I need something which would fit here:

remote_file “/tmp/$HOSTNAME.tar.xz” do
source "https://user:pass@server:port/aaa/$HOSTNAME.tar.xz"
owner “root"
group “root"
mode 00600
not_if { ::File.exists?(”/some/path/here”) }
end


Tomasz Chmielewski
http://wpkg.org


#2

You can interpolate the ohai hostname attribute like so: “#{node[‘hostname’]}”

Tomasz Chmielewski tch@wpkg.org wrote:

How can I use the $HOSTNAME variable in a recipe?

I need something which would fit here:

remote_file “/tmp/$HOSTNAME.tar.xz” do
source "https://user:pass@server:port/aaa/$HOSTNAME.tar.xz"
owner “root"
group “root"
mode 00600
not_if { ::File.exists?(”/some/path/here”) }
end


Tomasz Chmielewski
http://wpkg.org


#3

Hi,

you can use Ohai variable node[‘hostname’] or environment variable via
ENV[‘something’].

Damn Eric beat me to it…

in terminal open up chef-shell. It’ll allow you to experiment without
having to execute the whole recipe.

Cheers
Alex-

On Sun, Apr 27, 2014 at 7:58 PM, Tomasz Chmielewski tch@wpkg.org wrote:

How can I use the $HOSTNAME variable in a recipe?

I need something which would fit here:

remote_file “/tmp/$HOSTNAME.tar.xz” do
source "https://user:pass@server:port/aaa/$HOSTNAME.tar.xz"
owner “root"
group “root"
mode 00600
not_if { ::File.exists?(”/some/path/here”) }
end


Tomasz Chmielewski
http://wpkg.org


#4

Unfortunately doesn’t work as expected.

This is the output of $HOSTNAME on this server:

$ echo $HOSTNAME
server.example.com

In the recipe, “#{node[‘hostname’]}” gives me just “server” (so, without
".example.com" I’m getting from $HOSTNAME variable).

“#{node[‘fqdn’]}” gives me “localhost”, and “#{node[‘domain’]}” is
empty.

Are there any other ways to get a variable to be used in a recipe
(putting aside whether the fqdn should be localhost or not)?


Tomasz Chmielewski
http://wpkg.org

On 2014-04-28 04:04, Wolfe, Eric G wrote:

You can interpolate the ohai hostname attribute like so:
"#{node[‘hostname’]}"

Tomasz Chmielewski tch@wpkg.org wrote:

How can I use the $HOSTNAME variable in a recipe?

I need something which would fit here:

remote_file “/tmp/$HOSTNAME.tar.xz” do
source "https://user:pass@server:port/aaa/$HOSTNAME.tar.xz"
owner “root"
group “root"
mode 00600
not_if { ::File.exists?(”/some/path/here”) }
end


Tomasz Chmielewski
http://wpkg.org


#5

You can also try “#{ENV[‘HOSTNAME’]}”.

Joe

On Apr 27, 2014, at 8:27 PM, Tomasz Chmielewski tch@wpkg.org wrote:

Unfortunately doesn’t work as expected.

This is the output of $HOSTNAME on this server:

$ echo $HOSTNAME
server.example.com

In the recipe, “#{node[‘hostname’]}” gives me just “server” (so, without “.example.com” I’m getting from $HOSTNAME variable).

“#{node[‘fqdn’]}” gives me “localhost”, and “#{node[‘domain’]}” is empty.

Are there any other ways to get a variable to be used in a recipe (putting aside whether the fqdn should be localhost or not)?


Tomasz Chmielewski
http://wpkg.org

On 2014-04-28 04:04, Wolfe, Eric G wrote:

You can interpolate the ohai hostname attribute like so: "#{node[‘hostname’]}"
Tomasz Chmielewski tch@wpkg.org wrote:
How can I use the $HOSTNAME variable in a recipe?
I need something which would fit here:
remote_file “/tmp/$HOSTNAME.tar.xz” do
source "https://user:pass@server:port/aaa/$HOSTNAME.tar.xz"
owner “root"
group “root"
mode 00600
not_if { ::File.exists?(”/some/path/here”) }
end

Tomasz Chmielewski
http://wpkg.org


#6

Turned out #{node[‘machinename’]} was what I was looking for - thanks
for the tip!


Tomasz Chmielewski

On 2014-04-28 04:06, Alex Vinyar wrote:

Hi,

you can use Ohai variable node[‘hostname’] or environment variable via
ENV[‘something’].

Damn Eric beat me to it…

in terminal open up chef-shell. It’ll allow you to experiment without
having to execute the whole recipe.

Cheers
Alex-

On Sun, Apr 27, 2014 at 7:58 PM, Tomasz Chmielewski tch@wpkg.org
wrote:

How can I use the $HOSTNAME variable in a recipe?

I need something which would fit here:

remote_file “/tmp/$HOSTNAME.tar.xz” do
source "https://user:pass@server:port/aaa/$HOSTNAME.tar.xz"
owner “root"
group “root"
mode 00600
not_if { ::File.exists?(”/some/path/here”) }
end


Tomasz Chmielewski
http://wpkg.org [1]


#7

On Sun Apr 27 20:39:39 2014, Tomasz Chmielewski wrote:

Turned out #{node[‘machinename’]} was what I was looking for - thanks
for the tip!

^^^^ that is the correct answer for ohai 7

node[‘hostname’] == always the short hostname (hostname -s on linux)
node[‘fqdn’] == DNS resolved FQDN (hostname -f on linux)
node[‘machinename’] == return host hostname (whatever hostname
returns)

it grew organically so it doesn’t necessary make the best sense.

the default node_name was also changed (in chef 11.10-ish?) so that if
you don’t specify an explicit node_name is uses the node[‘fqdn’] and
then node[‘machinename’] so that reading the default node_name should
never fail with nil any more.

node[‘machinename’] won’t be backwards compatible if you care about
that, but it’s probably preferable to node[‘fqdn’] going forwards.

node[‘machinename’] || node[‘fqdn’] should be a backwards-compatible
idiom that prefers the new behavior.