Showing with 809 additions and 3 deletions.
  1. +7 −0 .fixtures.yml
  2. +13 −0 .gitignore
  3. +16 −0 .travis.yml
  4. +7 −0 Gemfile
  5. +13 −0 LICENSE
  6. +11 −0 Modulefile
  7. +90 −3 README.md
  8. +18 −0 Rakefile
  9. +147 −0 manifests/init.pp
  10. +474 −0 spec/classes/init_spec.rb
  11. +2 −0 spec/spec_helper.rb
  12. +11 −0 tests/init.pp
7 changes: 7 additions & 0 deletions .fixtures.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fixtures:
repositories:
'stdlib':
repo: 'git://github.com/puppetlabs/puppetlabs-stdlib.git'
ref: '3.2.0'
symlinks:
'hosts': "#{source_dir}"
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Default .gitignore for Ruby
*.gem
*.rbc
.bundle
Expand All @@ -16,3 +17,15 @@ tmp
.yardoc
_yardoc
doc/

# Vim
*.swp

# OS X
.DS_Store

# Puppet
metadata.json
coverage/
spec/fixtures/modules/*
spec/fixtures
16 changes: 16 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
env:
- PUPPET_VERSION=2.7.23
- PUPPET_VERSION=3.3.2
notifications:
email: false
rvm:
- 1.9.3
- 1.8.7
matrix:
allow_failures:
- env: PUPPET_VERSION=2.7.23
language: ruby
before_script: "gem install --no-ri --no-rdoc bundler"
script: 'bundle exec rake validate && bundle exec rake lint && SPEC_OPTS="--format documentation" bundle exec rake spec'
gemfile: Gemfile
7 changes: 7 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
source "https://rubygems.org"

puppetversion = ENV.key?('PUPPET_VERSION') ? "= #{ENV['PUPPET_VERSION']}" : ['>= 2.7']
gem 'puppet', puppetversion
gem 'puppetlabs_spec_helper', '>= 0.1.0'
gem 'puppet-lint', '>= 0.3.2'
gem 'facter', '>= 1.7.0', "< 1.8.0"
13 changes: 13 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Copyright (C) 2010-2013 Garrett Honeycutt <code@garretthoneycutt.com>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
11 changes: 11 additions & 0 deletions Modulefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name 'ghoneycutt-hosts'
version '2.0.0'
source 'git://github.com/ghoneycutt/puppet-module-hosts.git'
author 'ghoneycutt'
license 'Apache License, Version 2.0'
summary 'Manages host entries'
description "Can ensure entries for localhost, localhost6, and $::fqdn,
including aliases and optionally purge unmanaged entries."
project_page 'https://github.com/ghoneycutt/puppet-module-hosts'

dependency 'puppetlabs/stdlib', '3.2.x'
93 changes: 90 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,91 @@
puppet-module-hosts
===================
hosts module
============

Puppet module to manage host entries
Manage host entries.

Can ensure entries for localhost, localhost6, and $::fqdn, including aliases
and optionally purge unmanaged entries.

[![Build Status](https://api.travis-ci.org/ghoneycutt/puppet-module-hosts.png?branch=master)](https://travis-ci.org/ghoneycutt/puppet-module-hosts)

===

# Compatibility

This module targets Puppet v3. It should work with any *nix based system that uses `/etc/hosts`.

===

# Parameters

enable_ipv4_localhost
---------------------
Boolean to enable ipv4 localhost entry

- *Default*: true

enable_ipv6_localhost
---------------------
Boolean to enable ipv6 localhost entry

- *Default*: true

enable_fqdn_entry
-----------------
Boolean to enable entry for fqdn

- *Default*: true

fqdn_host_aliases
-----------------
String or Array of aliases for fqdn

- *Default*: $::hostname

localhost_aliases
-----------------
String or Array of aliases for localhost

- *Default*: [ 'localhost', 'localhost4', 'localhost4.localdomain4' ]

localhost6_aliases
------------------
String or Array of aliases for localhost6

- *Default*: [ 'localhost6', 'localhost6.localdomain6' ]

purge_hosts
-----------
Boolean to optionally purge unmanaged entries from hosts

- *Default*: false

target
------
String for path to hosts file

- *Default*: /etc/hosts

collect_all
-----------
Boolean to optionally collect all the exported Host resources

- *Default*: false

host_entries
------------
Hash of host entries

- *Default*: undef

===

# Hiera example of host_entries
<pre>
---
hosts::host_entries:
'servicename.example.com':
ip: '10.0.0.5'
host_aliases:
- 'servicename'
</pre>
18 changes: 18 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'rubygems'
require 'puppetlabs_spec_helper/rake_tasks'
require 'puppet-lint/tasks/puppet-lint'
PuppetLint.configuration.send('disable_80chars')
PuppetLint.configuration.ignore_paths = ["spec/**/*.pp", "pkg/**/*.pp"]

