puppet变量定义:

        由字母[a-z]、[A-Z]、[0-9]和下划线(_)组成,且大小写敏感,puppet中变量必须以"$"为前缀后接"="进行赋值.

变量可以保存字符串、数值、布尔型、数组、哈希和特殊的undef值.


文章写puppet变量的赋值、作用域和Facter变量.


puppet 变量赋值.

示例:

1
2
3
4
5
class apache ($sta =  "present" ) {
   package { "httpd" :
     ensure=> $sta,
   }
}

注意:$sta就是变量,声明变量的值为present.


puppet还支持变量之间赋值,变量之间赋值必须使用双引号,双引号会解析变量中的值后再次赋值变量,如果使用单引号则无法解析变量的值.

示例:

1
2
3
$status=1
$value= "$status"
notify { "status is $value" :}

注释(下):包含字符串的变量赋值时推荐使用{},以便更好的识别变量名.(不加也没问题输出也是正确的)

1
2
3
$status=3.1415926
$value= "statis is ${status}"
notify { "this is  $value" :}

puppet变量不支持重复赋值,大部分其他变成语言支持重复赋值.shell就支持通一个变量多次赋值.

错误示例:

1
2
3
4
5
6
$content= "this is file"
file  { "/tmp/text.txt" :
     ensure=>  file ,
     content=>  "$content" ,
}
$content= "my name is lisi."

agent端更新会报错:

1
Error: Could not retrieve catalog from remote server: Error 400 on SERVER: Cannot reassign variable content at  /etc/puppet/modules/admin/manifests/init .pp:13 on node sh-web1.localdomain

正确示例:

1
2
3
4
5
$content= "this is file"
file  { "/tmp/text.txt" :
     ensure=>  file ,
     content=>  "$content" ,
}

Agent端更新:

1
2
3
4
5
6
7
8
9
10
11
[root@sh-web1 ~] # puppet agent -t
Notice: Ignoring --listen on onetime run
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog  for  sh-web1.localdomain
Info: Applying configuration version  '1505842152'
Notice:  /Stage [main] /Admin/Exec [selinux] /returns : executed successfully
Notice:  /Stage [main] /Admin/File [ /tmp/text .txt] /ensure : defined content as  '{md5}ee93ffc81f3898fcba28a1d0e72c0029'
Notice: Finished catalog run  in  0.30 seconds
[root@sh-web1 ~] # cat /tmp/text.txt 
this is  file


