|
Puppet代码的规范
puppet对agent的配置清单主要集中在manifests和modules两个目录中,manifests和modules目录中清单代码文件的符号间距、缩进与空白的使用规范.
*.pp文件
建议使用两个空格的软标签.
不推荐使用制表符.
尾部不要包含空格.
建议单行不要超过80个字符的宽度.
在资源的属性中,通过=>符号进行属性对齐。
注释
puppet的注释目前支持两种风格:
一种shell脚本风格."#"
1
2
3
4
| #this is test
package {"httpd":
ensure=> present,
}
|
另一种C语言风格以/*开始,*/结束的多行注释,不支持//注释.
1
2
3
4
5
6
| /*
this is test
*/
package {"httpd":
ensure=> present,
}
|
变量规范:
变量只包含字母[a-z][A-Z]、数字[0-9]和下划线(_)。
正确:
错误:
'',"",{}等符号,在不包含变量的字符串中都应该使用‘’进行引用;如果字符串中包含变量则通过""进行引用;如果字符串中既有变量又有字符串,可以通过{}进行引用.
正确:
1
2
| /etc/${file}.conf
"${::operatingssytem} is not supported by ${module_name}"
|
不推荐:(这样写也正确,官方不推荐)
1
2
| /etc/${file}.conf
"$::operatingssytem is not supported by $module_name"
|
变量被引用时不加""。(我之前代码都加的有,官方不推荐)
正确:
不推荐:
1
2
| mode => "$mode"
mode => "${mode}"
|
资源规范:
资源的标题需要用单引号''或双引号""引起来,同时标题中不能包含空格和连字符.
资源标题:
推荐:
1
2
3
4
5
6
| package {"httpd":
ensure=> present,
}
package {'httpd':
ensure=> present,
}
|
不推荐:
1
2
3
| package {httpd:
ensure=> present,
}
|
资源符号对齐:
推荐:
1
2
3
4
| package {"httpd":
ensure=> present,
owner=> root,
}
|
不推荐:
1
2
3
4
| package {"httpd":
ensure=> present,
owner=> root,
}
|
资源属性顺序:
推荐:
1
2
3
4
| package {"httpd":
ensure=> present,#ensure优先放在前面.
owner=> root,
}
|
不推荐:
1
2
3
4
| package {"httpd":
owner=> root,
ensure=> present,
}
|
资源关系:
资源应该由逻辑关系被划分为一组,而非通过资源类型划分.
推荐:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| class nginx {
package {"nginx":
ensure => present,
}
service {"nginx":
ensure => true,
enable => true,
require => Package['nginx'], #引用依赖首字母要大写
}
}
class httpd {
package {"httpd":
ensure => present,
}
service {"httpd":
ensure => true,
enable => true,
require => Package['httpd'], #引用依赖首字母要大写
}
}
|
不推荐:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| class package {
package {"nginx":
ensure => present,
}
package {"httpd":
ensure => present,
}
}
class service {
service {"nginx":
ensure => true,
enable => true,
require => Package['nginx'], #引用依赖首字母要大写
}
service {"httpd":
ensure => true,
enable => true,
require => Package['httpd'], #引用依赖首字母要大写
}
}
|
软连接
通过file资源建立软连接的时候,需要设置ensure => link,并通过target属性来指定目标文件.
1
2
3
4
| file {"/tmp/3.pp":#软连接文件.(目标文件)
ensure=> link,
target=> '/root/3.pp',#源文件.
}
|
注释:/root/3.pp文件,我想软连接到/tmp下.
正确写法:
1
2
3
4
5
| [iyunv@sh-web1 ~]# cat link.pp
file {"/tmp/3.pp":
ensure=> link,
target=> '/root/3.pp',
}
|
错误写法:
1
2
3
| file {"/tmp/3.pp":
target=> '/root/3.pp',
}
|
agent本地应用*.pp文件:
1
2
3
4
5
6
7
| [iyunv@sh-web1 ~]# puppet apply link.pp
Notice: Compiled catalog for sh-web1.localdomain in environment production in 0.07 seconds
Notice: /Stage[main]/Main/File[/tmp/3.pp]/ensure: created
Notice: Finished catalog run in 0.02 seconds
[iyunv@sh-web1 ~]# ls /tmp/
3.pp yum_save_tx-2017-09-22-17-52B5ZM81.yumtx yum_save_tx-2017-09-26-02-00w5fKzh.yumtx
text.txt yum_save_tx-2017-09-22-17-52b9CPEA.yumtx yum_save_tx-2017-09-26-18-03lhfm3k.yumtx
|
查看软连接文件的内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| [iyunv@sh-web1 ~]# cat /tmp/3.pp
$package = ['php','php-devel']
class nginx {
$packages += ['php-pecl-geoip']
package {[packages]:
ensure=> present,
}
}
class apache {
$packages += ['httpd']
package {[packages]:
ensure => present,
}
}
include nginx
|
#查看源文件的内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| [iyunv@sh-web1 ~]# cat 3.pp
$package = ['php','php-devel']
class nginx {
$packages += ['php-pecl-geoip']
package {[packages]:
ensure=> present,
}
}
class apache {
$packages += ['httpd']
package {[packages]:
ensure => present,
}
}
include nginx
|
文件模式:
通过file资源设置文件权限时要注意:
1、文件权限应该由4位数字组成,而非3位.(之前文章都是三位,官网推荐四位)
2、权限数字应该要使用''引起来.(之前文章这块有些没有引)
推荐:
1
2
3
4
| file {"/tmp/test.txt":
ensure =>file,
mode =>'0644',
}
|
不推荐:
1
2
3
4
| file {"/tmp/test.txt":
ensure =>file,
mode =>644,
}
|
if条件语句的规范
通常不建议将selector语句与资源混用.
推荐写法:
1
2
3
4
5
6
7
8
9
| $file_mode= $::operatingssytem ? {
debian=> '0700',
redhat=> '0644',
Centos=> '0644',
}
file {'/tmp/test.txt':
conten => "hello world\n",
mode => $file_mode,
}
|
不推荐写法:
1
2
3
4
5
6
7
8
9
| file {'/tmp/test.txt':
mode => $::operatingssytem ? {
debian=> '0700',
redhat=> '0644',
Centos=> '0644',
conten => "hello world\n",
mode => $file_mode,
}
}
|
class的规范:
符号关联关系:
通过->符号建立资源之间的关联关系的顺序为从"左到右".
正确写法:
Package["httpd"] -> Service["httpd"]
错误写法:
Service["httpd"] -> Package["httpd"]
模块的继承:
继承可以在模块中使用,但不推荐跨模块的命名空间使用.
class ssh{}
class ssh::client inherits {}
class ss::service inherits {}
跨模块继承(不推荐):
class ssh{}
class ssh::client work {}
class ss::service apache {}
标示符命名规范:
1)变量命名规则
符合正则表达式规范(\A\$[a-zA-Z0-9_]+\Z),不包含特殊的字符.如%@^等.
变量名区分大小写,如$foo和$FOO为不同的变量。
class类的命名规范:
符合正则表达式规范(\A\$[a-zA-Z0-9_]+\Z),不包含特殊的字符.如%@^等.
如果类名中使用了命名空间需要以"::"作为分隔,并符合正则表达式规范(\A([a-z][a-z0-9_]*)?(::[a-z][a-zA-Z0-9]*)*\Z)
modules命名规则:
符合正则表达式规范。
模块名的首字母不能为大写.
tag命名规则:符合正则表达式规范(/\A[a-z0-9._-]+\Z)。
nodes节点命名规则:符合正则表达式规范(/\A[a-z0-9._-]+\Z)。
puppet文件的导入和类的声明
在manifests目录内文件与文件之间的导入功能通常使用import函数来完成。
示例1:
site.pp
import nodes
示例2:
site.pp
import 'nodes/*.pp'
注释:import函数可以导入manifests目录中的一个文件,也可以导入多个文件,多个文件可以使用通配符"*"来表示.
类的声明:
1
2
3
4
| node base {
include ntp
include apache
}
|
|
|