Puppet conditional check between stroller and ec2

I looked through the documentation for the puppet variables and didn't seem to understand how to apply this to the following situation

if vagrant (local machine) phpfpm::nginx::vhost { 'vhost_name': server_name => 'dev.demo.com', root => '/vagrant/public', } else if aws ec2 (remote machine) phpfpm::nginx::vhost { 'vhost_name': server_name => 'demo.com', root => '/home/ubuntu/demo.com/public', } 

thanks

+4
source share
3 answers

Try running facter on both your vagrant host and your EC2 instance, and look for the differences. I suspect that the "actual virtual" might be different between the two hosts, or that EC2 could return a bunch of ec2_ facts that would not be present on the roaming host.

You can then use this fact as a top-level variable, as shown below. I also switched to case case, as this made IMHO support a little easier, plus you can use the default block to check for errors.

 case $::virtual { 'whatever vagrant returns' : { <vagrant specific provisionin> } 'whatever the EC2 instance returns' : { <EC2 specific provisioning> } default : { fail("Unexpected virtual value of $::virtual") } } 
+7
source

NOTE. In the three years since this answer was facter , Vagrant introduced a hash variant of facter . See @thomas answer below. I believe this is the right way and makes my proposed kernel command line trick pretty obsolete. The rationale for using the fact has not changed, however, it has only intensified (for example, Vagrant currently supports the AWS provider).

ORIGINAL ANSWER: be careful - you assume that you are using virtual boxing for strollers and vice versa, but Vagrant is working on supporting other virtualization technologies (for example, kvm), and you can use VirtualBox without strollers one day (for example, for production).

Instead, I use the trick to pass the "vagrant = yes" parameter to the kernel when I create a base box, which is then accessible through / proc / cmdline. Then you can create a new fact based on this (for example, the file / etc / vagrant and check it on subsequent launches).

+5
source

Vagrant has a great Puppet fact-finding utility :

facter (hash) - a hash of data to set as available fax variables within the Puppets.

For example, here is a snippet from my Vagrantfile with Puppet setup:

 config.vm.provision "puppet", :options => ["--fileserverconfig=/vagrant/fileserver.conf"] do |puppet| puppet.manifests_path = "./" puppet.module_path = "~/projects/puppet/modules" puppet.manifest_file = "./vplan-host.pp" puppet.facter = { "vagrant_puppet_run" => "true" } end 

And then we will use this fact, for example, as follows:

  $unbound_conf = $::vagrant_puppet_run ? { 'true' => 'puppet:///modules/unbound_dns/etc/unbound/unbound.conf.vagrant', default => 'puppet:///modules/unbound_dns/etc/unbound/unbound.conf', } file { '/etc/unbound/unbound.conf': owner => root, group => root, notify => Service['unbound'], source => $unbound_conf, } 

Please note that this fact is only available during puppet provision .

+3
source

All Articles