设为首页 收藏本站

运维网

查看: 94|回复: 0

[技术分享] Puppet变量的赋值、作用域和Facter变量(十三)

[复制链接]

累计签到:1 天
连续签到:1 天
发表于 2017-9-21 09:37:10 | 显示全部楼层 |阅读模式
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
[[email protected] ~]# 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
[[email protected] ~]# 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
[[email protected] ~]# 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
[[email protected] ~]# 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
[[email protected] ~]# 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
[[email protected] ~]# 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
[[email protected] ~]# 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中模块的名字.






运维网声明 1、欢迎大家加入本站运维交流群:群①:263444886群②:197202523群③:485755530群④:201730672群⑤:202807635运维网交流群⑥:281548029
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、其他单位或个人使用、转载或引用本文时必须注明原文的出处
4、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意
5、运维网 - 服务您的运维操作管理专家!
6、联系人Email:[email protected] 网址:www.iyunv.com

点击关注更多内容
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册  

本版积分规则  允许回帖邮件提醒楼主

关注运维网官方微信X

关注运维网官方微信

扫描二维码关注运维网官方微信,最新一手资源尽在官方微信!快快关注我们吧...

扫描微信二维码查看详情

客服 E-mail:[email protected]

本站由安畅云和青云提供云计算服务

运维网--中国最专业的运维工程师交流社区

京ICP备14039699号-1 Copyright © 2012-2017

使用手机软件扫描微信二维码

关注我们可获取更多热点资讯

Good good study day day up !


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


合作伙伴: 青云cloud 51idc

快速回复 返回顶部 返回列表