哈希类型

        puppet语言支持hash类型.它与数组类似,都是带索引的对象集合,与数组的区别在于作为索引步进限于数字,可以是任何对象。在puppet中哈希类型的"键"必须是字符串类型,但其"值"可以是任意的类型.

格式如下:

{key1 => "value",key2 => "vale",...}


示例如下:

1
2
3
[root@puppet ~] # cat 4.pp 
$myname = { name1 => lisi, name2 => zhangsan }
notice ($myname[name1])

 agent端应用:

1
2
3
4
[root@puppet ~] # puppet apply 4.pp 
Notice: Scope(Class[main]): lisi
Notice: Compiled catalog  for  puppet.localdomain  in  environment production  in  0.05 seconds
Notice: Finished catalog run  in  0.01 seconds


布尔类型

puppet支持布尔类型,布尔型经常用于函数的返回状态,布尔型只有两个值true(真)或者false(假).在通过布尔型赋值变量时,需要注意不要加双引号("")或者单引号('')。


正确的赋值方法:

1
2
$switch =  true
$switch =  false

错误的赋值方法:

1
2
$switch =  "true"
$switch =  'true'

安装puppetlabs-stdlib模块库.

1
2
3
4
5
6
[root@puppet ~] # puppet module install puppetlabs-stdlib
Notice: Preparing to  install  into  /etc/puppet/modules  ...
Notice: Downloading from https: //forgeapi .puppetlabs.com ...
Notice: Installing --  do  not interrupt ...
/etc/puppet/modules
└── puppetlabs-stdlib (v4.20.0)


master端的puppet代码文件.

