[root@master1 puppet]# yum install -y puppet-3.8.4-1.el7.noarch.rpm facter-2.4.1-1.el7.x86_64.rpm
puppet help
[root@master1 ~]# puppet describe --list
[root@master1 ~]# puppet describe package
[root@master1 ~]# mkdir manifests
[root@master1 ~]# cd manifests/
[root@master1 manifests]# vim test1.pp
group {‘distro‘:
gid => 2000,
ensure => present,
}
user{‘centos‘:
uid => 2000,
gid => 2000,
shell => ‘/bin/bash‘,
home => ‘/home/centos‘,
ensure => present,
}
[root@master1 manifests]# puppet apply -v test1.pp
Notice: Compiled catalog for master1.master1.com in environment production in 0.57 seconds
Info: Applying configuration version ‘1484681988‘
Notice: /Stage[main]/Main/Group[distro]/ensure: created
Notice: /Stage[main]/Main/User[centos]/ensure: created
Info: Creating state file /var/lib/puppet/state/state.yaml
Notice: Finished catalog run in 0.41 seconds
[root@master1 ~]# cat /var/lib/puppet/state/state.yaml
[root@master1 ~]# tail /etc/group | grep 2000
distro:x:2000:
[root@master1 ~]# tail /etc/passwd | grep 2000
centos:x:2000:2000::/home/centos:/bin/bash
[root@master1 ~]#
[root@master1 manifests]# vim test2.pp
file {‘/tmp/mydir‘:
ensure => directory,
}
ensurce => directory,
file {‘/tmp/mydir‘:
ensurce => directory,
}
file {‘/tmp/puppet.file‘:
content => ‘puppet testing\nsecond line.‘,
ensure => file,
owner => ‘centos‘,
group => ‘distro‘,
mode => ‘0400‘,
}
file {‘/tmp/fstab.puppet‘:
source => ‘/etc/fstab‘,
ensure => file,
}
file {‘/tmp/puppet.link‘:
ensure => link,
target => ‘/tmp/puppet.file‘,
}
[root@master1 tmp]# ll /tmp/
total 8
-rw-r--r-- 1 root root 1065 Jan 18 13:12 fstab.puppet
drwxr-xr-x 2 root root 6 Jan 18 13:12 mydir
-r-------- 1 centos distro 28 Jan 18 13:12 puppet.file
lrwxrwxrwx 1 root root 16 Jan 18 13:12 puppet.link -> /tmp/puppet.file
[root@master1 tmp]#
exec {‘/usr/sbin/modprobe ext4‘:
user => root,
group => root,
refresh => ‘/usr/sbin/modprobe -r ext4 && /usr/sbin/modprobe ext4‘,
timeout => 5,
tries => 2,
}
[root@master1 ~]# puppet apply -v test3.pp
Notice: Compiled catalog for master1.master1.com in environment production in 0.14 seconds
Info: Applying configuration version ‘1484724300‘
Notice: /Stage[main]/Main/Exec[/usr/sbin/modprobe ext4]/returns: executed successfully
Notice: Finished catalog run in 0.30 seconds
exec {‘/usr/bin/echo hello > /tmp/hello.txt‘:
user => root,
group => root,
}
exec {‘/usr/bin/echo mageedu > /tmp/hello.txt‘:
user => root,
group => root,
creates => ‘/tmp/hello.txt‘,
}
测试:文件存在没有运行
[root@master1 ~]# puppet apply -v test3.pp
Notice: Compiled catalog for master1.master1.com in environment production in 0.14 seconds
Info: Applying configuration version ‘1484730095‘
Notice: /Stage[main]/Main/Exec[/usr/sbin/modprobe ext4]/returns: executed successfully
Notice: Finished catalog run in 0.07 seconds
测试:删除掉文件后,运行了:
[root@master1 ~]# rm /tmp/hello.txt
rm: remove regular file ‘/tmp/hello.txt’? y
[root@master1 ~]# puppet apply -v test3.pp
Notice: Compiled catalog for master1.master1.com in environment production in 0.14 seconds
Info: Applying configuration version ‘1484730156‘
Notice: /Stage[main]/Main/Exec[/usr/bin/echo mageedu > /tmp/hello.txt]/returns: executed successfully
Notice: /Stage[main]/Main/Exec[/usr/sbin/modprobe ext4]/returns: executed successfully
如果test命令执行失败,才会执行上面的命令:
exec {‘/usr/bin/echo mageedu > /tmp/hello2.txt‘:
user => root,
group => root,
unless => ‘/usr/bin/test -e /tmp/hello2.txt‘,
}
测试:第一次文件不存在,执行:
[root@master1 ~]# puppet apply -v test3.pp
Notice: Compiled catalog for master1.master1.com in environment production in 0.14 seconds
Info: Applying configuration version ‘1484730808‘
Notice: /Stage[main]/Main/Exec[/usr/bin/echo mageedu > /tmp/hello2.txt]/returns: executed successfully
Notice: /Stage[main]/Main/Exec[/usr/sbin/modprobe ext4]/returns: executed successfully
测试:第二次,文件存在不执行:
[root@master1 ~]# puppet apply -v test3.pp
Notice: Compiled catalog for master1.master1.com in environment production in 0.14 seconds
Info: Applying configuration version ‘1484730849‘
Notice: /Stage[main]/Main/Exec[/usr/sbin/modprobe ext4]/returns: executed successfully
Notice: Finished catalog run in 0.20 seconds
[root@master1 ~]# vim test4.pp
notify{"hello there.":}
测试:
[root@master1 ~]# puppet apply -v test4.pp
Notice: Compiled catalog for master1.master1.com in environment production in 0.06 seconds
Info: Applying configuration version ‘1484734291‘
Notice: hello there.
Notice: /Stage[main]/Main/Notify[hello there.]/message: defined ‘message‘ as ‘hello there.‘
[root@master1 ~]# vim test5.pp
cron{"sync time":
command => ‘/usr/sbin/ntpdate s2c.time.edu.cn &> /dev/null‘,
minute => ‘*/10‘,
}
测试:
[root@master1 ~]# puppet apply -v test5.pp
Notice: Compiled catalog for master1.master1.com in environment production in 0.16 seconds
Info: Applying configuration version ‘1484738321‘
Notice: /Stage[main]/Main/Cron[sync time]/ensure: created
*/10 * * * * /usr/sbin/ntpdate s2c.time.edu.cn &> /dev/null
删除计划任务:
cron{"sync time":
command => ‘/usr/sbin/ntpdate s2c.time.edu.cn &> /dev/null‘,
minute => ‘*/10‘,
ensure => absent, ##
}
原文地址:http://blog.51cto.com/zhongle21/2089225