Ask Your Question
0

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

Hi,

Below is the puppet manifests,

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

I have written below serverspec for this

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

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
0

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('server.puppetlabs.com') do
  its(:ipaddress) { should eq '172.45.300.456' }
  it { should be_resolvable.by('hosts') }
end

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

UPDATE

See this:

# cat /tmp/foo.pp 
host { 'server.puppetlabs.com' :
  ensure       => present,
  host_aliases => 'server',
  ip           => '1.1.1.1',
}
# puppet apply /tmp/foo.pp 
Notice: Compiled catalog for myhost in environment production in 0.14 seconds
Notice: /Stage[main]//Host[server.puppetlabs.com]/ensure: created
Notice: Finished catalog run in 0.26 seconds
# grep -P "1.1.1.1\tserver.puppetlabs.com\tserver" /etc/hosts
1.1.1.1 server.puppetlabs.com   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.puppetlabs.com\tserver' }
end
edit flag offensive delete link more

Comments

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.puppetlab.com server' } \n172.45.300.456 server.puppetlab.com 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.puppetlabs.com 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.puppetlabs.com\tserver" } and 2) its(:content) { should match /^172.45.300.456\tserver.puppetlabs.com\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

Stats

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

Seen: 153 times

Last updated: Dec 16 '15