Add file with puppet and remove it after a run

6,566

Unfortunately this is not how Puppet is designed to work - Puppet will enforce a known system state, rather than being an interactively running script.

One way of doing what you want is to use exec resources to add/remove the file, and using a locally-deployed 'source'.

Example (not complete, may not work):

class opensips::package {
  file { '/path/to/tmp/file.ext':
    ensure  => 'present',
    mode    => '0755',
    source  => 'puppet:///modules/opensips/myfile',
  }

  exec { 'deploy_opensips-enable-apt-policy':
      command => '/bin/cp /path/to/tmp/file.ext /path/to/real/file.ext',
      creates => '/path/to/real/file.ext',
      before  => Package[$opensips::opensips_debs],
      require => File['opensips-enable-apt-policy'],
  }

  package { $opensips::opensips_debs:
    ensure => $opensips::package_ensure,
    notify => Exec['remove_opensips-enable-apt-policy'],
  }

  exec { 'remove_opensips-enable-apt-policy':
      command     => '/bin/rm /path/to/real/file.ext',
      refreshonly => true,
  }
}
Share:
6,566

Related videos on Youtube

Frederik
Author by

Frederik

Experienced in Cisco routing/switching, Mikrotik routers, Windows Server 2008-2016, Debian/CentOS Linux, puppet and a whole lot more Programming experience includes PHP, C#.NET and I've been touching most other languages more or less

Updated on September 18, 2022

Comments

  • Frederik
    Frederik almost 2 years

    I want to add a file with puppet that should only exist while puppet runs.

    The file should be there to prevent apt from starting any services (which fails) during my puppet run.

    This is the class I have so far that doesn't work since there is a duplicate declaration of the same file:

    # Opensips package install
    class opensips::package {
      case $::osfamily {
        'Debian' : {
          file { 'opensips-enable-apt-policy':
            ensure  => 'present',
            path    => '/usr/sbin/policy-rc.d',
            mode    => '0755',
            content => "#!/bin/sh
    echo \"All runlevel operations denied by puppet module ${module_name}\" >&2
    exit 101
    ",
          } ->
          package { $opensips::opensips_debs: ensure => $opensips::package_ensure, } ->
          file { 'opensips-disable-apt-policy':
            ensure => 'absent',
            path   => '/usr/sbin/policy-rc.d',
          }
    
        }
        default  : {
          fail("Unsupported OS: ${::osfamily}")
        }
      }
    }
    

    This should give an idea of the desired end-state, but to sum it up:

    • Create the file /usr/sbin/policy-rc.d with the content above
    • Run the package install
    • Remove the file

    How would I do this the best/easiest way?

    The agent is Debian Jessie and runs puppet version 3.8.1

    I have tried to put the alias metaparameter on the file declaration success.

  • Frederik
    Frederik almost 9 years
    Thanks for your answer. I decided to drop it altogether since it was only a problem during developing the module and shouldn't be a problem on a fresh server.