1
2
3
node base {
include admin
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
node  /sh- (proxy|web)\d+/  inherits base {
   case  $:: hostname  {
     /sh-proxy \d+/: {
          include apache
          user { "test1" :
             ensure => present,
             }
       }
      "sh-web1" : {
             include nginx::nginxconf
             include php
             if  str2bool ($is_virtual) { #返回值为true就include apache类.
             include apache
             }
         
     }
}


agent端更新,初次安装模块更新可能输出信息会比较多.

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
[root@sh-web1 ~] # puppet agent -t
Notice: Ignoring --listen on onetime run
Info: Retrieving pluginfacts
Info: Retrieving plugin
Notice:  /File [ /var/lib/puppet/lib/puppet/parser/functions/keys .rb] /ensure : defined content as  '{md5}c10485a3d6c53b6d57a891b9852898de'
Notice:  /File [ /var/lib/puppet/lib/puppet/parser/functions/dig44 .rb] /ensure : defined content as  '{md5}3078b97ee941c261944857373d400ed6'
Notice:  /File [ /var/lib/puppet/lib/puppet/parser/functions/uriescape .rb] /ensure : defined content as  '{md5}ba78def2cd0e60bdc4412df6c7b891ec'
Notice:  /File [ /var/lib/puppet/lib/puppet/parser/functions/validate_ipv4_address .rb] /ensure : defined content as  '{md5}593e8f832469cb6a48c5f16ee66c3b2d'
Notice:  /File [ /var/lib/puppet/lib/puppet/parser/functions/validate_ipv6_address .rb] /ensure : defined content as  '{md5}48d3733012818993eae662839183d139'
Notice:  /File [ /var/lib/puppet/lib/puppet/parser/functions/glob .rb] /ensure : defined content as  '{md5}c4106d2aff24f4b5a32b54bf4cd452a2'
Notice:  /File [ /var/lib/puppet/lib/puppet/parser/functions/validate_integer .rb] /ensure : defined content as  '{md5}65aa35f7450794aaadb6ad2c2e114df7'
Notice:  /File [ /var/lib/puppet/lib/puppet/functions/validate_re .rb] /ensure : defined content as  '{md5}42092f592ebf89b8a504b10c900230d8'
Notice:  /File [ /var/lib/puppet/lib/puppet/provider/file_line/ruby .rb] /ensure : defined content as  '{md5}1cf38c2e1e23f0218ff1b0df4517dd0f'
Notice:  /File [ /var/lib/puppet/lib/puppet/type ] /ensure : created
Notice:  /File [ /var/lib/puppet/lib/puppet/type/anchor .rb] /ensure : defined content as  '{md5}bbd36bb49c3b554f8602d8d3df366c0c'
Notice:  /File [ /var/lib/puppet/lib/puppet/parser/functions/validate_numeric .rb] /ensure : defined content as  '{md5}0e36d370262b8bdef2f88f0a3cb5b30e'
Notice:  /File [ /var/lib/puppet/lib/facter ] /ensure : created
Notice:  /File [ /var/lib/puppet/lib/facter/puppet_settings .rb] /ensure : defined content as  '{md5}9438c0839ae28dc52fffb8348ae5124f'
Notice:  /File [ /var/lib/puppet/lib/facter/service_provider .rb] /ensure : defined content as  '{md5}66cc42526eae631e306b397391f1f01c'
Notice:  /File [ /var/lib/puppet/lib/facter/util ] /ensure : created
Notice:  /File [ /var/lib/puppet/lib/facter/util/puppet_settings .rb] /ensure : defined content as  '{md5}9f1d2593d0ae56bfca89d4b9266aeee1'
Notice:  /File [ /var/lib/puppet/lib/facter/facter_dot_d .rb] /ensure : defined content as  '{md5}d71e93183a680ac78bc0389fd50470a0'
Notice:  /File [ /var/lib/puppet/lib/facter/root_home .rb] /ensure : defined content as  '{md5}35702ae0c7410ec4d2101113e2f697fa'
Notice:  /File [ /var/lib/puppet/lib/facter/package_provider .rb] /ensure : defined content as  '{md5}539766a71dfb2f65e94a7c91bf413fcf'
Notice:  /File [ /var/lib/puppet/lib/puppet/parser/functions/delete_values .rb] /ensure : defined content as  '{md5}b410f5618b4a6158a921acb7b2dc628d'
Notice:  /File [ /var/lib/puppet/lib/puppet/parser/functions/reject .rb] /ensure : defined content as  '{md5}689f6a7c961a55fe9dcd240921f4c7f9'
Notice:  /File [ /var/lib/puppet/lib/puppet/parser/functions/range .rb] /ensure : defined content as  '{md5}ab19430b6b9737cf56263eb65d80cba1'
Notice:  /File [ /var/lib/puppet/lib/puppet/parser/functions/validate_slength .rb] /ensure : defined content as  '{md5}6cbcfe15378ca4a780bac786223aacac'
Notice:  /File [ /var/lib/puppet/lib/puppet/functions/validate_array .rb] /ensure : defined content as  '{md5}9052b0026da174636c276a2512cf5acc'
Notice:  /File [ /var/lib/puppet/lib/facter/pe_version .rb] /ensure : defined content as  '{md5}60d47406026c8201e51394227ddf780d'
Notice:  /File [ /var/lib/puppet/lib/puppet/parser/functions/pick_default .rb] /ensure : defined content as  '{md5}ad3ea60262de408767786d37a54d45dc'
Notice:  /File [ /var/lib/puppet/lib/puppet/functions/is_a .rb] /ensure : defined content as  '{md5}9dad7f8c9b75348cd97aca986ac0b29a'
Notice:  /File [ /var/lib/puppet/lib/puppet/parser/functions/join .rb] /ensure : defined content as  '{md5}2ed4f56d296a4535da142e01b11a126d'
Notice:  /File [ /var/lib/puppet/lib/puppet/functions/validate_ipv4_address .rb] /ensure : defined content as  '{md5}4a5039b99ac97cc0447faa343b9f7416'
Notice:  /File [ /var/lib/puppet/lib/puppet/parser/functions/strip .rb] /ensure : defined content as  '{md5}da0ce253cb63a4863f15f9d145217db5'
Notice:  /File [ /var/lib/puppet/lib/puppet/parser/functions/upcase .rb] /ensure : defined content as  '{md5}e875fc4f03adec1ff3b42d22f177441e'
Notice:  /File [ /var/lib/puppet/lib/puppet/functions/is_float .rb] /ensure : defined content as  '{md5}af3bd6bb56878bac8cc4fe4f7564e4f9'
Notice:  /File [ /var/lib/puppet/lib/puppet/type/file_line .rb] /ensure : defined content as  '{md5}7486754fca55fe98d40f268b032b7e6a'
Notice:  /File [ /var/lib/puppet/lib/puppet/parser/functions/time .rb] /ensure : defined content as  '{md5}cd96d1f039f8875af083091e3637190b'
Notice:  /File [ /var/lib/puppet/lib/puppet/parser/functions/is_bool .rb] /ensure : defined content as  '{md5}b8800ff7a11b4e8c03616041e218225f'
Notice:  /File [ /var/lib/puppet/lib/puppet/parser/functions/deprecation .rb] /ensure : defined content as  '{md5}4323210434d36e37977251f906a232b8'
Notice:  /File [ /var/lib/puppet/lib/puppet/parser/functions/validate_string .rb] /ensure : defined content as  '{md5}8afa7b0dcfe17bfbbb5704ad54664cc2'
Notice:  /File [ /var/lib/puppet/lib/puppet/functions/is_string .rb] /ensure : defined content as  '{md5}230e9eabc5c9e1d8d5fb7b3c6c12b300'
Notice:  /File [ /var/lib/puppet/lib/puppet/parser/functions/reverse .rb] /ensure : defined content as  '{md5}209e7ef512963251571c515e2d0aee10'
Notice:  /File [ /var/lib/puppet/lib/puppet/parser/functions/type .rb] /ensure : defined content as  '{md5}4709f7ab8a8aad62d77a3c5d91a3aa08'
Notice:  /File [ /var/lib/puppet/lib/puppet/parser/functions/zip .rb] /ensure : defined content as  '{md5}133f3d4c54640844e656e2e6e790318e'
Notice:  /File [ /var/lib/puppet/lib/puppet/parser/functions/ensure_resource .rb] /ensure : defined content as  '{md5}de703fe63392b939fc2b4392975263de'
Notice:  /File [ /var/lib/puppet/lib/puppet/parser/functions/validate_re .rb] /ensure : defined content as  '{md5}d5963c404e3ac1670553f306221c2655'
Notice:  /File [ /var/lib/puppet/lib/puppet/parser/functions/shell_join .rb] /ensure : defined content as  '{md5}b99a23d5e62e2e1b98accde5c22e45c9'
Notice:  /File [ /var/lib/puppet/lib/puppet/functions/validate_ip_address .rb] /ensure : defined content as  '{md5}65a12af9a2c2a9c70d820d04d19ec891'
Notice:  /File [ /var/lib/puppet/lib/puppet/parser/functions/is_function_available .rb] /ensure : defined content as  '{md5}f13934d6b41561ef54d88cf0da86231b'
Notice:  /File [ /var/lib/puppet/lib/puppet/parser/functions/str2saltedsha512 .rb] /ensure : defined content as  '{md5}457ab12e4329494ae6276cfa4f20eb23'
Notice:  /File [ /var/lib/puppet/lib/puppet/parser/functions/delete_undef_values .rb] /ensure : defined content as  '{md5}52beef9ee37f84ed2278a69ec4383125'
Notice:  /File [ /var/lib/puppet/lib/puppet/parser/functions/fqdn_rotate .rb] /ensure : defined content as  '{md5}2483d17df4e6cb25d92b4e8520f30957'
Notice:  /File [ /var/lib/puppet/lib/puppet/parser/functions/validate_ip_address .rb] /ensure : defined content as  '{md5}b23c3d5ce6839e32d0186411147a6a44'
Notice:  /File [ /var/lib/puppet/lib/puppet/parser/functions/is_email_address .rb] /ensure : defined content as  '{md5}1eb786779743e93a7bb9fe8087b38b8d'
Notice:  /File [ /var/lib/puppet/lib/puppet/parser/functions/values_at .rb] /ensure : defined content as  '{md5}f7e6ad2a1126acd4fb5f7fcf9bfc2e2b'
Notice:  /File [ /var/lib/puppet/lib/puppet/functions/to_json .rb] /ensure : defined content as  '{md5}bb6893a3e9eb6c48064c5a3c0bd4a41a'
Notice:  /File [ /var/lib/puppet/lib/puppet/parser/functions/getvar .rb] /ensure : defined content as  '{md5}0c8c5cef7e158e232a8cf6e42c10d0ff'
Notice:  /File [ /var/lib/puppet/lib/puppet/parser/functions/strftime .rb] /ensure : defined content as  '{md5}8f15e2e3732b6d1d357a1fa1826800d4'
Notice:  /File [ /var/lib/puppet/lib/puppet/parser/functions/chop .rb] /ensure : defined content as  '{md5}3beb80906fa3c759945a2664fe510b20'
Notice:  /File [ /var/lib/puppet/lib/puppet/parser/functions/ensure_resources .rb] /ensure : defined content as  '{md5}c92d8b69d6354eda24aa3a13d88177b2'
Notice:  /File [ /var/lib/puppet/lib/puppet/parser/functions/is_float .rb] /ensure : defined content as  '{md5}6257620b98c5099293be7aa4088b88ce'
Notice:  /File [ /var/lib/puppet/lib/puppet/parser/functions/parsejson .rb] /ensure : defined content as  '{md5}15165fd3807d9f3d657697fa854d643d'
Notice:  /File [ /var/lib/puppet/lib/puppet/parser/functions/validate_cmd .rb] /ensure : defined content as  '{md5}7df12370db442eddddcf4dd7a5364b5e'
Info: Loading facts
Info: Caching catalog  for  sh-web1.localdomain
Info: Applying configuration version  '1506348730'
Notice:  /Stage [main] /Admin/Exec [selinux] /returns : executed successfully
Notice:  /Stage [main] /Php/Package [php] /ensure : created
Notice:  /Stage [main] /Php/Package [php-devel] /ensure : created
Notice:  /Stage [main] /Service/Service [httpd] /ensure : ensure changed  'stopped'  to  'running'
Info:  /Service [httpd]: Unscheduling refresh on Service[httpd]
Notice: Finished catalog run  in  11.61 seconds


扩展:

        puppet-stdlib模块包含2个函数,str2bool和num2boole,str2bool可以将字符串转换为布尔型,numboole可以将数值转换为布尔型.


str2bool

Converts certain strings to a Boolean. This attempts to convert strings that contain the values '1', 'true', 't', 'y', or 'yes' to true. Strings that contain values '0', 'false', 'f', 'n', or 'no', or that are an empty string or undefined are converted to false. Any other value causes an error. These checks are case insensitive.


注释:返回值包含'1', 'true', 't', 'y', or 'yes'判断为true,'0', 'false', 'f', 'n', or 'no'判断为false.