首页 > 解决方案 > 无法使用 Chef Tomcat 设置环境变量

问题描述

我正在使用来自https://supermarket.chef.io/cookbooks/tomcat的食谱

instance_name = node['tomcat']['name']

# Install the Tomcat Service
tomcat_install instance_name do
  version node['tomcat']['version']
  dir_mode '0755'
  install_path node['tomcat']['install_dir']
  tarball_uri node['tomcat']['install_tar']
  tomcat_user node['tomcat']['user']
  tomcat_group node['tomcat']['group']
end

tomcat_service instance_name do
  action [:start, :enable]
  env_vars [{ 'CATALINA_PID' => '/opt/tomcat_helloworld/bin/non_standard_location.pid' }, { 'SOMETHING' => 'some_value' }]
  sensitive true
end

我应该找到一个名为 SOMETHING 的环境变量,但是当我这样做时

echo $SOMETHING

我没有找到,我也没有在#{derived_install_path}/bin/setenv.sh 中找到带有这个变量的setenv.sh,但是这个文件不存在。

标签: chef-infratomcat8cookbook

解决方案


自定义资源的env_vars属性tomcat_service设置 Tomcat 服务的环境变量instance_name这些不是 SHELL 环境变量。

tomcat_service 设置已安装的 tomcat 实例以使用适当的 init 系统(sys-v、upstart 或 systemd)运行

在 CentOS 中,服务由处理systemctl,因此这些变量被添加到相应的 Systemd 服务文件中。

例子:

/etc/systemd/system/tomcat_helloworld.service

截图内容:

Environment="CATALINA_BASE=/opt/tomcat_helloworld"
Environment="CATALINA_PID=/opt/tomcat_helloworld/bin/non_standard_location.pid"
Environment="SOMETHING=some_value"

推荐阅读