天天看点

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

继续阅读