简体   繁体   English

如何在人偶中附加文件

[英]How to append file in puppet

I have puppet code for nginx.conf . 我有nginx.conf的p代码。 The file is created by source => puppet://path to file which contain the required file contents. 该文件由source => puppet://path to file包含所需文件内容的source => puppet://path to file创建。 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. 我必须附加此nginx.conf文件,该文件可以部署在需要它的特定节点上。 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. 但是,此模块取决于包含nginx.conf文件的先前模块。

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? 如何在不打扰以上代码的情况下附加nginx.conf文件?

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. 我建议使用Puppet Forge的Nginx模块,这些模块的主要优点是您不必重新发明轮子,可以重复使用这些模块或使它们适应您的需求。

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. 这仍然允许您拥有默认的nginx.conf(作为模板),并且通过使用类,您将能够根据自己的喜好重新调整nginx.conf模板的用途。

ie: 即:

host_1.pp: 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: 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. 但是,如果仍要使用模块,则可以将nginx.conf设置为模板,并根据您选择的环境/主机用您选择的变量填充它。

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. 尽管从长远来看,IMO使用正确的社区模块将为您和我们的团队带来更好的回报。

I did use the exec to append the file, as there were many restrictions to try other ways like adding any new module. 我确实使用了exec来追加文件,因为尝试其他方式(例如添加任何新模块)有很多限制。

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"]], } 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... 感谢MichalT的支持...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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