标签能更好的让puppet识别一段代码。同时,客户端也可以执行指定的tag,而不需要全部都执行。

 

tag元参数,用于标签资源;

tag函数,用于标签容器;

tagged函数,用于判断一个容器,是否有某个标签,即用于检查tag函数。

 

puppet的自动分配标签分两种:资源和容器。默认是以自己的类型,作为标签名。

 

资源自动分配标签的特点:

       ①自己的资源类型;

       title

       ③所在容器的类型;

       ④所在容器的tital和每个命名空间;

       ⑤继承的tag

 

容器自动分配标签的特点:

       ①自己的类型;

       title和每个命名空间;

       ③所在容器的类型;

       ④所在容器的tital和每个命名空间;

       ⑤继承的tag

 

注:凡title的内容有“/”,该title不会被添加为标签名。

 

#title/

file {"/tmp/filebucket":

       ensure   => file,

       content   => "hello wrold",

}

 

#title不含/

file {"filebucket":

       ensure   => file,

       content   => "hello wrold",

       path   => "/tmp/filebucke ",

}

 

 

 

①.tag元参数:

 

       一般用于,各种资源,可以使用数组。

格式:

 

       tag => "标签名",

       tag => ["标签名1",…, "标签名n"],

 

注:可放入到任何一类资源。

 

例:

#

cron { "run-puppet":

          command => "/usr/sbin/puppet agent --server=master.perofu.com   --test >/dev/null    2>&1",

          minute => inline_template("<%= (hostname+name).hash.abs % 60   %>"),

          tag => "cron_test",

}

 

#

[root@master manifests]# crontab -l

# HEADER: This file was autogenerated at   Fri Mar 21 15:24:59 +0800 2014 by puppet.

# HEADER: While it can still be managed   manually, it is definitely not recommended.

# HEADER: Note particularly that the   comments starting with 'Puppet Name' should

# HEADER: not be deleted, as doing so   could cause duplicate cron jobs.

 

[root@master ~]# puppet agent   --server=master.perofu.com --test --tags cron_test

info: Caching catalog for master.perofu.com

info: Applying configuration version   '1395386333'

notice:   /Stage[main]//Cron[run-puppet]/ensure: created

notice: Finished catalog run in 0.04   seconds

[root@master manifests]# crontab -l

# HEADER: This file was autogenerated at   Fri Mar 21 15:43:55 +0800 2014 by puppet.

# HEADER: While it can still be managed   manually, it is definitely not recommended.

# HEADER: Note particularly that the   comments starting with 'Puppet Name' should

# HEADER: not be deleted, as doing so   could cause duplicate cron jobs.

# Puppet Name:   run-puppet

32 * * * *   /usr/sbin/puppet agent --server=master.perofu.com --test >/dev/null  2>&1

 

 

 

②.tag函数:

       tag函数可以为容器添加标签,,默认就是标签名,个人理解:容器差不多是类,因为类用的多。

 

       如果用户想添加另外的tag,需要class第一行,进行定义。

 

格式:

 

       class   类名{

              tag("标签名")

              …….

}

 

 

例:

#容器的标签名现在有两个:sshsecurity

class ssh{

       tag("security")

       service{"sshd":

              ensure => running,

              enable => true,

       }

}


 

③.tagged函数:

 

    判断一个容器的标签,即tag函数,是否被定义过。

 

vi site.pp

if tagged("sss"){

    $a="The tag is sss"

} else {

    $a="The tag is NONE"

}


file {"/tmp/temp.txt"

    content => "$a",

}

#

cat /tmp/temp.txt

The tag is NONE

 

 

④.如何查看生成的tag标签:

    可查看/var/lib/puppet/reports/节点/yaml