cookbook 'hostsfile', '= 0.2.1', :supermarket
Provides an resource for managing the /etc/hosts file
cookbook 'hostsfile', '= 0.2.1'
knife supermarket install hostsfile
knife supermarket download hostsfile
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.
<table> <tr> <th>Attribute</th> <th>Description</th> <th>Example</th> <th>Default</th> </tr> <tr> <td>ip_address</td> <td>(name attribute) the IP address for the entry</td> <td><tt>18.104.22.168</tt></td> <td></td> </tr> <tr> <td>hostname</td> <td>(required) the hostname associated with the entry</td> <td><tt>example.com</tt></td> <td></td> </tr> <tr> <td>aliases</td> <td>array of aliases for the entry</td> <td><tt>['www.example.com']</tt></td> <td><tt></tt></td> </tr> <tr> <td>comment</td> <td>a comment to append to the end of the entry</td> <td><tt>'interal DNS server'</tt></td> <td><tt>nil</tt></td> </tr> <tr> <td>priority</td> <td>the relative position of this entry</td> <td><tt>20</tt></td> <td>(varies, see Priorities section)</td> </tr> </table>
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:
22.214.171.124 example.com www.example.com
hostsfile_entry '126.96.36.199' do hostname 'www.example.com' end
would yield an /etc/hosts file like this:
188.8.131.52 example.com 184.108.40.206 www.example.com
Creates a new hosts file entry. If an entry already exists, it will be overwritten by this one.
hostsfile_entry '220.127.116.11' do hostname 'example.com' action :create end
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 '18.104.22.168' do hostname 'example.com' action :create_if_missing end
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.
22.214.171.124 example.com www.example.com # Created by Chef
hostsfile_entry '126.96.36.199' do hostname 'www2.example.com' aliases ['foo.com', 'foobar.com'] comment 'Append by Recipe X' end
188.8.131.52 example.com www.example.com www2.example.com foo.com foobar.com # Created by Chef, Appended by Recipe X
Updates the given hosts file entry. Does nothing if the entry does not exist.
hostsfile_entry '184.108.40.206' do hostname 'example.com' comment 'Update by Chef' action :update end
This will create an entry like this:
220.127.116.11 example # Updated by Chef
Removes an entry from the hosts file. Does nothing if the entry does not exist.
hostsfile_entry '18.104.22.168' do action :remove end
This will remove the entry for
If you're using Berkshelf, just add
hostsfile to your
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 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:
- Local, loopback
However, you can override it using the
- Fork the project
- Create a feature branch corresponding to you change
- Commit and test thoroughly
- Create a Pull Request on github
- ensure you add a detailed description of your changes
License and Authors
Copyright 2012, CustomInk, LLC
This cookbook has no specified dependencies.