繁体   English   中英

如何在人偶中附加文件

[英]How to append file in puppet

我有nginx.conf的p代码。 该文件由source => puppet://path to file包含所需文件内容的source => puppet://path to file创建。 我不想打扰此文件,因为它是默认设置。

我必须附加此nginx.conf文件,该文件可以部署在需要它的特定节点上。 因此,我编写了负责新更改的单独模块。 但是,此模块取决于包含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'], } }

如何在不打扰以上代码的情况下附加nginx.conf文件?

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

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

即:

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

但是,如果仍要使用模块,则可以将nginx.conf设置为模板,并根据您选择的环境/主机用您选择的变量填充它。

这将对您的代码进行最少的更改。

尽管从长远来看,IMO使用正确的社区模块将为您和我们的团队带来更好的回报。

我确实使用了exec来追加文件,因为尝试其他方式(例如添加任何新模块)有很多限制。

我创建了一个包含附加行的文件,然后将其删除。

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

感谢MichalT的支持...

暂无
暂无

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

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