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,175
  • 2
  • 25
  • 45
greg_1_anderson
  • 291
  • 1
  • 2
  • 6

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.

greg_1_anderson
  • 291
  • 1
  • 2
  • 6
  • Does this mean I need 2 `hiera.yaml` files, one for use with vagrant, and one for use with Puppet (in production)? – Felipe Alvarez Jun 02 '16 at 04:52
  • I only ever had one hiera.yaml file. I am not using this setup any longer, but I believe that the technique in this answer no longer works in recent versions of Puppet. If you have trouble with it, please see some of the other answers. – greg_1_anderson Jun 02 '16 at 14:11
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 :-)

  • This was exactly the set up that I had. I created a mapped folder `config.vm.synced_folder("D:/branches/preprod/hieradata", "/etc/puppet/hieradata")` and also specified `puppet.hiera_config_path = "D:/branches/preprod/hiera.yaml"` in puppet configuration stanzas. – Felipe Alvarez May 18 '16 at 05:18
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|
ferventcoder
  • 347
  • 3
  • 4
  • 12
  • Thanks for the reference to --yamldir; that is very helpful. The issue, though, is that I need to use a relative path. If I put in an absolute path, I would have to use ["--yamldir /tmp/vagrant-puppet-2/manifests/configuration"]; however, I can't trust that /tmp/vagrant-puppet-2 is going to be a stable path. Is there a variable I can reference in the Vagrantfile that contains this path? Or can I set some variable to stipulate what path to use instead of /tmp/vagrant-puppet-2? Anything that gets away from making assumptions about what paths Vagrant is going to use would work. – greg_1_anderson Feb 24 '15 at 23:06
  • @greg_1_anderson you can set your own directories, that way you are explicit about the directory and not making assumptions on vagrant. `config.vm.synced_folder "puppet/manifests/configuration", "/hieradata"` – ferventcoder Feb 25 '15 at 12:52
  • Updated the answer. Also if you feel that relative path would be helpful, please create a ticket at https://tickets.puppetlabs.com/browse/HI - thanks! – ferventcoder Feb 25 '15 at 12:57
  • Puppet does allow relative paths for the hiera datadir configuration when running `puppet apply`. The datadir will be relative to the current dir where Puppet was run. – Nathan Oct 28 '16 at 10:10