How to set up :datadir: when using Hiera with Puppet and Vagrant

10,969

Solution 1

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.

Solution 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 :-)

Solution 3

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
Share:
10,969

Related videos on Youtube

greg_1_anderson
Author by

greg_1_anderson

Co-maintainer of drush and author of the drush chapter of The Definitive Guide to Drupal 7.

Updated on September 18, 2022

Comments

  • greg_1_anderson
    greg_1_anderson over 1 year

    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?

  • greg_1_anderson
    greg_1_anderson about 9 years
    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.
  • ferventcoder
    ferventcoder about 9 years
    @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
    ferventcoder about 9 years
    Updated the answer. Also if you feel that relative path would be helpful, please create a ticket at tickets.puppetlabs.com/browse/HI - thanks!
  • Felipe Alvarez
    Felipe Alvarez almost 8 years
    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
    Felipe Alvarez almost 8 years
    Does this mean I need 2 hiera.yaml files, one for use with vagrant, and one for use with Puppet (in production)?
  • greg_1_anderson
    greg_1_anderson almost 8 years
    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.
  • Nathan
    Nathan over 7 years
    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.