天天看點

Puppet Class(類)特性(十一)

puppet類:

        為了通用目标或目的的組織在一起的一個或者多個資源;是以它是命名的代碼塊,在某個位置建立之後可在puppet全局使用.類似于其他程式設計語言中的類的功能,puppet的類可以繼承,也可以包含子類。

        類的定義使用class name,子產品下的init.pp檔案定義和子產品同名的類且唯一,可以為空或嵌套其他類.

puppet class(類)有兩種:

1、有參數的類

2、無參數的類

puppet class(類)的特性:

1、class 類隻有調用才會執行,調用稱作:聲明一個類

     使用include class_name

2、類的繼承:

   class B_name inherits A_name   

3、子類的命名方式:

   父類::子類

示例:

修改node.pp檔案,增加sh-web1主機比對.

注釋:sh-web1聲明nginx類.

1

2

3

4

5

6

7

8

9

10

11

12

13

<code>node </code><code>/sh-</code><code>(proxy|web)\d+/ {</code>

<code>  </code><code>case</code> <code>$::</code><code>hostname</code> <code>{</code>

<code>    </code><code>"sh-proxy2"</code><code>: {</code>

<code>         </code><code>include apache</code>

<code>         </code><code>user {</code><code>"test1"</code><code>:</code>

<code>            </code><code>ensure =&gt; present,</code>

<code>            </code><code>}</code>

<code>      </code><code>}</code>

<code>     </code><code>"sh-web1"</code><code>: {</code>

<code>            </code><code>include nginx</code>

<code>         </code><code>} </code>

<code>    </code><code>}</code>

<code>}</code>

puppet代碼nginx class.

<code>modules</code><code>/nginx/manifests/init</code><code>.pp檔案(無參數的類定義).</code>

