2

I want to create a new file on a specific Puppet agent and store the output of a Linux command to the file. Is there an easy way to do this with the content attribute? The command I want to run is

file { '/home/akrall/out_string': 
    ensure => 'file',
    owner => 'andreas',
    group => 'andreas',
    mode  => '400'
    content => '<the output of the command would go here>'
}

The command itself that I want to run is openssl rand -hex 32, and I want to ensure that it's unique for every agent, so that's why I opted not to store it as a Puppet variable and send the same variable to multiple agents due to the fact that it would be the same string for every agent. Can anyone tell me how to do this? I tried content => '"$(openssl rand -hex 32)"' and that didn't work for me, and I'm not sure what else I should try.

Edit: If there's no easy way to do it with content, I can just use exec with the command to write the output to the file, but I was hoping there was something I could do with file so I could keep it all in the same resource.

AndreasKralj
  • 321
  • 1
  • 4
  • 15

1 Answers1

4

You could do that with the generate() function but as the command generates a different result each time you run it, you would end up with the file changing on every Puppet run.

What you can do instead is use the fqdn_rand_string() function from the stdlib module. Something like:

file { '/home/akrall/out_string': 
  ensure  => 'file',
  owner   => 'andreas',
  group   => 'andreas',
  mode    => '400',
  content => fqdn_rand_string(64, '0123456789abcdef'),
}

This seeds the random number generator with the hostname of the agent so it will always generate the same random string provided the hostname stays the same, but it will be different for each host.

bodgit
  • 4,661
  • 13
  • 26