Skip to content

Instantly share code, notes, and snippets.

@palmertime
Last active October 20, 2015 14:31
Show Gist options
  • Save palmertime/d4d7d36ab4138e54dbbf to your computer and use it in GitHub Desktop.
Save palmertime/d4d7d36ab4138e54dbbf to your computer and use it in GitHub Desktop.
describe 'hosts_file' do
let(:params) do {
:hosts_fqdn_aliases => ['test1.example.com','test1'],
'hosts_resources' => {
'extra' => {
'name' => 'extra.example.com',
'host_aliases' => ['extra', 'blah', 'blah.examplecar.com', 'blah.example.com'],
'ip' => '10.10.10.11',
}
},
}
end
let(:facts) do {
:operatingsystem => 'RedHat',
:operatingsystemmajrelease => '6',
:fqdn => 'test.example.com',
:hostname => 'test',
:ipaddress_eth0 => '10.10.10.10',
}
end
it 'should have an augeas resource' do
should contain_augeas('rename_ip6')
end
describe_augeas 'renameip6', :lens => 'hosts', :target => 'etc/hosts' do
it 'should change canonical' do
should execute.with_change
aug_get('canonical').should == 'localhost6'
should execute.idempotently
end
end
it do
should contain_host('localhost').with({
'ensure' => 'present',
'host_aliases' => ['localhost.localdomain', 'localhost4', 'localhost4.localdomain4'],
'ip' => '127.0.0.1',
})
should contain_host('localhost6').with({
'ensure' => 'present',
'host_aliases' => ['localhost.localdomain', 'localhost', 'localhost6.localdomain6'],
'ip' => '::1',
})
should contain_host('fqdn').with({
'name' => 'test.example.com',
'ensure' => 'present',
'host_aliases' => ['test', 'test1', 'test1.example.com'],
'ip' => '10.10.10.10',
})
should contain_host('extra').with({
'name' => 'extra.example.com',
'ensure' => 'present',
'host_aliases' => ['extra', 'blah', 'blah.examplecar.com', 'blah.example.com'],
'ip' => '10.10.10.11',
})
end
end
class hosts_file (
$hosts_resources = $hosts_file::params::hosts_resources,
$hosts_osVerArch = $hosts_file::params::hosts_osVerArch,
$hosts_fqdn_aliases = $hosts_file::params::hosts_fqdn_aliases,
$hosts_defaults = $hosts_file::params::hosts_defaults,
$hosts_aug_defaults = $hosts_file::params::hosts_aug_defaults,
$defaults = $hosts_file::params::defaults,
) inherits hosts_file::params
{
contain hosts_file::config
}
class hosts_file::params {
$hosts_resources = false
$hosts_osVerArch = "${::operatingsystem} ${::operatingsystemmajrelease}"
$hosts_fqdn_aliases = []
$defaults = {
ensure => 'present',
require => Augeas['rename_ip6'],
}
case $hosts_osVerArch {
'RedHat 6': {
$hosts_defaults = {
localhost => {
host_aliases => [ 'localhost.localdomain', 'localhost4', 'localhost4.localdomain4' ],
ip => '127.0.0.1',
},
localhost6 => {
host_aliases => [ 'localhost.localdomain', 'localhost', 'localhost6.localdomain6' ],
ip => '::1',
},
fqdn => {
name => $::fqdn,
host_aliases => concat([$::hostname],$hosts_fqdn_aliases),
ip => $::ipaddress_eth0,
},
}
$hosts_aug_defaults = {
rename_ip6 => {
context => '/files/etc/hosts',
changes => [
"set *[ipaddr = '::1']/canonical localhost6",
"set *[ipaddr = '::1']/alias[1] localhost.localdomain",
"set *[ipaddr = '::1']/alias[2] localhost",
"set *[ipaddr = '::1']/alias[3] localhost6.localdomain6",
],
}
}
}
default: {
fail("Module ${module_name} does not support the ${hosts_osVerArch} Operating System")
}
}
}
@rnelson0
Copy link

Change the hosts_file::params class to have an actual param. Then you could set hosts_file::params::hosts_fqdn_aliases via hiera to the correct value and all the classes in hosts_file would receive the correct value for hosts_file::params::hosts_default.

class hosts_file::params (
  $hosts_fqdn_aliases = [],
) {
  $hosts_resources    = false
  $hosts_osVerArch    = "${::operatingsystem} ${::operatingsystemmajrelease}"
 $defaults           = {
    ensure  => 'present',
    require => Augeas['rename_ip6'],
  }
  case $hosts_osVerArch {
    'RedHat 6': {
      $hosts_defaults = {
        localhost      => {
          host_aliases => [ 'localhost.localdomain', 'localhost4', 'localhost4.localdomain4' ],
          ip           => '127.0.0.1',
        },
        localhost6     => {
          host_aliases => [ 'localhost.localdomain', 'localhost', 'localhost6.localdomain6' ],
          ip           => '::1',
        },
        fqdn           => {
          name         => $::fqdn,
          host_aliases => concat([$::hostname],$hosts_fqdn_aliases),
          ip           => $::ipaddress_eth0,
        },
      }
      $hosts_aug_defaults = {
        rename_ip6 => {
          context  => '/files/etc/hosts',
          changes  => [
            "set *[ipaddr = '::1']/canonical localhost6",
            "set *[ipaddr = '::1']/alias[1] localhost.localdomain",
            "set *[ipaddr = '::1']/alias[2] localhost",
            "set *[ipaddr = '::1']/alias[3] localhost6.localdomain6",
          ],
        }
      }
    }
    default: {
      fail("Module ${module_name} does not support the ${hosts_osVerArch} Operating System")
    }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment