微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

通过人偶3.7.4中的环境变量动态设置卷曲路径,返回“找不到命令”

如何解决通过人偶3.7.4中的环境变量动态设置卷曲路径,返回“找不到命令”

我们的某些实例正在较旧的操作系统上运行,而curl命令的位置与其他实例不同,这是由于“ curl:command not found”错误导致在这些节点上运行的人偶失败导致的。

然后我们尝试设置command => export $curl_path=$(which curl) && $curl_path -sS -o ${service_to_install} https://service_to_install.parent.com/service_to_install || rm -f ${service_to_install}"

但由于export: command not found遇到了人偶失败,我发现另一则帖子建议使用environment =>设置curl_path变量,如下所示:


  $service_to_install = '/root/service_to_install'
  
  exec { 'service_to_install_placeholder':
    command => "/bin/echo Unauthorized > ${service_to_install)}",creates => $service_to_install,}
  # Download the installer.
  exec { 'fetch_service_to_install' :
    environment => ["curl_path=$(which curl)"],command => "$curl_path -sS -o ${service_to_install} https://service_to_install.parent.com/service_to_install || rm -f ${service_to_install}",path    => [ '/sbin','/bin','/usr/sbin','/usr/bin' ],timeout => 600,onlyif  => "/bin/grep -q Unauthorized ${service_to_install}",}
  # then we run the installer
  exec { 'agent_install' :
    command => "bash ${service_to_install} ",require => Exec['fetch_service_to_install'],}

但这会导致/Stage[main]/Jumpcloud_login/Exec[fetch_service_to_install]/returns Could not find command ''

我们如何在此处使用which curl命令动态设置curl?

如果很重要,我们看到错误的系统是Amazon Linux AMI版本2018.03 RedHat。

解决方法

这似乎是facts.os.namefacts.os.release.major(及类似)上的Hiera匹配的完美用例。您可以想象顶级hiera.yaml看起来像这样:

version: 5
defaults:
  datadir: data
  data_hash: yaml_data
hierarchy:
  - name: 'os-specific data'
    path: 'os/%{facts.os.name}-%{facts.os.release.major}.yaml'
  - name: 'common data'
    path: 'common.yaml'

然后从那里使用一个名为my_module::curl_path的变量,该变量可能在/usr/bin/curl中默认为common.yaml,否则将被较旧/不同的机器覆盖。

到那时,您的代码将变为:

curl_path = lookup('my_module::curl_path',String)

exec { 'fetch_service_to_install' :
  command => "${curl_path} -sS -o ${service_to_install} https://service_to_install.parent.com/service_to_install || rm -f ${service_to_install}",path    => [ '/sbin','/bin','/usr/sbin','/usr/bin' ],timeout => 600,onlyif  => "/bin/grep -q Unauthorized ${service_to_install}",}

(可能最好使其成为清单/模块参数,因此不需要lookup()。)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。