in programming

Set Global Path Variable in Puppet

Puppet CookBook has some handy ‘recipes’ to supercharge your puppet workflow.

I found setting a global path variable, as the puppet cookbook suggested, to be extremely helpful and keeps the project DRY. You no longer have to set a path clause to each Exec command in your manifests.

Here is an example with multiple path declarations:

exec { "composer install phpunit":
  command => 'composer global require "phpunit/phpunit=4.4.*"',
  environment => ["COMPOSER_HOME=/usr/local/bin"],
  path    => '/usr/bin:/usr/local/bin:~/.composer/vendor/bin/',
  require => Exec['install composer']
}

exec { "composer install phploc":
  command => 'composer global require "phploc/phploc=*"',
  environment => ["COMPOSER_HOME=/usr/local/bin"],
  path    => '/usr/bin:/usr/local/bin:~/.composer/vendor/bin/',
  require => Exec['install composer']
}

exec { "composer install phpcpd":
  command => 'composer global require "sebastian/phpcpd=*"',
  environment => ["COMPOSER_HOME=/usr/local/bin"],
  path    => '/usr/bin:/usr/local/bin:~/.composer/vendor/bin/',
  require => Exec['install composer']
}

exec { "composer install phpcs":
  command => 'composer global require "squizlabs/php_codesniffer=2.1.*"',
  environment => ["COMPOSER_HOME=/usr/local/bin"],
  path    => '/usr/bin:/usr/local/bin:~/.composer/vendor/bin/',
  require => Exec['install composer']
}

By adding a global path reference in your project’s root manifest, you can reduce the amount of code needed.

Add global path declaration:

# set global path variable for project
Exec { path => [ "/usr/bin", "/usr/local/bin", "~/.composer/vendor/bin/" ] }

Updated module manifest:

exec { "composer install phpunit":
  command => 'composer global require "phpunit/phpunit=4.4.*"',
  environment => ["COMPOSER_HOME=/usr/local/bin"],
  require => Exec['install composer']
}

exec { "composer install phploc":
  command => 'composer global require "phploc/phploc=*"',
  environment => ["COMPOSER_HOME=/usr/local/bin"],
  require => Exec['install composer']
}
.
.
.

I hope this helps others trying to streamline their Puppet workflows. Please check out Puppet CookBook for tons of “task oriented solutions in Puppet”. And the recipe I used as a reference for this tutorial can be found here: http://www.puppetcookbook.com/posts/set-global-exec-path.html

Leave a Reply