Ask Your Question

how to use alias for host in serverspec

asked 2015-12-16 05:48:34 -0600

Chutki gravatar image

updated 2015-12-16 18:33:40 -0600


Below is the puppet manifests,

 host { $::fqdn :
    ensure       => $ensure,
    host_aliases => $::hostname,
    ip           => $::ipaddress,

I have written below serverspec for this

describe host('') do
#  it { should exist }
  its(:ipaddress) { should eq '172.45.300.456' }
  it { should be_aliased_to 'server }

But getting below error

On host `localhost'
     Failure/Error: it { should be_aliased_to 'server' }
       expected Host "server.puppetlabscom" to respond to `aliased_to?`
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2015-12-16 18:29:04 -0600

updated 2015-12-16 23:52:51 -0600

I think you're getting confused about Puppet resource types and Serverspec resource types. Remember, Serverspec is not part of Puppet.

The Serverspec host type is for hosts in DNS. Meanwhile, the Puppet host type is for records in the /etc/hosts file.

Also, the is_aliased_to matcher that you're trying to use is for the Serverspec mail_alias type.

You probably want to say something like:

describe host('') do
  its(:ipaddress) { should eq '172.45.300.456' }
  it { should'hosts') }

describe file('/etc/hosts') do
   its(:content) { should contain '172.45.300.456 server' }


See this:

# cat /tmp/foo.pp 
host { '' :
  ensure       => present,
  host_aliases => 'server',
  ip           => '',
# puppet apply /tmp/foo.pp 
Notice: Compiled catalog for myhost in environment production in 0.14 seconds
Notice: /Stage[main]//Host[]/ensure: created
Notice: Finished catalog run in 0.26 seconds
# grep -P "\\tserver" /etc/hosts   server

In other words, Puppet is using the tab character rather than spaces to separate data in the /etc/hosts file. Try this:

describe file('/etc/hosts') do
   its(:content) { should match '172.45.300.456\\tserver' }
edit flag offensive delete link more


I tried the second one "describe file('/etc/hosts')" but I am getting an error if there is any mismatch in spec "On host `localhost' Failure/Error: its(:content) { should contain '172.45.300.456 server' } \n172.45.300.456 server\n"

Chutki gravatar imageChutki ( 2015-12-16 21:44:11 -0600 )edit

So what does your /etc/hosts file actually contain?

Alex Harvey gravatar imageAlex Harvey ( 2015-12-16 22:06:45 -0600 )edit

"172.45.300.456 server"- this is how in /etc/hosts file format

Chutki gravatar imageChutki ( 2015-12-16 22:46:24 -0600 )edit

Ok I updated to show the issue.

Alex Harvey gravatar imageAlex Harvey ( 2015-12-16 23:46:33 -0600 )edit

I tried what you suggessted, but it didn't work Also, I tried below two options 1) its(:content) { should match "172.45.300.456\\tserver" } and 2) its(:content) { should match /^172.45.300.456\\tserver$/ } no luck :(

Chutki gravatar imageChutki ( 2015-12-17 05:14:32 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2015-12-16 05:48:34 -0600

Seen: 214 times

Last updated: Dec 16 '15