10

I'd like to know how to set up :datadir: in hiera.yaml for optimal usage with Puppet and Vagrant. Currently I'm using vagrant 1.5.0 with virtualbox 4.2 on Ubuntu 13.10 with an Ubuntu 12.04 guest running puppet 3.1.1

I am trying to set up an environment similar to this blog post, Puppet Best Practices: Environment specific configs. Specifically, my Vagrantfile contains:

  config.vm.define "servername" do |servername|
    servername.vm.box = "precise-puppet-3"
    servername.vm.network "private_network", ip: "192.168.213.2",
      virtualbox__intnet: "networkname"

    # Provision with puppet.
    servername.vm.provision :puppet do |puppet|
      puppet.hiera_config_path = "puppet/hiera.yaml"
      puppet.manifests_path = "puppet/manifests"
      puppet.module_path = "puppet/modules"
      puppet.manifest_file  = "servername.pp"
      puppet.facter = {
        "vagrant" => "1",
        "server" => "servername",
      }
    end
  end

I can confirm that the hiera_config_path is correct, because I get an error if I delete hiera.yaml.

puppet/hiera.yaml contains:

---
:backends: yaml
:yaml:
  :datadir: "manifests/configuration"
:hierarchy:
  - "%{::clientcert}"
  - "%{::environment}"
  - "virtual_%{::is_virtual}"
  - common
:logger: console

And, further, puppet/manifests/configuration/common.yaml contains:

---
myvar: "test"

Testing this from the commandline:

$ hiera -c hiera.yaml myvar
test

So far, so good. However, if I try to test this from within a puppet manifest file, the variable cannot be found, and I get an error. Example test:

$myvariable = hiera(myvar)
notice("My variable is: ${myvar}")

The error is:

Error: Could not find data item myvar in any Hiera data file and no default supplied at...

If I ssh into my machine via vagrant ssh, I can see that Vagrant is mounting my manifest directory at /tmp/vagrant-puppet-2. If I edit the hiera.yaml file, and replace :datadir: with the full path /tmp/vagrant-puppet-2/manifests/configuration, then my Puppet manifests can access my Hiera data. Can I do this with a relative path, though?

sebix
  • 4,432

4 Answers4

9

I found the solution while documenting my question. Change :datadir: to read:

  :datadir: "%{settings::manifestdir}/configuration"

Puppet will provide the path to the manifest directory in $settings::manifestdir. Storing the Hiera data inside the manifest directory is useful because Vagrant will mount this directory explicitly before running Puppet in the guest system, and other directories you might select for this purpose might not be available.

2

The hiera.yaml I'm working with specifies :datadir: /etc/puppet/hiera and I had no luck with setting the --yamldir option as some of the other answers specified. However, I realised after a while that I could just map my hieradata to that location on the guest vm:

config.vm.synced_folder "../puppet/hiera", "/etc/puppet/hiera"

This works nicely :-)

1

This is what I am doing in my own puppet experiments.

VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "puppetlabs/debian-7.8-64-puppet" # source box on atlas
  config.vm.hostname = "wheezybox"                  # hostname of box

  # Include Hiera Data Directory (no automatic option for this)
  config.vm.synced_folder "../../hieradata", "/tmp/vagrant-puppet/hieradata"

  # Puppet Configuration
  config.vm.provision :puppet do |puppet|
    puppet.manifests_path    = "../../manifests/"
    puppet.manifest_file     = "site.pp"
    puppet.module_path       = ["../../modules/"]    # shared modules
    puppet.hiera_config_path = "../../hiera.yaml"    # hiera config file
    puppet.working_directory = "/tmp/vagrant-puppet" # default hiera path
    puppet.options           = "--verbose --debug"
  end
end

My minimalist hiera.yaml looks like this:

---
:backends:
  - yaml
:yaml:
  :datadir: "hieradata"
:hierarchy:
  - "node/%{::hostname}"

And for illustration purposes, my directory structure on the host (MacBook) looks like this:

    .
    ├── hiera.yaml
    ├── hieradata
    │   └── node
    │       ├── centos6box.yaml
    │       ├── precisebox.yaml
    │       └── wheezybox.yaml
    ├── manifests
    │   └── site.pp
    ├── modules -> ../puppet-common/modules/
    └── vagrants
        ├── README.md
        ├── centos6
        │   └── Vagrantfile
        ├── precise
        │   └── Vagrantfile
        └── wheezy
            └── Vagrantfile
0

Your original issue was/is that :datadir needs to be an absolute path. Hiera does not allow you to specify relative paths for the :datadir. If you feel this should be allowed, please submit a request to change it.

manifestdir is deprecated. You may prefer to use yamldir instead. You can override that setting when you pass puppet apply.

For vagrant:

 servername.vm.provision :puppet, :options => ["--yamldir some/absolute/path"]  do |puppet|
  puppet.hiera_config_path = "puppet/hiera.yaml"
  puppet.manifests_path = "puppet/manifests"
  puppet.module_path = "puppet/modules"
  puppet.manifest_file  = "servername.pp"
  puppet.facter = {
    "vagrant" => "1",
    "server" => "servername",
  }
end

UPDATE: Since you need to provide an absolute path (and because vagrant), you should set up your own shared folder so you can be explicit about where it is and not make assumptions on a vagrant set path for puppet execution. Add this to your Vagrantfile:

config.vm.synced_folder "puppet/manifests/configuration", "/hieradata"

and then change the first line above to:

servername.vm.provision :puppet, :options => ["--yamldir /hieradata"]  do |puppet|