簡體   English   中英

如何使用 python::virtualenv 在 puppet 中創建 python 環境變量?

[英]How do I create python environment variables in puppet using python::virtualenv?

我正在運行與 Slack 交互的 python 腳本。 我將 Slack api 令牌放入 python 腳本中

the_token = os.environ.get('SLACK_TOKEN')

我嘗試使用 python 環境

$var_name = 'SLACK_TOKEN'
$token = 'xxxx-xxxxxxxxxx-xxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxx'

python::virtualenv { $virtualenv_path:
  ensure       => present,
  requirements => '/opt/<dir>/<dir>/<dir>/requirements.txt'
  owner        => $::local_username,
  version      => '3',
  require      => [Class['<class>']],
  environment  => ["${var_name}=${token}"],
}

我認為“virtualenv”塊的最后一行會設置環境變量,但顯然不是。

manifests/virtualenv.pp中有一個 exec 正在運行 pip 命令來安裝/啟動虛擬環境(對不起,我不是 Python 虛擬環境的專家)。

      exec { "python_requirements_initial_install_${requirements}_${venv_dir}":
        command     => "${pip_cmd} --log ${venv_dir}/pip.log install ${pypi_index} ${proxy_flag} --no-binary :all: -r ${requirements} ${extra_pip_args}",
        refreshonly => true,
        timeout     => $timeout,
        user        => $owner,
        subscribe   => Exec["python_virtualenv_${venv_dir}"],
        environment => $environment,  <----- HERE
        cwd         => $cwd,
      }

When the exec runs, Puppet opens a shell, pipes in the environment variables, runs the command and closes the shell so the environment variables exist within the shell Puppet started but not in the Python environment it created. function 的目的可能是設置命令路徑,如果它們位於不同的位置或傳遞代理配置,因此 pip 可以從外部站點拉入包。

我驗證了 exec 正在處理您使用它正確發送的內容。

class test {

$var_name = 'test'
$token = 'xxxx-xxxxxxxxxx-xxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxx'

  exec { "test":
    command     => "/bin/env > /tmp/env.txt",
    environment => ["${var_name}=${token}"],
  }
}

但是你會注意到我不得不將 exec 運行的環境轉儲到一個文件中才能看到它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM