1

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?

2 Answers2

0

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.

i.e:

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.

MMT
  • 1,931
  • 3
  • 19
  • 35
  • Thanks for your suggestion, but I have restriction over it. If I use this module then I would need to replace my existing modules which is used to define the nginx.conf file. – Khalid Waseem May 10 '16 at 08:46
  • Yes, there is always this issue. However as with any refactoring of a code you need to think of how much it will improve your life/project vs the time it will take to implement new code. So if this is just a matter of few hours to implement changes vs easy and clean configuration changes in the future then it may be worth it. Or it may be just over complicating the setup it's your call. If you post a new questions with the your existing module maybe I'll able to help you change it. – MMT May 10 '16 at 09:27
0

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

Thanks MichalT for your support...