Showing posts with label puppet. Show all posts
Showing posts with label puppet. Show all posts

Replace a file in Puppet only if the file exists


Replace file in Puppet only if the file exists. So i have this question from my colleague. It sounds simple right? but it is not.

Some says that use exec with onlyif but it produces warning/errors logs if the file doesn't exist. In which functionality, it will suffice your needs, but you will have this warning logs.

This is the workaround that i've done:


file { '/tmp/file01.txt':    source => "puppet:///modules/common/custommodule/file01.txt",    owner => 'root',    group => 'root',    mode => '0640',  }

  exec {"copy_file01":    command => "/usr/bin/diff /tmp/file01.txt /var/tmp/file01.txt; if [ $? -ne 0 ]; then /bin/cp /tmp/file01.txt /var/tmp/file01.txt; fi",     require=> File['/tmp/file01.txt'],    onlyif => "/usr/bin/test -e /var/tmp/file01.txt",
  }


two things that this will accomplish:

1. It will not replace or copy the file if the file doesn't exists. Where as if I use "file" type in puppet, it will create the file regardless if it exists or not.
2. If the file changes in puppet it will replace the existing file in the client. Test will be done by the diff command.

The only disadvantage of this, is that it will create a file under a directory. 

By penoi on Thursday, June 23, 2016 | | A comment?

Devops Singapore Meetup Puppet in production session



Devops Singapore Meetup Puppet in production session

By penoi on Thursday, June 14, 2012 | | A comment?

Puppet starting the service before package install

Here's an example of a simple manifest file that will install a package (ex. apache) and will ensure that that the service was started after install


package {'httpd':
ensure => installed,
source => '/var/tmp/httpd-2.2.3-53.el5.centos.i386.rpm',
}

service {'httpd':
ensure => running,
enable => true,
hasstatus => true,
hasrestart => true,
}



Seems legit. Let's apply the manifest


[root@learn tmp]# puppet apply httpd.install.pp
err: /Stage[main]//Service[httpd]/ensure: change from stopped to running failed: Could not start Service[httpd]: Execution of '/sbin/service httpd start' returned 1: at /var/tmp/httpd.install.pp:11
notice: /Stage[main]//Package[httpd]/ensure: created
notice: Finished catalog run in 31.04 seconds



hmmm.. it seems that puppet was starting the service before the install. Let's modify the file


package { 'httpd':
ensure => installed,
source => '/var/tmp/httpd-2.2.3-53.el5.centos.i386.rpm',
}

service { 'httpd':
require => Package['httpd'],
ensure => running,
enable => true,
hasstatus => true,
hasrestart => true,
}



Now let's apply the new manifest file


[root@learn tmp]# puppet apply httpd.install.pp
notice: /Stage[main]//Package[httpd]/ensure: created
notice: /Stage[main]//Service[httpd]/ensure: ensure changed 'stopped' to 'running'
notice: Finished catalog run in 34.10 seconds

[root@learn tmp]# chkconfig --list |grep httpd
httpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
pe-httpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off

[root@learn tmp]# rpm -qa |grep httpd
pe-httpd-2.2.3-17.pe.el5
pe-httpd-passenger-2.2.11-11.pe.el5
httpd-2.2.3-63.el5.centos.1
[root@learn tmp]# service httpd status
httpd (pid 15355) is running...



Disclaimer: im still a puppetlabs newbie. :)

By penoi on Saturday, June 2, 2012 | | A comment?