Search code examples
vagrantpuppet

Set an env var for only the provisioner


I need an environment variable added to the front of $PATH that:

  1. Doesn't last beyond the provisioning run.
  2. Is dependent i.e. something will be installed earlier in the run that is then is available via $PATH, so I can't set it globally as this cookbook says to.

I tried the answer here:

Exec { environment => [ "foo=$bar" ] }

but I get the error Error: All resource specifications require names. When I add a name I get other errors about syntax, for which my fiddling around to fix just gives me other errors (the error Syntax error at '}'; expected '}' is my favourite!)

I've tried using export to set it, but I see: Error: Could not find command 'export'

I've tried using set and setenv too, with similar results. There must be a straightforward way to do this, but I can't find it.

Edit

Just to add, these are the available shells:

$ cat /etc/shells
# /etc/shells: valid login shells
/bin/sh
/bin/dash
/bin/bash
/bin/rbash
/bin/zsh
/usr/bin/zsh

zsh is part of the provisioning, but it could be a requirement of the answer, if needs be.


Solution

  • I tried a few ways for this, but the best I found was to use Hiera. I read quite a few blogs on how to set this up with Vagrant too, but this was the best I found.

    My project directory layout

    Vagrantfile
    pp/
      manifests/
      modules/
      data/
        hiera.yml
        common.yml
    

    Vagrantfile

    The relevant part of the Vagrantfile:

      config.vm.provision "puppet" do |puppet|
        puppet.manifests_path = "pp/manifests"
        puppet.module_path = "pp/modules/custom"
        puppet.manifest_file  = "default.pp"
        puppet.hiera_config_path = "pp/data/hiera.yaml"
      end
    

    I've no idea yet why there needs to be a hiera.yaml which points to a common.yaml, but that's the way it is.

    hiera.yaml

    ---
    :backends:
      - yaml
    
    :hierarchy:
      - "common"
    
    :yaml:
      :datadir: '/vagrant/pp/data'
    

    common.yaml

    ---
    ruby_version: "2.3.0"
    ruby_prefix: "/opt/rubies"
    ...
    

    Then in a manifest

    $ruby_version = hiera("ruby_version")
    $ruby_prefix = hiera("ruby_prefix")
    $ruby_dir_fullpath = "${ruby_prefix}/ruby-${ruby_version}"
    

    Seems like a lot of effort to me, but again, that's the way it is.