Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
158 lines (123 sloc) 3.67 KB

hostsfile LWRP

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


At this time, you must have a Unix-based machine. This could easily be adapted for Windows machines. Please submit a Pull Request if you wish to add Windows support.


Attribute Description Example Default
ip_address (name attribute) the IP address for the entry
hostname (required) the hostname associated with the entry
aliases array of aliases for the entry [''] []
comment a comment to append to the end of the entry 'interal DNS server' nil


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:


hostsfile_entry '' do
  hostname ''

would yield an /etc/hosts file like this:


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

hostsfile_entry '' do
  hostname ''
  action :create

This will create an entry like this:


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 '' do
  hostname ''
  action :create_if_missing


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. # Created by Chef
hostsfile_entry '' do
  hostname ''
  aliases ['', '']
  comment 'Append by Recipe X'

would yield: # Created by Chef, Appended by Recipe X


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

hostsfile_entry '' do
  hostname ''
  comment 'Update by Chef'
  action :update

This will create an entry like this:           example # Updated by Chef


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

hostsfile_entry '' do
  action :remove

This will remove the entry for


Download or install this cookbook from the community site:

$ knife cookbook site install hostsfile

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

# metadata.rb
depends 'hostsfile'


  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

Authors: Seth Vargo (@sethvargo)

Copyright 2012, CustomInk, LLC

Something went wrong with that request. Please try again.