Skip to content

Easily manage your hosts file with Chef

License

Notifications You must be signed in to change notification settings

bcg62/hostsfile

 
 

Repository files navigation

hostsfile LWRP

Build Status

hostsfile provides an LWRP for managing your hosts file using Chef.

Requirements

  • Chef 10 or higher
  • Ruby 1.8.7 or higher

Attributes

Attribute Description Example Default
ip_address (name attribute) the IP address for the entry 1.2.3.4
hostname (required) the hostname associated with the entry example.com
aliases array of aliases for the entry ['www.example.com'] []
comment a comment to append to the end of the entry 'interal DNS server' nil
priority the relative position of this entry 20 (varies, see **Priorities** section)

Actions

Please note: As of v0.1.2, specifying a hostname or alias that exists in another entry will remove that hostname from the other entry before adding to this one. For example:

1.2.3.4          example.com www.example.com

and

hostsfile_entry '2.3.4.5' do
  hostname  'www.example.com'
end

would yield an /etc/hosts file like this:

1.2.3.4          example.com
2.3.4.5          www.example.com

create

Creates a new hosts file entry. If an entry already exists, it will be overwritten by this one.

hostsfile_entry '1.2.3.4' do
  hostname  'example.com'
  action    :create
end

This will create an entry like this:

1.2.3.4          example.com

create_if_missing

Create a new hosts file entry, only if one does not already exist for the given IP address. If one exists, this does nothing.

hostsfile_entry '1.2.3.4' do
  hostname  'example.com'
  action    :create_if_missing
end

append

Append a hostname or alias to an existing record. If the given IP address doesn't not already exist in the hostsfile, this method behaves the same as create. Otherwise, it will append the additional hostname and aliases to the existing entry.

1.2.3.4         example.com www.example.com # Created by Chef
hostsfile_entry '1.2.3.4' do
  hostname  'www2.example.com'
  aliases   ['foo.com', 'foobar.com']
  comment   'Append by Recipe X'
  action    :append
end

would yield:

1.2.3.4         example.com www.example.com www2.example.com foo.com foobar.com # Created by Chef, Appended by Recipe X

update

Updates the given hosts file entry. Does nothing if the entry does not exist.

hostsfile_entry '1.2.3.4' do
  hostname  'example.com'
  comment   'Update by Chef'
  action    :update
end

This will create an entry like this:

1.2.3.4           example # Updated by Chef

remove

Removes an entry from the hosts file. Does nothing if the entry does not exist.

hostsfile_entry '1.2.3.4' do
  action    :remove
end

This will remove the entry for 1.2.3.4.

Usage

If you're using Berkshelf, just add hostsfile to your Berksfile:

cookbook 'hostsfile'

Otherwise, install the cookbook from the community site:

knife cookbook site install hostsfile

Have any other cookbooks depend on hostsfile by editing editing the metadata.rb for your cookbook.

# metadata.rb
depends 'hostsfile'

Priority

Priority is a relatively new addition to the cookbook. It gives you the ability to (somewhat) specify the relative order of entries. By default, the priority is calculated for you as follows:

  1. Local, loopback
  2. IPV4
  3. IPV6

However, you can override it using the priority option.

Contributing

  1. Fork the project
  2. Create a feature branch corresponding to you change
  3. Commit and test thoroughly
  4. Create a Pull Request on github
    • ensure you add a detailed description of your changes

License and Authors

Copyright 2012 Seth Vargo, CustomInk, LLC

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.

About

Easily manage your hosts file with Chef

Resources

License

Stars

Watchers

Forks

Packages

No packages published