Ask Your Question
0

Set File content via Hiera

asked 2016-09-20 01:44:41 -0500

asktbt gravatar image

Hi,

I am looking for the syntax to set the content for a File resource from hiera. Specifically I want to set the template being used:

# Puppet
class test (
  $file_content = undef,
){

  file{'/tmp/testfile':
    ensure  => file,
    content => $file_content,
  }

}

# Hiera
---
test::file_content: template('modules/test/test.erb')

Since Hiera is returning a string, the template function is not being evaluated.

I am pretty sure I saw this being done somewhere, but I cannot figure out the correct syntax anymore.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2016-09-20 09:16:19 -0500

Jeremiah Powell gravatar image

Hiera doesn't call random functions like template(). Per the documentation, hiera only supports a few methods:

  • hiera() - do another hiera lookup for x and use that as this item's value
  • scope() - do a variable interpolation (redundant as the syntax for variables does this already)
  • alias() - make this key an alias to another key (sort of a link to the value of the other variable)
  • literal() - escape the % symbol so%{} can appear as a value

Why not just set the name of the template to use in hiera and call the template() in your code?

Puppet

class test (
  $file_name = undef,
){

  file{'/tmp/testfile':
    ensure  => file,
    content => template($file_name),
  }

}

Hiera

--- 
test::file_name: 'modules/test/test.erb'
edit flag offensive delete link more
0

answered 2016-09-21 05:33:23 -0500

asktbt gravatar image

Thanks for the suggestion. IRL the use-case is a bit more complex than outlined here. The file resource is used within create_resources() to create files. The majority of files is created without templates, but in one specific case a template is used to iterate over a hash. Therefore I have to pass the template function to the the content-parameter in the file resource.
Otherwise your suggestion would have certainly worked.

So I try to reduce this code

# Puppet
class test() {

$files = {
  'file1' => {
    ensure => file,
    content => 'Some string',
  },
  'file2' => {
    ensure => file,
    content => template('test/template.erb'),
  },
}

create_resources(file, $files)

}

into (not working):

# Puppet
class test(
  $files,
) {
  create_resources(file,$files)
}

# Hiera
---
test::files:
  file1:
    ensure: file
    content: Some string
  file2:
    ensure: file
    content: template('test/template.erb')

it feels to me like this is something that should be doable, but I can neither find any technical support for that claim, nor reason why. Quite the opposite. Everything I know and read about Hiera and Puppet tells me it should not work (and it does not either).

So unless somebody knows a workaround this, I'll change my question: is there a more elegant way to do this?

edit flag offensive delete link more

Comments

I'm having the same issue with create_resources() and templates. What's strange is that template functionality doesn't work but "source" works just fine:
'/usr/local/bin/list_cluster_members.sh':
source: 'puppet:///modules/profiles/usr/local/bin/list_cluster

bschonecker gravatar imagebschonecker ( 2016-10-21 05:51:01 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2016-09-20 01:44:41 -0500

Seen: 624 times

Last updated: Sep 21 '16