Puppet test and remove an array of files/folders

淺唱寂寞╮ 提交于 2020-01-06 15:30:43

问题


I'm looking to make the following code work somehow, it seems if i do not test the files/folders first I end up with the error:

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

The pertinent parts

$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"],
}

回答1:


Your code looks basically correct, but you went overboard with your file resources:

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

This does create all the file resources from your array (since you use an array for the resource title) but each single one of them will then try to use the same array as the path value, which does not make sense.

TL;DR remove the path parameter and it should Just Work.




回答2:


You can actually simplify this down a lot. Puppet only runs the file removal if the files don't exist, so the check_presence exec is not required.

You can't give a path an array, but you can pass the title as an array and then the paths get automatically made.

$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,
}

However, running the stop_dt_agent exec is a bit fragile. You could probably refactor this into a service resource instead:

  service { 'dynatrace':
    ensure   => stopped,
    provider => 'base',
    stop     => 'kill -TERM $(ps ax | grep \"dtwsagent\"|grep -v grep|awk '{print \$1}')',
    status   => "ps ax | grep "dtwsagent"",
  }


来源:https://stackoverflow.com/questions/34163776/puppet-test-and-remove-an-array-of-files-folders

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!