<code>class nginx {</code>

<code>  </code><code>package {</code><code>"nginx"</code><code>:</code>

<code>    </code><code>ensure=&gt; present,</code>

<code>  </code><code>}</code>

puppet代碼apche class.

<code>modules</code><code>/apache/manifests/init</code><code>.pp檔案(有參數的類定義).</code>

<code>class apache ($sta = </code><code>"present"</code><code>) {</code>

<code>  </code><code>package {</code><code>"httpd"</code><code>:</code>

<code>    </code><code>ensure=&gt; $sta,</code>

sh-web1更新agent安裝nginx報錯如下:

<code>[root@sh-web1 puppet]</code><code># puppet agent -t</code>

<code>Notice: Ignoring --listen on onetime run</code>

<code>Info: Retrieving pluginfacts</code>

<code>Info: Retrieving plugin</code>

<code>Info: Caching catalog </code><code>for</code> <code>sh-web1.localdomain</code>

<code>Info: Applying configuration version </code><code>'1505454999'</code>

<code>Error: Execution of </code><code>'/usr/bin/yum -d 0 -e 0 -y list nginx'</code> <code>returned 1: Error: No matching Packages to list</code>

<code>Error: </code><code>/Stage</code><code>[main]</code><code>/Nginx/Package</code><code>[nginx]</code><code>/ensure</code><code>: change from absent to present failed: Execution of </code><code>'/usr/bin/yum -d 0 -e 0 -y list nginx'</code> <code>returned 1: Error: No matching Packages to list</code>

<code>Notice: Finished catalog run </code><code>in</code> <code>6.93 seconds</code>

原因:沒有release源,更新安裝eple-release源即可.

<code># yum install epel-release -y</code>

更新安裝nginx.

<code>Notice: </code><code>/Stage</code><code>[main]</code><code>/Nginx/Package</code><code>[nginx]</code><code>/ensure</code><code>: created</code>

<code>Notice: Finished catalog run </code><code>in</code> <code>34.99 seconds</code>

Class類的繼承(inherits)

注意:子類的命名不支援特殊符号'.',支援下劃線'_'

建立:modules\nginx\manifests\nginx.conf.pp

<code>class nginx::nginxconf inherits nginx {</code>

<code>    </code><code>file</code> <code>{</code><code>"/etc/nginx/nginx.conf"</code><code>:</code>

<code>        </code><code>ensure=&gt; </code><code>file</code><code>,</code>

<code>        </code><code>mode=&gt; 0644,</code>

<code>        </code><code>source</code><code>=&gt; </code><code>'puppet:///modules/nginx/nginx.conf'</code><code>,</code>

<a href="https://s5.51cto.com/wyfs02/M00/A5/82/wKioL1m_OlbyJVDWAAF2RcDROqE318.png" target="_blank"></a>

注意:puppet:///modules/nginx/nginx.conf  #不需要寫files目錄,指定子產品檔案後puppet預設回去files目錄下找對應的檔案.

files目錄存放puppet代碼調用的源檔案.

\modules\nginx\files\nginx.conf

<a href="https://s4.51cto.com/wyfs02/M02/06/D2/wKiom1m_OlaS87UsAAFHvUDo_oI244.png" target="_blank"></a>

修改主機聲明的class:

<code>        </code><code>include nginx::nginxconf</code>

<code> </code><code>} </code>

<a href="https://s3.51cto.com/wyfs02/M01/A5/82/wKioL1m_OnSxHwjhAAFl3ALSdhQ673.png" target="_blank"></a>

注釋:聲明nginx::nginxconf子類後,會繼承父類nginx,安裝nginx軟體包,然後替換檔案.

sh-web1主機更新puppet agent.

<code>[root@sh-web1 nginx]</code><code># puppet agent -t</code>

<code>Info: Applying configuration version </code><code>'1505457148'</code>

<code>Notice: </code><code>/Stage</code><code>[main]</code><code>/Nginx</code><code>::Nginxconf</code><code>/File</code><code>[</code><code>/etc/nginx/nginx</code><code>.conf]</code><code>/ensure</code><code>: defined content as </code><code>'{md5}142cb8f11006c0d78607c66cb82ae83d'</code>

<code>Notice: Finished catalog run </code><code>in</code> <code>8.69 seconds</code>

檢視更新内容:

<code>[root@sh-web1 nginx]</code><code># cat nginx.conf | grep 65535</code>

<code>    </code><code>worker_connections  65535;</code>

類的嵌套:

子產品下的init.pp檔案類必須和子產品名字相同且唯一,當然Class(類)中也支援嵌套聲明其他類或者定義空類.

示例:以apache為例.

<a href="https://s3.51cto.com/wyfs02/M00/06/D6/wKiom1m_ZVSxAaZEAAF-SrgT_O0652.png" target="_blank"></a>

test\modules\apache\manifests

<code>cat</code> <code>init.pp</code>

<code>class apache {</code>

<code>    </code><code>include </code><code>install</code>

<code>    </code><code>include service</code>

<code>cat</code> <code>install</code><code>.pp</code>

<code>    </code><code>class </code><code>install</code> <code>{</code>

<code>        </code><code>package {</code><code>"httpd"</code><code>:</code>

<code>        </code><code>ensure=&gt; present,</code>

<code>cat</code> <code>service.pp</code>

<code>    </code><code>class service {</code>

<code>        </code><code>service {</code><code>"httpd"</code><code>:</code>

<code>        </code><code>name=&gt; </code><code>"httpd"</code><code>,</code>

<code>        </code><code>ensure=&gt; running,</code>

<code>        </code><code>enable</code><code>=&gt; </code><code>true</code><code>,</code>

<code>        </code><code>provider =&gt; init,</code>

<code>        </code><code>require=&gt; Class[</code><code>"install"</code><code>],</code><code>#注意:此處要求啟動服務的前提時要完成install類的操作,如果不定義可能會報下面的錯.</code>

報錯:

<code>[root@sh-proxy2 puppet]</code><code># puppet agent -t</code>

<code>Info: Caching catalog </code><code>for</code> <code>sh-proxy2.localdomain</code>

<code>Info: Applying configuration version </code><code>'1505714213'</code>

<code>Error: </code><code>/Service</code><code>[httpd]: Could not evaluate: Could not </code><code>find</code> <code>init script </code><code>for</code> <code>'httpd'</code>

<code>Notice: </code><code>/Stage</code><code>[main]</code><code>/Admin/Exec</code><code>[selinux]</code><code>/returns</code><code>: executed successfully</code>

<code>Notice: </code><code>/Stage</code><code>[main]</code><code>/Install/Package</code><code>[httpd]</code><code>/ensure</code><code>: created</code>

<code>Notice: Finished catalog run </code><code>in</code> <code>2.75 seconds</code>

添加完require正确的更新:

<code>Info: Applying configuration version </code><code>'1505714421'</code>

<code>Notice: </code><code>/Stage</code><code>[main]</code><code>/Service/Service</code><code>[httpd]</code><code>/ensure</code><code>: ensure changed </code><code>'stopped'</code> <code>to </code><code>'running'</code>

<code>Info: </code><code>/Service</code><code>[httpd]: Unscheduling refresh on Service[httpd]</code>

<code>Notice: Finished catalog run </code><code>in</code> <code>3.27 seconds</code>

<code>[root@sh-proxy2 puppet]</code><code># /etc/init.d/httpd status</code>

<code>httpd (pid  88530) is running...</code>

本文轉自青衫解衣 51CTO部落格,原文連結:http://blog.51cto.com/215687833/1966274

繼續閱讀