puppet bolt templates ??

538 views
Skip to first unread message

Andy Hall

unread,
Sep 16, 2019, 12:21:05 PM9/16/19
to Puppet Users
hey there just starting using bolt and has a simple plan which applies a manifest but I'd know like to write a dynamic file from a template like epp in puppet. is this possible ? I do not see anything in the docs except just uploading a static file : https://puppet.com/docs/bolt/latest/running_bolt_commands.html#concept-6839

please do let me know if this is possible - it's great being able to write a manifest and templates would be the icing on the cake !

thanks.

KevinR

unread,
Sep 17, 2019, 7:19:29 AM9/17/19
to Puppet Users
Hi Andy,

you can do this with an Apply block in your plan:

apply_prep($nodes)

apply($nodes, '_run_as' => 'root'){
  file { '/root/my_file.txt':
    ensure  => file,
    content => epp('my_module/my_file.epp', { 'my_param' => 'my_value' })
  }
}

For the above example, you'll need to create a directory called 'my_module' in Bolt's modulepath, and then put the .epp file in a templates subdirectory.

-Kevin

Andy Hall

unread,
Sep 17, 2019, 8:40:11 AM9/17/19
to Puppet Users
That's great thanks !! So if I want to pass a param to the template from the command line would this work ??

1. The command:

bolt plan run profiles::puppet_upgrade servers=hostname.domain.com location=uk --user root

2. The plan:

plan profiles::puppet_upgrade(
  TargetSpec $servers,
) {

  apply($servers) {
    class { 'profiles::upgrade': location => $location }
  }
}

3. The manifest:

class profiles::upgrade ($location = undef) {

  file { '/root/my_file.txt':
      ensure  => file,
      content => epp('my_module/my_file.epp', { '$_location' => '$location' })
  }
}

4. The template:

location = <%= $_location %>

KevinR

unread,
Sep 17, 2019, 8:54:32 AM9/17/19
to Puppet Users
Yes that will work, there are a few erros in your code though:
- $location should not be quoted when calling epp()
- the name of the variable for epp should not begin with $

class profiles::upgrade ($location = undef) {

  file { '/root/my_file.txt':
      ensure  => file,
      content => epp('my_module/my_file.epp', { 'location' => $location })
  }
}

The template:

location = <%= $location %>

-Kevin

Andy Hall

unread,
Sep 17, 2019, 12:32:58 PM9/17/19
to Puppet Users
This works great thanks very much for your help. I just had to add the location param to the plan itself as follows:

plan profiles::puppet_upgrade(
  TargetSpec $servers,
  String $location,
) {

It works perfectly !! Could I ask if the params can be read from a file similar to hiera (or an inventory in ansible) rather than being passed on the command line ? If bolt can do that then I'd never write another playbook again :-)

KevinR

unread,
Sep 18, 2019, 10:13:19 AM9/18/19
to Puppet Users
Yes to all of the above :-)

1) Within Apply blocks, you can leverage Hiera data from the module. Simply put a hiera.yaml in the root of the module to define where Hiera data should be read from

2) While Hiera is nice, it might be even more powerful (depending on the use case) to leverage the new capabilities in the v2 Bolt inventory file to define facts and/or variables for groups and/or nodes: https://puppet.com/docs/bolt/latest/inventory_file_v2.html#inventory-facts-vars-features-v2

-Kevin

Andy Hall

unread,
Sep 18, 2019, 12:46:49 PM9/18/19
to Puppet Users
OK this is great. Really looking forward to using this more. Being able to leverage our existing puppet codebase and modules for a quick agentless solution means we don't have to migrate everything to ansible. Thanks again for all your help !!

Andy Hall

unread,
Sep 19, 2019, 6:00:18 AM9/19/19
to Puppet Users
OK so how do I get the plan to read from the inventory file ?? I am running this plan :

bolt plan run puppet6::puppet_upgrade -i inventory.yaml --nodes puppet6_nodes

