0

我希望以某种方式使以下代码工作,似乎如果我不首先测试文件/文件夹,我最终会遇到错误:

错误:无法应用目录:文件[/opt/dynatrace-6.2]上的参数路径失败:文件路径必须是完全限定的,而不是'[“/opt/dynatrace-6.2”、“/opt/dynatrace-5.6.0” , "/opt/rh/httpd24/root/etc/httpd/conf.d/dtload.conf", "/opt/rh/httpd24/root/etc/httpd/conf.d/01_dtagent.conf"]' 在 newrelic .pp:35

相关部分

$dtdeps = [
  "/opt/dynatrace-6.2",
  "/opt/dynatrace-5.6.0",
  "${httpd_root}/conf.d/dtload.conf",
  "${httpd_root}/conf.d/01_dtagent.conf",
]

exec { "check_presence":
  require => File[$dtdeps],
  command => '/bin/true',
  onlyif => "/usr/bin/test -e $dtdeps",
}

file { $dtdeps:
  require => Exec["check_presence"],
  path    => $dtdeps,
  ensure  => absent,
  recurse => true,
  purge   => true,
  force   => true,
} ## this is line 35 btw

exec { "stop_dt_agent":
  command => "PID=$(ps ax |grep dtwsagent |grep -v grep |awk '{print$1}') ; [ ! -z $PID ] && kill -9 $PID",
  provider => shell,
}

service { "httpd_restart" :
    ensure    => running,
    enable    => true,
    restart   => "/usr/sbin/apachectl configtest && /etc/init.d/httpd reload",
    subscribe => Package["httpd"],
}
4

2 回答 2

1

您的代码看起来基本正确,但您的file资源过度使用:

file { $dtdeps:
  require => Exec["check_presence"],
  path    => $dtdeps,
  ...

确实会从您的数组中创建所有文件资源(因为您使用数组作为资源标题)但是它们中的每一个都将尝试使用与值相同的数组path,这没有意义。

TL;DR 删除path参数,它应该可以正常工作。

于 2015-12-09T10:31:52.573 回答
1

您实际上可以简化很多。Puppet 仅在文件不存在时运行文件删除,因此不需要 check_presence exec。

您不能为路径提供数组,但可以将标题作为数组传递,然后自动生成路径。

$dtdeps = [
  "/opt/dynatrace-6.2",
  "/opt/dynatrace-5.6.0",
  "${httpd_root}/conf.d/dtload.conf",
  "${httpd_root}/conf.d/01_dtagent.conf",
]

file { $dtdeps:
  ensure  => absent,
  recurse => true,
  purge   => true,
  force   => true,
}

exec { "stop_dt_agent":
  command     => '[ ! -z $PID ] && kill -9 $PID',
  environment => ["PID=\$(ps ax |grep dtwsagent |grep -v grep |awk '{print$1}'))"],
  provider     => shell,
}

但是,运行 stop_dt_agent exec 有点脆弱。您可能可以将其重构为service资源:

  service { 'dynatrace':
    ensure   => stopped,
    provider => 'base',
    stop     => 'kill -TERM $(ps ax | grep \"dtwsagent\"|grep -v grep|awk '{print \$1}')',
    status   => "ps ax | grep "dtwsagent"",
  }
于 2015-12-09T10:35:24.313 回答