2019独角兽企业重金招聘Python工程师标准>>>
检查语法:
我们先创建一个正确的的类:
vi init.pp
#增加这些东西
class nginx {package { 'nginx' : ensure => present, }
}
执行语法检查:
puppet parser validate init.pp
如果无误,则木有提示。
修改一下,使之成为一个有问题的类,改成这样:
class 'nginx' {package { nginx ensure => present, }
}
在这里,有两个错误和一个不规范。
检查语法看看:
root@docker:~# puppet parser validate init.pp
Error: Could not parse for environment production: Syntax error at 'nginx' at /root/init.pp:1
提示问题出在第一行,那是因为类名不能再引号内。把引号去掉之后,再检查一次:
root@docker:~# puppet parser validate init.pp
Error: Could not parse for environment production: Syntax error at 'ensure'; expected '}' at /root/init.pp:2
问题出在第二行,因为资源名后要跟":",把ensure分隔开。修改之后,继续检查:
root@docker:~# puppet parser validate init.pp
root@docker:~#
没有提示即表明检查通过。
注意:资源名可以不在引号内,但建议使用引号!
代码调试:
创建一个test.pp,定义一个exec资源:
vi test.pp
#添加以下内容
exec { 'test' :command => "/bin/ls abc.txt", #在本地执行ls,检查是否有abc.txt这个文件logoutput => on_failure, #配置logoutput,输出更多信息
}
输出如下:
root@docker:~# puppet apply test.pp
Notice: Compiled catalog for docker.hzg.com in environment production in 0.07 seconds
Notice: /Stage[main]/Main/Exec[test]/returns: /bin/ls: cannot access abc.txt: No such file or directory
Error: /bin/ls abc.txt returned 2 instead of one of [0]
Error: /Stage[main]/Main/Exec[test]/returns: change from notrun to 0 failed: /bin/ls abc.txt returned 2 instead of one of [0]
Notice: Finished catalog run in 0.18 seconds
留意Notice行:提示No such file or directory
我们还可以选择通过notify显示信息:
修改test.pp,追加内容:
echo 'notify { "I am running on node $fqdn": }' >> test.pp
提示信息如下:
root@docker:~# puppet apply test.pp
Notice: Compiled catalog for docker.hzg.com in environment production in 0.07 seconds
Notice: /Stage[main]/Main/Exec[test]/returns: /bin/ls: cannot access abc.txt: No such file or directory
Error: /bin/ls abc.txt returned 2 instead of one of [0]
Error: /Stage[main]/Main/Exec[test]/returns: change from notrun to 0 failed: /bin/ls abc.txt returned 2 instead of one of [0]
Notice: I am running on node docker.hzg.com
Notice: /Stage[main]/Main/Notify[I am running on node docker.hzg.com]/message: defined 'message' as 'I am running on node docker.hzg.com'
Notice: Finished catalog run in 0.22 seconds
留意这一行,把我们需要输出的信息显示出来了,功能和echo差不多:
Notice: I am running on node docker.hzg.com
备注:$fqdn是facter自带的变量值
获取命令帮助:
puppet help
这两句给出了最简单的提示:
See 'puppet help <subcommand> <action>' for help on a specific subcommand action.
See 'puppet help <subcommand>' for help on a specific subcommand.
意味着我们可以使用诸如以下的命令获取帮助
puppet help agent
也可以这样:
puppet SUBCOMMAND --help
puppet master和agent的用法和一些重要参数:
master:
puppet master [-D|--daemonize|--no-daemonize] [-d|--debug] [-h|--help]
[-l|--logdest <file>|console|syslog] [-v|--verbose] [-V|--version]
[--compile <node-name>]
--debug #-d 启用完整的调试模式,输出很多信息
--verbose #-v 输出详细信息
--logdest #-l 日志发送方式,默认用syslog
--version #-V 打印版本号
--compile #以JSON方式输出编译的catalog
agent:
puppet agent [--certname <name>] [-D|--daemonize|--no-daemonize]
[-d|--debug] [--detailed-exitcodes] [--digest <digest>] [--disable [message]] [--enable]
[--fingerprint] [-h|--help] [-l|--logdest syslog|<file>|console]
[--no-client] [--noop] [-o|--onetime] [-t|--test]
[-v|--verbose] [-V|--version] [-w|--waitforcert <seconds>]
puppet apply相关:
puppet apply用于检测manifests或离线时使用,此时agent不会连接到master。
puppet apply [-h|--help] [-V|--version] [-d|--debug] [-v|--verbose]
[-e|--execute] [--detailed-exitcodes] [-l|--logdest <file>] [--noop]
[--catalog <catalog>] [--write-catalog-summary] <file>
用法实例:
1.把日志输出到指定文件:
puppet apply -l /var/log/init.log test.pp #还是之前例子中的test.pp
查看该日志:
2014-08-27 16:36:48 +0800 Puppet (notice): Compiled catalog for docker.hzg.com in environment production in 0.21 seconds
2014-08-27 16:36:52 +0800 /Stage[main]/Main/Exec[test]/returns (notice): /bin/ls: cannot access abc.txt: No such file or directory
2014-08-27 16:36:52 +0800 Puppet (err): /bin/ls abc.txt returned 2 instead of one of [0]
2014-08-27 16:36:52 +0800 /Stage[main]/Main/Exec[test]/returns (err): change from notrun to 0 failed: /bin/ls abc.txt returned 2 instead of one of [0]
2014-08-27 16:36:52 +0800 Puppet (notice): I am running on node docker.hzg.com
2014-08-27 16:36:52 +0800 /Stage[main]/Main/Notify[I am running on node docker.hzg.com]/message (notice): defined 'message' as 'I am running on node docker.hzg.com'
2014-08-27 16:36:52 +0800 Puppet (notice): Finished catalog run in 0.26 seconds
2.使用-e执行命令中的puppet代码(记住,是puppet代码!):
agent上创建一个test模块:
mkdir -p /etc/puppet/modules/test/{manifests,files,templates}
vi /etc/puppet/modules/test/manifests/init.pp
#增加这些内容:
class test {file { '/home/hello.txt' :ensure => file,content => 'hello world',}
}
先用--noop模拟运行:
root@docker:~# puppet apply -e "include test" --noop
Notice: Compiled catalog for docker.hzg.com in environment production in 0.17 seconds
Notice: /Stage[main]/Test/File[/home/hello.txt]/ensure: current_value absent, should be file (noop)
Notice: Class[Test]: Would have triggered 'refresh' from 1 events
Notice: Stage[main]: Would have triggered 'refresh' from 1 events
Notice: Finished catalog run in 0.18 seconds
可以看到-e所指定的命令是master节点上定义docker.hzg.com.pp中所用到的一条puppet代码,用于加载test类。
执行实际变更:
puppet apply -e "include test"
root@docker:/home# ls
hello.txt jack mywords.txt puppet.conf success.txt
可以看到相应的文件已经被创建。