And am getting this error :

puppet6::puppet_upgrade: expects a value for parameter 'location'

Here is my inventory file :

groups:
  - name: puppet6_nodes
    nodes:
      - host-name-01
    vars:
      location: ldn1

And here is the plan with params :

plan puppet6::puppet_upgrade(
  TargetSpec $nodes,
  String $location,
) {

  apply($nodes) {
    class { 'puppet6::upgrade': location => $location }
  }

}

Do the nodes and vars not get passed from the inventory to the plan ?? Thanks.

KevinR

unread,
Sep 19, 2019, 8:12:37 AM9/19/19
to Puppet Users
You do it as follows:

plan puppet6::puppet_upgrade(
  TargetSpec $nodes,
) {

  $targets = get_targets($nodes)
  $targets.each |$target| {
    apply($nodes) {
      class { 'puppet6::upgrade': location => $target.vars['location'] }
    }
  }

}

-Kevin

Andy Hall

unread,
Sep 19, 2019, 12:59:19 PM9/19/19
to Puppet Users
Fantastic all works now. If I have any further questions I'll create a new thread. Thanks.

Shirish Shukla

unread,
Feb 11, 2020, 10:24:24 AM2/11/20
to Puppet Users
What about if puppet agent not installed on target 
Is there any way we can use epp file

Alex Dreyer

unread,
Feb 11, 2020, 10:33:37 AM2/11/20
to puppet...@googlegroups.com
On Tue, Feb 11, 2020 at 7:24 AM Shirish Shukla <shiris...@gmail.com> wrote:
What about if puppet agent not installed on target 
Is there any way we can use epp file

There are a few options 
- You can target localhost for the apply to create the file locally and then use upload_file to copy it.
- You can write a task that accepts the contents and path as a parameter and writes the file to disk.
 

On Monday, 16 September 2019 21:51:05 UTC+5:30, Andy Hall wrote:
hey there just starting using bolt and has a simple plan which applies a manifest but I'd know like to write a dynamic file from a template like epp in puppet. is this possible ? I do not see anything in the docs except just uploading a static file : https://puppet.com/docs/bolt/latest/running_bolt_commands.html#concept-6839

please do let me know if this is possible - it's great being able to write a manifest and templates would be the icing on the cake !

thanks.

--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/d09a4037-518a-4ecf-bfd1-e729485346fd%40googlegroups.com.

Kevin Reeuwijk

unread,
Feb 11, 2020, 11:57:22 AM2/11/20
to puppet...@googlegroups.com
If you target the remote node (that doesn't have a Puppet agent) with an apply() block in a Bolt plan, the  prereq apply_prep($nodes) step will install the Puppet agent binaries on that node for you.
This allows you to still leverage all the functionality, without activating the agent.

Kind regards,
Kevin Reeuwijk
Principal Sales Engineer, NEMEA
mobile: +31 6 272 33 55 1
kev...@puppet.com | @KevinReeuwijk
Puppet. The shortest path to better software.


You received this message because you are subscribed to a topic in the Google Groups "Puppet Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/puppet-users/5HOpJEVhqOQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to puppet-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/CAMahoJtpASY9cwER1sDCWc1EcyYwKaDccJ8Yr200yJYESAbLBw%40mail.gmail.com.

Shirish Shukla

unread,
Feb 12, 2020, 1:25:51 AM2/12/20
to puppet...@googlegroups.com
There should be inbuilt function to achieve so, as its very basic functionality every bolt user expect.

Michael Smith

unread,
Feb 25, 2020, 12:37:22 PM2/25/20
to puppet...@googlegroups.com
The existing functions 'epp' and 'inline_epp' work. For example, the following plan returns the resolved template:

plan examples::epp() {
  $templ = @(END)
<%= $foo %>
END
  return inline_epp($templ, {'foo' => 'hello'})
}


Reply all
Reply to author
Forward
0 new messages