Search code examples
rubylinuxchef-infranfs

How do I add a NFS share using Chef in an idempotent way?


I want to share folder using NFS in Linux using Chef. So far I've got this:

#share the path using NFS
File.open("/etc/exports", 'a') do |file|
    file.puts FS_BASE_PATH+" *(rw)"
  end

Which works, once. Subsequent provisioning duplicates this line.

Do I continue down this path, checking the file contents to see if it already contains the line in Ruby? OR, is there a command to configure the NFS share without having to edit the exports file directly, which sorts this out on my behalf? In Windows I'd use NET SHARE.

The flavour of Linux and NFS is: Ubuntu 12.04, nfs-kernel-server


Solution

  • It is generally recommended to manage the entire file by Chef. In this case, I'd use an attribute of the exported shares in the "nfs" cookbook (or whatever you name it), and then use a "template" resource to manage the content of the file. Something like this in the cookbook's attributes file, or applied via a role:

    default['nfs']['exports'] = ["/srv/export 10.0.0.0/8(ro,sync,no_root_squash)"]
    

    The template in the recipe:

    template "/etc/exports" do
      source "exports.erb"
      mode 00644
    end
    

    Then the template itself:

    <%- node['nfs']['exports'].each do |line| %>
    <%= line %>
    <%- end %>
    

    Of course, there's a very nice NFS cookbook already, published by an excellent cookbook author on the Chef Community Site (nfs cookbook). It is very complete, including tests.