变量的作用域:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$content变量分别定义于 top 域、node域和 local 域.
示例:
$content= "top"
node base {
     include admin
         $content= "node"
}
node  /sh- (proxy|web)\d+/  inherits base {
   case  $:: hostname  {
     /sh-proxy \d+/: {
          include apache
          user { "test1" :
             ensure => present,
             }
       }
      "sh-web1" : {
         include nginx::nginxconf
         $content= "sh-web1"
         notify { "this value is $content" :}
         
     }
}

Agent端更新:

1
2
3
4
5
6
7
8
9
10
[root@sh-web1 ~] # puppet agent -t
Notice: Ignoring --listen on onetime run
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog  for  sh-web1.localdomain
Info: Applying configuration version  '1505844151'
Notice:  /Stage [main] /Admin/Exec [selinux] /returns : executed successfully
Notice: this value is sh-web1
Notice:  /Stage [main] /Main/Node [sh-proxywebd] /Notify [this value is sh-web1] /message : defined  'message'  as  'this value is sh-web1'
Notice: Finished catalog run  in  0.25 seconds

注意:当top、node和local作用域同时定义相应变量,local优先级高于top和node作用域.


$content变量定义于top域和node域,local域并未定义但却可以调用top域和node域的变量.

示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$content= "top"
node base {
     include admin
     $content= "node"
}
node  /sh- (proxy|web)\d+/  inherits base {
   case  $:: hostname  {
     /sh-proxy \d+/: {
          include apache
          user { "test1" :
             ensure => present,
             }
       }
      "sh-web1" : {
         include nginx::nginxconf
             notify { "this value is $content" :}
             
     }
}

Agent端更新:

1
2
3
4
5
6
7
8
9
10
[root@sh-web1 ~] # puppet agent -t
Notice: Ignoring --listen on onetime run
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog  for  sh-web1.localdomain
Info: Applying configuration version  '1505844281'
Notice:  /Stage [main] /Admin/Exec [selinux] /returns : executed successfully
Notice: this value is node
Notice:  /Stage [main] /Main/Node [sh-proxywebd] /Notify [this value is node] /message : defined  'message'  as  'this value is node'
Notice: Finished catalog run  in  0.26 seconds

注意:local作用域若没有定义变量但可以引用top和node作用域变量的值,默认node优先级大于top,顺序从内向外.


$content变量分别在node域和local域定义,top域却调用不到变量的值.

示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
notify { "this value is $content" :}
node base {
     include admin
     $content= "node"
}
node  /sh- (proxy|web)\d+/  inherits base {
   case  $:: hostname  {
     /sh-proxy \d+/: {
          include apache
          user { "test1" :
             ensure => present,
             }
       }
      "sh-web1" : {
         include nginx::nginxconf
         $content= "sh-web1"
         
     }
}

Agent端更新:

1
2
3
4
5
6
7
8
9
10
[root@sh-web1 ~] # puppet agent -t
Notice: Ignoring --listen on onetime run
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog  for  sh-web1.localdomain
Info: Applying configuration version  '1505844582'
Notice: this value is 
Notice:  /Stage [main] /Main/Notify [this value is ] /message : defined  'message'  as  'this value is '
Notice:  /Stage [main] /Admin/Exec [selinux] /returns : executed successfully
Notice: Finished catalog run  in  0.27 seconds


注意:变量值为空,意味着top作用域并不能引用node和local作用域的值.


Facter变量

        Facter是一款扩展性强且功能强大的跨平台的系统性能分析收集工具,它可以收集Agent的信息,并将收集到的Agent信息作为变量传给master使用,在puppet中这些变量均为top作用域变量,可以在puppet中的任何位置调用他们.


直接运行facter命令:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
[root@puppet ~] # facter 
architecture => x86_64
augeasversion => 1.0.0
bios_release_date => 07 /02/2015
bios_vendor => Phoenix Technologies LTD
bios_version => 6.00
blockdevice_sda_model => VMware Virtual S
blockdevice_sda_size => 32212254720
blockdevice_sda_vendor => VMware,
blockdevice_sr0_model => VMware SATA CD01
blockdevice_sr0_size => 4467982336
blockdevice_sr0_vendor => NECVMWar
blockdevices => sda,sr0
boardmanufacturer => Intel Corporation
boardproductname => 440BX Desktop Reference Platform
boardserialnumber => None
domain => localdomain
facterversion => 2.4.6
filesystems => ext4,iso9660
fqdn => puppet.localdomain
gid => root
hardwareisa => x86_64
hardwaremodel => x86_64
hostname  => puppet
id  => root
interfaces => eth0,lo
ipaddress => 192.168.30.134
ipaddress_eth0 => 192.168.30.134
ipaddress_lo => 127.0.0.1
is_virtual =>  true
kernel => Linux
kernelmajversion => 2.6
kernelrelease => 2.6.32-431.el6.x86_64
kernelversion => 2.6.32
lsbdistcodename => Final
lsbdistdescription => CentOS release 6.5 (Final)
lsbdistid => CentOS
lsbdistrelease => 6.5
lsbmajdistrelease => 6
lsbminordistrelease => 5
lsbrelease => :base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-noarch
macaddress => 00:0C:29:53:DD:61
macaddress_eth0 => 00:0C:29:53:DD:61
manufacturer => VMware, Inc.
memoryfree => 1.56 GB
memoryfree_mb => 1600.11
memorysize => 1.82 GB
memorysize_mb => 1861.87
mtu_eth0 => 1500
mtu_lo => 16436
netmask => 255.255.255.0
netmask_eth0 => 255.255.255.0
netmask_lo => 255.0.0.0
network_eth0 => 192.168.30.0
network_lo => 127.0.0.0
operatingsystem => CentOS
operatingsystemmajrelease => 6
operatingsystemrelease => 6.5
os => { "name" => "CentOS" "lsb" =>{ "distid" => "CentOS" "distdescription" => "CentOS release 6.5 (Final)" "distcodename" => "Final" "majdistrelease" => "6" "minordistrelease" => "5" "distrelease" => "6.5" "release" => ":base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-noarch" },  "family" => "RedHat" "release" =>{ "full" => "6.5" "minor" => "5" "major" => "6" }}
osfamily => RedHat
partitions => { "sda1" =>{ "filesystem" => "ext4" "uuid" => "917f7d76-ead2-4a7a-9c36-e5da5dde658d" "mount" => "/boot" "size" => "1024000" },  "sda2" =>{ "filesystem" => "LVM2_member" "size" => "61888512" }}
path =>  /usr/lib64/qt-3 .3 /bin : /usr/local/sbin : /usr/local/bin : /sbin : /bin : /usr/sbin : /usr/bin : /root/bin
physicalprocessorcount => 1
processor0 => Intel(R) Core(TM) i5-4590 CPU @ 3.30GHz
processorcount => 1
processors => { "physicalcount" =>1,  "models" =>[ "Intel(R) Core(TM) i5-4590 CPU @ 3.30GHz" ],  "count" =>1}
productname => VMware Virtual Platform
ps  =>  ps  -ef
puppetversion => 3.8.7
rubyplatform => x86_64-linux
rubysitedir =>  /usr/lib/ruby/site_ruby/1 .8
rubyversion => 1.8.7
selinux =>  false
serialnumber => VMware-56 4d 78 db 2d 64 3c 00-a9 b8 0a 12 14 53  dd  61
sshdsakey => AAAAB3NzaC1kc3MAAACBAMWqA+bZeB11WxxdVpUG0vBTz9lsh58jNeRzGd4AdfS5czaDiVvWwpE77KePHJhXgJwMah /4KzZi + /nTx/lNlhhkaoyWEGy8F0RGEm4z9IKt0vA7ic6TXItge1HVH/QFuP657HCNBh6vGpALhQT50IiG6iAk +IVKkEodv9pV3nVlAAAAFQCYDwsefL0eVK /eBj1BOzTBaflLuwAAAIAhmRyHOAyMuNmbhFAISEoxuNqdaCvQK/JuxlE +KAWFFTq7U4wqzn /syPM4Rygauw3BA6JXzVrJhfRQiBg7TvObqlIOHRlqOu +9hy684OjpHRKmKONAPjaIjnDUp0F5lV2iOrmN8mqPbRLbfzP7 /p/Cb09r51BFRfpOdozpDPt74wAAAIAY/BeBxyscThPiE/0VTzANsryL4 +Bu //18qVaf5fTmeha3RSxqPBWkZR144hd8eLYZBcYx7jmvEtWuMAxxvK6K5dJfPCD5h0r2DFD36yhl1VdLuFS1ywSTxMDd3VvSrrdbACf +whzZ1ykC9FXOY3j2uU3tOa8CAxF8tERuSO /KkA ==
sshfp_dsa => SSHFP 2 1 917530c4885fa74246bdf7d34478fcf495ea1748
SSHFP 2 2 b65a9c7944c55dbc7fb7ff39f83cc11518730dc433b2abd0d52fc33040354f97
sshfp_rsa => SSHFP 1 1 0e3b51b07ee0e3d76f5888d28f33de9953e9e2b5
SSHFP 1 2 e4f8efe594408450015f3f2a9571f1cbd611d031b0bbc0568b1d6f9262cf98ae
sshrsakey => AAAAB3NzaC1yc2EAAAABIwAAAQEAzsUKTsQ1M3ccxgmm9yN /LqvOxnRRaDJfDGNQaRmEFi96pII2v215gak02fEkVMr116oGC9gAu7Dfwm08p3zdmDh7Lhx +V286scgPeacAD8Lb3jj5F5hXRzBKz6E8oasi34 /FQZ7fUa7k5gaQ0Jv1mkjW89WsH4F5emlD5pA75Gxjcb9nf4UV1OVl4jhZXj21xvWVoJIbNCcTTSn8m7ImJ7eSs6/fWCGh7tdJViwaMhwRG4hV3n4fcC7dXM0Ol8jMm8i5bLWtk050 +gJ4GeJKveSkN4RzFaowkiFp7m5W6Gc4hXfQGzk8epwpsdWdvJu0oBKV+SvBBBNsJ+kA7fHNrw==
swapfree => 3.00 GB
swapfree_mb => 3071.99
swapsize => 3.00 GB
swapsize_mb => 3071.99
system_uptime => { "seconds" =>60398,  "uptime" => "16:46 hours" "hours" =>16,  "days" =>0}
timezone => CST
type  => Other
uniqueid => a8c0861e
uptime => 16:46 hours
uptime_days => 0
uptime_hours => 16
uptime_seconds => 60398
uuid => DB784D56-642D-003C-A9B8-0A121453DD61
virtual => vmware

示例:

sh-web1主机引用facter变量ipaddress_eth0.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
node base {
include admin
}
node  /sh- (proxy|web)\d+/  inherits base {
   case  $:: hostname  {
     /sh-proxy \d+/: {
          include apache
          user { "test1" :
             ensure => present,
             }
       }
      "sh-web1" : {
         include nginx::nginxconf
         notify { "hostip is $ipaddress_eth0" :}
 
     }
}


sh-web1 puppet agent端更新信息:

1
2
3
4
5
6
7
8
9
10
[root@sh-web1 ~] # puppet agent -t
Notice: Ignoring --listen on onetime run
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog  for  sh-web1.localdomain
Info: Applying configuration version  '1505901622'
Notice:  /Stage [main] /Admin/Exec [selinux] /returns : executed successfully
Notice: hostip is 192.168.30.131
Notice:  /Stage [main] /Main/Node [sh-proxywebd] /Notify [hostip is 192.168.30.131] /message : defined  'message'  as  'hostip is 192.168.30.131'
Notice: Finished catalog run  in  0.27 seconds


扩展:

puppet 内置变量

puppet的内置变量由puppet.conf配置文件定义,并在puppet代码文件中可以直接使用的变量称为内置变量.内置变量分为Agent内置变量和Master内置变量.

Agent端的内置变量:

1
2
$environment:客户端引用的环境代码
$clientcert:客户端认证

Master内置变量:

1
2
3
$servername:Master的fqdn
$serverip:Master的ip。
$module_name:Master中模块的名字.