desc "Run puppet in noop mode and check for syntax errors."
task :validate do
Dir['manifests/**/*.pp'].each do |manifest|
sh "puppet parser validate --noop #{manifest}"
end
Dir['spec/**/*.rb','lib/**/*.rb'].each do |ruby_file|
sh "ruby -c #{ruby_file}" unless ruby_file =~ /spec\/fixtures/
end
Dir['templates/**/*.erb'].each do |template|
sh "erb -P -x -T '-' #{template} | ruby -c"
end
end
147 changes: 147 additions & 0 deletions manifests/init.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
# == Class: hosts
#
# Manage /etc/hosts
#
class hosts (
$collect_all = false,
$enable_ipv4_localhost = true,
$enable_ipv6_localhost = true,
$enable_fqdn_entry = true,
$fqdn_host_aliases = $::hostname,
$localhost_aliases = ['localhost',
'localhost4',
'localhost4.localdomain4'],
$localhost6_aliases = ['localhost6',
'localhost6.localdomain6'],
$purge_hosts = false,
$target = '/etc/hosts',
$host_entries = undef,
) {


# validate type and convert string to boolean if necessary
$collect_all_type = type($collect_all)
if $collect_all_type == 'string' {
$collect_all_real = str2bool($collect_all)
} else {
$collect_all_real = $collect_all
}

# validate type and convert string to boolean if necessary
$enable_ipv4_localhost_type = type($enable_ipv4_localhost)
if $enable_ipv4_localhost_type == 'string' {
$ipv4_localhost_enabled = str2bool($enable_ipv4_localhost)
} else {
$ipv4_localhost_enabled = $enable_ipv4_localhost
}

# validate type and convert string to boolean if necessary
$enable_ipv6_localhost_type = type($enable_ipv6_localhost)
if $enable_ipv6_localhost_type == 'string' {
$ipv6_localhost_enabled = str2bool($enable_ipv6_localhost)
} else {
$ipv6_localhost_enabled = $enable_ipv6_localhost
}

# validate type and convert string to boolean if necessary
$enable_fqdn_entry_type = type($enable_fqdn_entry)
if $enable_fqdn_entry_type == 'string' {
$fqdn_entry_enabled = str2bool($enable_fqdn_entry)
} else {
$fqdn_entry_enabled = $enable_fqdn_entry
}

# validate type and convert string to boolean if necessary
$purge_hosts_type = type($purge_hosts)
if $purge_hosts_type == 'string' {
$purge_hosts_enabled = str2bool($purge_hosts)
} else {
$purge_hosts_enabled = $purge_hosts
}

if $ipv4_localhost_enabled == true {
$localhost_ensure = 'present'
$localhost_ip = '127.0.0.1'
$my_localhost_aliases = $localhost_aliases
} else {
$localhost_ensure = 'absent'
$localhost_ip = '127.0.0.1'
$my_localhost_aliases = ''
}

if $ipv6_localhost_enabled == true {
$localhost6_ensure = 'present'
$localhost6_ip = '::1'
$my_localhost6_aliases = $localhost6_aliases
} else {
$localhost6_ensure = 'absent'
$localhost6_ip = '::1'
$my_localhost6_aliases = undef
}

$my_localhost_aliases_type = type($my_localhost_aliases)
if $my_localhost_aliases_type != 'string' and $my_localhost_aliases_type != 'array' {
fail("hosts::localhost_aliases must be a string or an array. Detected type is <${my_localhost_aliases_type}>.")
}

$my_localhost6_aliases_type = type($my_localhost6_aliases)
if $my_localhost6_aliases_type != 'string' and $my_localhost6_aliases_type != 'array' {
fail("hosts::localhost6_aliases must be a string or an array. Detected type is <${my_localhost6_aliases_type}>.")
}

if $fqdn_entry_enabled == true {
$fqdn_ensure = 'present'
$my_fqdn_host_aliases = $fqdn_host_aliases
$fqdn_ip = $::ipaddress
} else {
$fqdn_ensure = 'absent'
$my_fqdn_host_aliases = ''
$fqdn_ip = ''
}

Host {
target => $target,
}

host { 'localhost':
ensure => 'absent',
}

host { 'localhost.localdomain':
ensure => $localhost_ensure,
host_aliases => $my_localhost_aliases,
ip => $localhost_ip,
}

host { 'localhost6.localdomain6':
ensure => $localhost6_ensure,
host_aliases => $my_localhost6_aliases,
ip => $localhost6_ip,
}

@@host { $::fqdn:
ensure => $fqdn_ensure,
host_aliases => $my_fqdn_host_aliases,
ip => $fqdn_ip,
}

case $collect_all_real {
# collect all the exported Host resources
true: {
Host <<| |>>
}
# only collect the exported entry above
default: {
Host <<| title == $::fqdn |>>
}
}

resources { 'host':
purge => $purge_hosts,
}

if $host_entries != undef {
validate_hash($host_entries)
create_resources(host,$host_entries)
}
}
Loading