简体   繁体   中英

How to append file in puppet

I have puppet code for nginx.conf . The file is created by source => puppet://path to file which contain the required file contents. I don't want to disturb this file because it is for default setting.

I have to append this nginx.conf file which can be deployed on specific node where it is required. So I have written the separate module which is responsible for new changes. But this module is dependent on previous module which contain the nginx.conf file.

if ! defined(File['/etc/nginx/nginx.conf']) { file { '/etc/nginx/nginx.conf' : ensure => present, owner => root, group => root, mode => '0644', source => 'puppet:///modules/path/to/file/nginx_default.conf', require => Package[ 'nginx' ], notify => Service[ 'nginx'], } }

How could I append the nginx.conf file without disturbing above code?

I would recommend using Nginx modules from Puppet Forge the main benefit of the modules is that you don't have to reinvent the wheel, you can reuse the modules or adapt them to your needs.

This will still allow you to have a default nginx.conf (as a template) and by using classes you would be able to repurpose the nginx.conf template to your liking.

ie:

host_1.pp:

class { 'nginx':
  # Fix for "upstream sent too big header ..." errors
  fastcgi_buffers     => '8 8k',
  fastcgi_buffer_size => '8k',
  ssl_ciphers         => 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256',
  upstream => {
    fpmbackend => 'server unix:/var/run/php-fpm-www.sock',
  },
}

host_2.pp:

class { 'nginx':
  # Fix for "upstream sent too big header ..." errors
  fastcgi_buffers     => '8 8k',
  fastcgi_buffer_size => '36k',
  upstream => {
    fpmbackend => 'server unix:/var/run/php-fpm-host2.sock',
  },
}

However if you still want to use your modules you can setup the nginx.conf as a template and have it populated with variables of you choosing based on the environment/host of your choosing.

This will make the least changes to your code.

Although IMO in the long run using correct community modules will pay off better for you and our team.

I did use the exec to append the file, as there were many restrictions to try other ways like adding any new module.

I have created one file containing appending lines and then removed it.

include existing::module if ! defined (File["/new/path/for/temp/file/nginx_append.conf"]) file{"/new/path/for/temp/file/nginx_append.conf": ensure => present, mode => 755, owner => 'root', group => 'root', source => 'puppet:///modules/module-name/nginx_append.conf', } } exec {"nginx.conf": cwd => '/new/path/for/tenter code hereemp/file', command => "/bin/cat /new/path/for/temp/file/nginx_append.conf >> /etc/nginx/nginx.conf && rm /new/path/for/temp/file/nginx_append.conf", require => [ Service["nginx"]], include existing::module if ! defined (File["/new/path/for/temp/file/nginx_append.conf"]) file{"/new/path/for/temp/file/nginx_append.conf": ensure => present, mode => 755, owner => 'root', group => 'root', source => 'puppet:///modules/module-name/nginx_append.conf', } } exec {"nginx.conf": cwd => '/new/path/for/tenter code hereemp/file', command => "/bin/cat /new/path/for/temp/file/nginx_append.conf >> /etc/nginx/nginx.conf && rm /new/path/for/temp/file/nginx_append.conf", require => [ Service["nginx"]], }

Thanks MichalT for your support...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM