Debian – Add file with puppet and remove it after a run

debiandebian-jessiepuppetpuppet-agent

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.

Best Answer

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,
  }
}