Ask Your Question

Run a command conditionally just for the first time

asked 2014-01-10 08:55:02 -0500

jaksky gravatar image

updated 2014-01-10 08:55:44 -0500


during the installation process of the cluster I need to run some command just once on the cluster from any node. So that means that before execution need to check the state and if that hasn't been execute yet than perform the action.

For sure that will be achieved by Exec.

Is there any good way how to approach that?

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2014-01-12 11:22:08 -0500

spuder gravatar image

updated 2014-01-12 19:48:14 -0500

Leave a breadcrumb, and then only execute if the breadcrumb is absent. In my case the breadcrumb is a file called '.gitlabdatabasedone'

Then do a test -f on that file.

  unless => '/usr/bin/test -f .gitlab_database_done

Here is an example from one of my manifests.

  # Setup gitlab database
  exec { 'setup gitlab database':
    cwd     => "${gitlab::git_home}/gitlab",
    command => '/usr/bin/yes yes | bundle exec rake gitlab:setup RAILS_ENV=production',
    unless  => "/usr/bin/test -f ${gitlab::git_home}/.gitlab_database_done",
    timeout => 600,
    before  => File["${gitlab::git_home}/.gitlab_database_done"],

  # Trap door to only allow database setup once
  file { "${gitlab::git_home}/.gitlab_database_done":
    ensure  => present,
    content ...
edit flag offensive delete link more



instead of `unless => test -f ...` you can also use `creates => ...`. This is more readable (at least in my opinion) and should also be faster because puppet does not have to ...(more)

Stefan gravatar imageStefan ( 2014-01-12 14:59:02 -0500 )edit

I agree with Stefan, use creates whenever you can it is much more readable.

doc75 gravatar imagedoc75 ( 2014-01-13 03:36:08 -0500 )edit

answered 2014-01-10 10:50:01 -0500

lavaman gravatar image
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2014-01-10 08:55:02 -0500

Seen: 13,170 times

Last updated: Jan 12 '14