Puppet : file resource only if file exists

倖福魔咒の 提交于 2019-12-11 07:30:26

问题


What I want to do is quite simple :

1.

Copy /source/file to /target/file. I achieve this using the following:

file { 'my_file_copy':
  ensure   => file,
  source   => 'file:/source/file',
  path     => "/target/file",
}

2.

However, if file /source/file does not exist, I do NOT want it to perform this task.

I am really struggling with this logic. I attempted the solution below but it throws exceptions during puppet run.

puppet: if one file exists then copy another file over

Is there a better way of achieving this task ? Ideally, I would like to only use "file" and avoid using "exec". But at this point I would settle for a solution !


回答1:


Because Puppet is a declarative language where only the end-state is declared, imperative logic such as what you've described - if A, do X - is often hard to express.

Personally, I would try to simply avoid this requirement of having file B copied if and only if file A exists. Often there's a better way.

If the requirement needs to stay, however, then use of Exec here sounds like a pretty good option to me.

exec { 'my_file_copy':
  command => 'cp /source/file /target/file',
  onlyif  => 'test -e /source/file',
  creates => '/target/file',
  path    => '/bin',
}



回答2:


You could use this logic:

$file = "/source/file"

  exec { "chk_${file}_exist":
    command => "true",
    path    =>  ["/usr/bin","/usr/sbin", "/bin"],
    onlyif  => "test -f ${file}"
  }

  file {"/target/file":
    ensure => file,
    source => 'file:/source/file',
    owner  => 'root',
    group  => 'root',
    mode   => '0750',
    require => Exec["chk_${file}_exist"],
  }


来源:https://stackoverflow.com/questions/47251255/puppet-file-resource-only-if-file-exists

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