仅为供应商设置环境变量

Set an env var for only the provisioner

我需要在 $PATH 前面添加一个环境变量:

  1. 持续时间不超过预配 运行。
  2. 是相关的,即会在 运行 之前安装一些东西,即 然后 可以通过 $PATH 使用,所以我不能全局设置它as this cookbook says to.

我试过了the answer here:

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

但我收到错误 Error: All resource specifications require names。当我添加一个名字时,我得到了关于语法的其他错误,为此我摆弄修复只是给了我其他错误(错误 Syntax error at '}'; expected '}' 是我最喜欢的!)

我试过使用 export 来设置它,但我看到:Error: Could not find command 'export'

我也尝试过使用 setsetenv,结果相似。一定有一个简单的方法可以做到这一点,但我找不到它。

非常感谢任何帮助或见解。


编辑:补充一下,这些是可用的 shell:

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

zsh 是配置的一部分,但如果需要,它可能是答案的要求。

添加到你的路径前面,你想像这样添加你的资源默认我相信:

Exec { environment => "PATH=value:$PATH", }

这可能不正确,但我知道它会替换您设置的变量,默认情况下不会附加到它们。更多详情请见 https://docs.puppetlabs.com/puppet/latest/reference/type.html#exec-attribute-environment

我为此尝试了几种方法,但我发现最好的方法是使用 Hiera。我也阅读了很多关于如何使用 Vagrant 进行设置的博客,但是 this was the best 我找到了。

我的项目目录布局

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

Vagrantfile

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

我还不知道为什么需要 hiera.yaml 指向 common.yaml,但事实就是这样。

hiera.yaml

---
:backends:
  - yaml

:hierarchy:
  - "common"

:yaml:
  :datadir: '/vagrant/pp/data'

common.yaml

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

然后在清单中

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

对我来说似乎付出了很多努力,但事实就是如此。