【问题标题】:How to append file in puppet如何在人偶中附加文件
【发布时间】:2016-09-01 07:36:20
【问题描述】:

我有 nginx.conf 的傀儡代码。 该文件由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 文件?

【问题讨论】:

    标签: nginx puppet puppetlabs-apache


    【解决方案1】:

    我建议使用来自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 设置为 template,并根据您选择的环境/主机填充您选择的 variables

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

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

    【讨论】:

    • 感谢您的建议,但我对此有限制。如果我使用这个模块,那么我需要替换我现有的用于定义 nginx.conf 文件的模块。
    • 是的,这个问题一直存在。但是,与对代码的任何重构一样,您需要考虑它将在多大程度上改善您的生活/项目与实现新代码所需的时间。因此,如果这只是几个小时的时间来实现更改而不是将来简单而干净的配置更改,那么它可能是值得的。或者它可能只是让设置过于复杂,这是你的电话。如果您使用现有模块发布新问题,也许我可以帮助您更改它。
    【解决方案2】:

    我确实使用了 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"]], }

    感谢 MichalT 的支持...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-07
      • 2015-11-14
      • 1970-01-01
      相关资源
      最近更新 更多