Puppet测试并删除一组文件/文件夹

时间:2015-12-08 18:55:39

标签: puppet dynatrace

我想以某种方式使下面的代码工作,似乎我不首先测试文件/文件夹我最终得到错误:

  

错误:无法应用目录:参数路径失败   文件[/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"],
}

2 个答案:

答案 0 :(得分:1)

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

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

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

TL; DR删除path参数,它应该只是工作。

答案 1 :(得分: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"",
  }