Passing parameters to puppet manifest via command line

I have been searching for an answer to this question with no luck, but is there a way to pass parameters into puppet manifests when running the 'apply' command, in a similar way to the way you pass parameters when running a UNIX script on the command line?

The suggestions I see mention either keeping variables at the top of the manifest for use later, or to store them in a hiera file. But neither really answer the question I am posing?

Any guidance on how to do this would be greatly appreciated?

Edit:

An example of what I have been doing is:

$doc_root = "/var/www/example"

exec { 'apt-get update':
 command => '/usr/bin/apt-get update'
}

package { 'apache2':
 ensure  => "installed",
 require => Exec['apt-get update']
}

file { $doc_root:
 ensure => "directory",
 owner => "www-data",
 group => "www-data",
 mode => 644
}

file { "$doc_root/index.html":
   ensure => "present",
   source => "puppet:///modules/main/index.html",
   require => File[$doc_root]
}

As you can see the variable is hardcoded at the top, whereas whilst I am trying to use the variable in the same way, I need to be able to pass the value in when running the apply command. Using lookup functions in conjunction with hiera.yaml files doesn't fulfil my requirements for the same reason. The only thing I can think may be a work around is to create a UNIX script that accepts parameters, saves those values in a yaml file, and then have the script execute the .pp file. But I'm hoping that puppet has a way to do this directly.

1 answer

  • answered 2020-10-23 15:35 Matt Schuchard

    The common procedure for passing variables into a classless manifest for use with the puppet apply subcommand would be to assign the value to a Facter fact from the CLI, and then resolve its value inside the manifest. You would begin with removing the hardcoded variable doc_root from the head of the manifest. Then, you would modify the variable into a fact like:

    file { $facts['doc_root']:
    ...
    file { "${facts['doc_root']}/index.html":
    ...
    require => File["${facts['doc_root']}"] <-- interpolation required due to Puppet DSL inability to resolve hash value as first class expression
    

    You would then pass the Facter value from the puppet apply subcommand like:

    FACTER_doc_root=/var/www/example puppet apply manifest.pp
    

    Note this also causes FACTER_doc_root to be temporarily set as an environment variable as a side effect.