How to copy and install 3 different files from puppet master to 3 different locations

177 views
Skip to first unread message

mike....@gmail.com

unread,
Apr 20, 2017, 4:51:50 PM4/20/17
to Puppet Users
I created a module to copy one file from the master and install to agents and it works fine with code below: but now I am trying to copy 3 different files in different directory and install each one on different environment. For example: file A need to be installed on all agents in A environment File B need to be installed on all agents in B environment File C need to be installed on all agents in C environment

  1 class profile::ma {
  2
  3 file { '/tmp/filename.sh':
  4     ensure  => 'present',
  5     replace => 'no',

  6     source  => 'puppet:///module/files/filename.sh',
  7     mode   => '0755',
  8     notify  => Exec['install'],
  9
 10 }
 11
 12    exec { 'install':
 13      command     => '/tmp/filename.sh -i',
 14      onlyif      => '/usr/bin/test ! -e /etc/filetocheck',
  15 }
16}

Joshua Schaeffer

unread,
Apr 21, 2017, 12:38:00 PM4/21/17
to Puppet Users
I take it you want to have one manifest that is agnostic to all environments and copy the right files? Your files have to be put into each environment respectively. When the manifest runs it will pull the file from that environment on the server.

  • A goes in /etc/puppetlabs/code/environments/A/modules/your_module/files/filename.sh
  • B goes in /etc/puppetlabs/code/environments/B/modules/your_module/files/filename.sh
  • C goes in /etc/puppetlabs/code/environments/C/modules/your_module/files/filename.sh
Your code remains the same, just make sure you call the right environment on your node. If you need to install the file into different locations based on the environment then you probably want to use either an if or case statement or use Hiera:


case $environment {
 
'A': {$install_path = '/env/A/install/path'}
 
'B': {$install_path = '/env/B/install/path''}
  '
C': {$install_path = '/env/C/install/path'}
  default: {}
}


file { $install_path:
  ensure => '
present',
  replace => '
no',
  source => '
puppet:///module/files/filename.sh',
  mode
=> '0755',

  notify
=> Exec['install'],
}


And in Hiera:

---
your_module
::A::install_path: '/env/A/install/path'
your_module
::B::install_path: '/env/B/install/path'
your_module
::C::install_path: '/env/C/install/path'


file
{ 'environment-file'
  path
=> lookup("your_module::${environment}::install_path", String)
 
...
}

Hope that helps.

Thanks,
Joshua Schaeffer

James Perry

unread,
Apr 21, 2017, 12:39:33 PM4/21/17
to Puppet Users
I'm no expert by far, but since I have been doing a lot of reading lately for an issue I am trying to resolve, would it make sense to do a define block for this? Looking at https://docs.puppet.com/puppet/4.9/lang_defined_types.html it seems this may work for you with some tweaking / testing. I'm not sure I have all of the syntax right.

something of the order below might work. 

define profile::ma ($envfile = "", $envsource = "") {
  if $envfile == "" {
    $envtarget = '/tmp/filename.sh'
  } else {
    $envtarget = $envfile
  }
  if $envsource == "" {
    $env_path = "files/filenames.sh"
  }
  else {
    $env_path = $envsource
  }

  file { $envtarget:
    ensure  => 'present',
    replace => 'no',
    source  => "puppet:///module/${env_path}",
    mode => '0755',
    notify => Exec['install']
  }
  
  exec {'install': 
    command => "${envtarget} -i",
    onlyif => '/usr/bin/test ! -e /etc/filetocheck',
  }
}

** Note there isn't any handling of "filetocheck" in this snippet, but you can add it. 

In your code block for the file you would have something like:

class env_a ($env_file = "/tmp/filename.sh", 
  $env_source = "files/env_a_file.sh"
  ){
   profile::ma{ 'profilea':
      envfile => $env_file,
      envsource => $env_source,
      } 
}

class env_b ($env_file = "/tmp/filename.sh", 
  $env_source = "files/env_b_file.sh"
  ){
   profile::ma{ 'profilea':
      envfile => $env_file,
      envsource => $env_source,
      } 
}

I put in smart parameters for the settings since I use foreman and find I will sometimes need to override a setting value from time to time when something breaks and I need a fix before being able to get the new code through change control. 

Now I haven't tested it but it does follow from the linked example above it appears that it should work. There probably also a way to dynamically setup the settings based on the defined environment, but that is beyond me right now. 
Reply all
Reply to author
Forward
0 new messages