<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ### 當文件更新時運行命令 當某個特定的文件更新后 Puppet 就該采取一些行動,這是一個非常常見的模式。 例如,在 rsync 配置片段的例子中,一旦修改了某個片段文件,就會調用 exec 資源更新主配置文件 rsyncd.conf。 每次運行 Puppet,exec 資源都會被運行,除非指定了如下參數中的一個: * creates * onlyif * unless * refreshonly =&gt; true refreshonly 參數的意思是:僅當從其他資源(例如一個文件資源)獲得一個 notify 才執行 exec 資源。 #### 準備工作 安裝 nginx 包(實際上,我們只需要配置文件,但這是獲得它的最簡單方式): ``` # apt-get install nginx ``` #### 操作步驟 1. 創建一個 nginx 模塊,其目錄結構如下: ``` # mkdir /etc/puppet/modules/nginx # mkdir /etc/puppet/modules/nginx/files # mkdir /etc/puppet/modules/nginx/manifests ``` 2. 使用如下內容創建 /etc/puppet/modules/nginx/manifests/nginx.pp 文件: ``` class nginx { package { "nginx": ensure =&gt; installed } service { "nginx": enable =&gt; true, ensure =&gt; running, } exec { "reload nginx": command =&gt; "/usr/sbin/service nginx reload", require =&gt; Package["nginx"], refreshonly =&gt; true, } file { "/etc/nginx/nginx.conf": source =&gt; "puppet:///modules/nginx/nginx.conf", notify =&gt; Exec["reload nginx"], require =&gt; Package["nginx"], } } ``` 3. 復制系統中的 nginx.conf 文件到模塊的相應目錄: ``` cp /etc/nginx/nginx.conf /etc/puppet/modules/nginx/files ``` 4. 添加如下代碼到你的配置清單: ``` include nginx ``` 5. 對復制到 Puppet 中的 nginx.conf 文件做個小改動,以便進行后續的測試: ``` # echo \# &gt;&gt;/etc/puppet/modules/nginx/files/nginx.conf ``` 6. 運行 Puppet: ``` # puppet agent --test info: Retrieving plugin info: Caching catalog for cookbook.bitfieldconsulting.com info: Applying configuration version '1303745502' --- /etc/nginx/nginx.conf 2010-02-15 00:16:47.000000000 -0700 +++ /tmp/puppet-file20110425-31239-158xcst-0 2011-04-25 09:39:49.586322042 -0600 @@ -48,3 +48,4 @@ # proxy on; # } # } +# info: FileBucket adding /etc/nginx/nginx.conf as {md5}7bf139588b5e cd5956f986c9c1442d44 info: /Stage[main]/Nginx/File[/etc/nginx/nginx.conf]: Filebucketed /etc/nginx/nginx.conf to puppet with sum 7bf139588b5ecd5956f986c9c1442d44 notice: /Stage[main]/Nginx/File[/etc/nginx/nginx.conf]/content: content changed '{md5}7bf139588b5ecd5956f986c9c1442d44' to '{md5} d28d08925174c3f6917a78797c4cd3cc' info: /Stage[main]/Nginx/File[/etc/nginx/nginx.conf]: Scheduling refresh of Exec[reload nginx] notice: /Stage[main]/Nginx/Exec[reload nginx]: Triggered 'refresh' from 1 events notice: Finished catalog run in 1.69 seconds ``` #### 工作原理 對于大多數服務來說,你應該簡單地定義一個服務資源,它從配置文件獲得 notify。 這會使 Puppet 重啟服務,從而應用改變之后的更新。 然而,nginx 有時不能正確的重新啟動,尤其是當使用 Puppet 重新啟動時, 所以我為某個站點炮制了一個讓 Puppet 運行 /etc/init.d/nginx reload 的補救措施, 用它替代服務的重啟。下面闡述它是如何工作的。 將 exec 資源中的 refreshonly 參數設置成 true: ``` exec { "reload nginx": command => "/usr/sbin/service nginx reload", require => Package["nginx"], refreshonly => true, } ``` 所以,僅當它獲得通知才會運行。 如果配置文件發生改變,就提供所需的通知(notify): ``` file { "/etc/nginx/nginx.conf": source => "puppet:///modules/nginx/nginx.conf", notify => Exec["reload nginx"], } ``` 每當 Puppet 更新了這個配置文件,它就會運行 exec, 這將會調用如下命令重新加載修改后的配置文件: ``` /usr/sbin/service nginx reload ``` 如果服務支持 reload 命令參數,就會在不中斷服務的情況下為 daemon 發出一個重新讀取配置文件的進程信號。 實際上,對于本例,更好的方法是為 nginx 服務定義如下的 restart 命令: ``` service { "nginx": restart => "/etc/init.d/nginx reload", } ``` 但是我想要和你分享一些我寫的用于展示 notify -&gt; Exec 技術的實際代碼, 并且那時,我還不知道 restart 而且它也不存在。 不過作為一種通用的模式,當更新文件后需要采取某種行動時,你會發現它有用。 #### 更多用法 每當遇到資源更新就要采取某些行動的情況,你就可以使用這個類似的模式。可能的用途包括: * 觸發服務重新加載配置文件 * 運行語法檢查,然后再重新啟動服務 * 連接 config 片段 * 運行測試 * 鏈接 exec 資源 如果當一個文件(假設名為 somefile)更新后需要執行多個命令,那么在每個命令的 exec 資源聲明中都使用 subscribe =&gt; File[somfile], (即一旦檢測到 somefile 文件的變化就重新執行 exec 資源)會更簡單, 而不是在 file 資源中使用 notify 執行命令。 效果是一樣的。 > ![注記](https://box.kancloud.cn/2016-05-12_5733eec619643.png) 譯者注 > 你也可以在 service 資源定義中使用 subscribe, 例如在一個 Redhat 風格的系統中,你可以使用如下的 service 聲明: > ``` > service { "nginx": > enable =&gt; true, > ensure =&gt; running, > restart =&gt; "/etc/init.d/nginx reload", > subscribe =&gt; [ File["/etc/nginx/nginx.conf"], > File["/etc/sysconfig/nginx"] ] > } > > ``` > 在上例中,使用數組為 subscribe 指定了兩個要檢測的文件, 也就是說,一旦檢測到兩個文件中的任何一個發生變化就重啟服務。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看