Code Monkey home page Code Monkey logo

hostsfile's Introduction

hostsfile LWRP

Build Status

hostsfile provides an LWRP for managing your /etc/hosts (or Windows equivalent) file using Chef.

Requirements

  • Chef 11 or higher
  • Ruby 1.9.3 or higher

Please stop opening Pull Requests to restore Ruby 1.8 support! Any of the 1.x.y series of this cookbook will work with Chef 10 and Ruby 1.8. You can use Chef's Omnibus installer to install Ruby 1.9+ and Seth Chisamore's Vagrant Omnibus plugin to get Ruby 1.9+ on your Vagrant box.

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
unique remove any existing entries that have the same hostname true false
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: In v0.1.2, specifying a hostname or alias that existed in another automatically removed that hostname from the other entry before. In v2.1.0, the unique option was added to give the user case-by-case control of this behavior. For example, given an /etc/hosts file that contains:

1.2.3.4          example.com www.example.com

when the Chef recipe below is converged:

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

then the /etc/hosts file will look like this:

1.2.3.4          example.com
2.3.4.5          www.example.com

Not specifying the unique parameter will result in duplicate hostsfile entries.

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 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'

Note that you can specify a custom path to your hosts file in the ['hostsfile']['path'] node attribute. Otherwise, it defaults to sensible paths depending on your OS.

Testing

If you are using ChefSpec to unit test a cookbook that implements the hostsfile_entry LWRP, this cookbook packages customer matchers that you can use in your unit tests:

  • append_hostsfile_entry
  • create_hostsfile_entry
  • create_hostsfile_entry_if_missing
  • remove_hostsfile_entry
  • update_hostsfile_entry

For example:

it 'creates a hostsfile entry for the DNS server' do
  expect(chef_run).to create_hostsfile_entry('1.2.3.4')
    .with_hostname('dns.example.com')
end

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. 127.0.0.1
  2. ::1
  3. 127.0.0.0/8
  4. IPV4
  5. IPV6
  6. default

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

License & Authors

Copyright 2012-2013, Seth Vargo
Copyright 2012, 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.

hostsfile's People

Contributors

sethvargo avatar guilhem avatar bdclark avatar e100 avatar d-higuchi avatar nkadel-skyhook avatar satiani avatar i-gantt avatar bookest avatar wk8 avatar nathenharvey avatar timurb avatar

Watchers

James Cloos avatar Blufe avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.