Question

I have a sequence of exec in my Puppet manifest:

  1. The first one downloads ZIP file with binary (unless the binary has already been installed) and saves it to /tmp.
  2. The second one unzips it.

When I apply the manifest for the first time, it works correctly. However, when I clean my /tmp and apply the manifest again, it fails because the first exec doesn't executed (that is correct), but the second still tries to execute and fails because ZIP file is not present.

How do I modify the manifest to skip the second exec if the first one doesn't download file?

exec { 'ngrok-download':
  command => 'wget https://dl.ngrok.com/linux_386/ngrok.zip -O /tmp/ngrok.zip',
  unless  => 'which ngrok',
  path    => ['/bin', '/usr/bin'],
}

exec { 'ngrok-unzip':
  command => 'unzip ngrok.zip',
  cwd     => '/tmp',
  path    => ['/usr/bin'],
  require => Exec['ngrok-download'],
}
Was it helpful?

Solution

Try this:

exec { 'ngrok-download':
  command => 'wget https://dl.ngrok.com/linux_386/ngrok.zip -O /tmp/ngrok.zip',
  unless  => 'which ngrok',
  path    => ['/bin', '/usr/bin'],
  notify  => Exec['ngrok-unzip'],
}

exec { 'ngrok-unzip':
  command     => 'unzip ngrok.zip',
  cwd         => '/tmp',
  path        => ['/usr/bin'],
  refreshonly => true,
  require     => Exec['ngrok-download'],
}

This will result in the unzip exec only running when the wget exec actually does something -- which it won't if ngrok is found.

Normally I would wget it to a more permanent location and leave it there. Then instead of the unless => 'which ngrok' check, replace with creates => '/path/to/zip.file'. The result being as long as the file is still there, none of the execs fire.

Comes in handy when you version the zip files and want to change versions.

OTHER TIPS

You could also try easier approach:

exec { 'ngrok-download':
  command => 'wget https://dl.ngrok.com/linux_386/ngrok.zip -O /tmp/ngrok.zip',
  unless  => 'which ngrok',
  path    => ['/bin', '/usr/bin'],
} ~>

exec { 'ngrok-unzip':
  command     => 'unzip ngrok.zip',
  cwd         => '/tmp',
  path        => ['/usr/bin'],
  refreshonly => true,
}

Where Exec['ngrok-download'] notifies Exec['ngrok-unzip'] if applied and Exec['ngrok-unzip'] refresh its state only if needed

Same thing can be achieved by doing following:

exec { 'ngrok-download':
  command => 'wget https://dl.ngrok.com/linux_386/ngrok.zip -O /tmp/ngrok.zip',
  unless  => 'which ngrok',
  path    => ['/bin', '/usr/bin'],
}

exec { 'ngrok-unzip':
  command     => 'unzip ngrok.zip',
  cwd         => '/tmp',
  path        => ['/usr/bin'],
  refreshonly => true,
}

Exec['ngrok-download'] ~> Exec['ngrok-unzip']

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top