Puppet 测试并删除 files/folders 的数组
Puppet test and remove an array of files/folders
我想让下面的代码以某种方式工作,看来如果我不先测试 files/folders,我就会得到错误:
Error: Failed to apply catalog: Parameter path failed on
File[/opt/dynatrace-6.2]: File paths must be fully qualified, not
'["/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"]' at
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}') ; [ ! -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"],
}
您的代码看起来基本上是正确的,但是您对 file
资源的使用过火了:
file { $dtdeps:
require => Exec["check_presence"],
path => $dtdeps,
...
这确实从您的数组创建所有文件资源(因为您使用数组作为资源标题)但是它们中的每一个 然后将尝试使用与 path
值相同的数组,这没有意义。
TL;DR 删除 path
参数,它应该可以正常工作。
你实际上可以简化很多。 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}'))"],
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"",
}
我想让下面的代码以某种方式工作,看来如果我不先测试 files/folders,我就会得到错误:
Error: Failed to apply catalog: Parameter path failed on File[/opt/dynatrace-6.2]: File paths must be fully qualified, not '["/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"]' at 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}') ; [ ! -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"],
}
您的代码看起来基本上是正确的,但是您对 file
资源的使用过火了:
file { $dtdeps:
require => Exec["check_presence"],
path => $dtdeps,
...
这确实从您的数组创建所有文件资源(因为您使用数组作为资源标题)但是它们中的每一个 然后将尝试使用与 path
值相同的数组,这没有意义。
TL;DR 删除 path
参数,它应该可以正常工作。
你实际上可以简化很多。 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}'))"],
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"",
}