自动化运维工具Puppet 快速入门(2)

file指的是普通文件,link为连接文件,此需结合target一起使用 directory:类型为目录,可通过source指向的路径复制生成,recuse属性指明是否为递归复制

owner:属主 group:属组 mode:权限

[root@node1 manifets]# puppet apply -v  web.pp

Notice: Compiled catalog for node1.alren.com in environment production in 1.48 seconds

Info: Applying configuration version '1480563754'

Info: Computing checksum on file /etc/httpd/conf/httpd.conf

Info: /Stage[main]/Main/File[httpd.conf]: Filebucketed /etc/httpd/conf/httpd.conf to puppet with sum 42566ec31df37e3d44429b285d015e1d

Notice: /Stage[main]/Main/File[httpd.conf]/content: content changed '{md5}42566ec31df37e3d44429b285d015e1d' to '{md5}8b01e334a6e975b659df5dd351923ccb'

Info: /Stage[main]/Main/File[httpd.conf]: Scheduling refresh of Service[httpd]

Notice: /Stage[main]/Main/Service[httpd]: Triggered 'refresh' from 1 events

Notice: Finished catalog run in 1.95 seconds

[root@node1 manifets]# ss -tnl

State      Recv-Q Send-Q Local Address:Port              Peer Address:Port

LISTEN    0      5      192.168.122.1:53                    *:*

LISTEN    0      128        *:22                    *:*

LISTEN    0      128    127.0.0.1:631                    *:*

LISTEN    0      100    127.0.0.1:25                    *:*

LISTEN    0      32        :::21                    :::*

LISTEN    0      128      :::22                    :::*

LISTEN    0      128      ::1:631                  :::*

LISTEN    0      128      :::8088                  :::*

LISTEN    0      100      ::1:25                    :::*

[root@node1 manifets]#

实例三:每三分钟同步下系统时间,写入定时任务

cron{'synctime':

command  => '/usr/sbin/ntpdate 10.1.0.1 &>/dev/null', #须执行的命令

ensure    => present,  #创建任务计划

minute    => '*/5',    #过多长是时间执行

user      => root,    #以谁的身份运行

}

[root@node1 testmanifests]# puppet apply -v cron.pp

Notice: Compiled catalog for node1.alren.com in environment production in 0.18 seconds

Info: Applying configuration version '1480593969'

Notice: /Stage[main]/Main/Cron[synctime]/minute: minute changed '*/3' to '*/5'

Notice: Finished catalog run in 0.09 seconds

[root@node1 testmanifests]# crontab -l

# HEADER: This file was autogenerated at 2016-12-01 20:06:10 +0800 by puppet.

# HEADER: While it can still be managed manually, it is definitely not recommended.

# HEADER: Note particularly that the comments starting with 'Puppet Name' should

# HEADER: not be deleted, as doing so could cause duplicate cron jobs.

# Puppet Name: synctime

*/5 * * * * /usr/sbin/ntpdate 10.1.0.1 &>/dev/null  #查看到此任务计划存在

[root@node1 testmanifests]#

实例四:puppet之if条件判断

if $osfamily =~ /(?i-mx:debian)/ {  $osfamily为facter中取得的内嵌变量

$webserver = 'apache2' #自定义变量

} else {

$webserver = 'httpd'

}

package{"$webserver":

ensure    => installed,

before    => [ File['httpd.conf'], Service['httpd'] ],

}

file{'httpd.conf':

path      => '/etc/httpd/conf/httpd.conf',

source    => '/root/testmanifests/httpd.conf',

ensure    => file,

}

service{'httpd':

ensure    => running,

enable    => true,

restart  => 'systemctl restart httpd.service',

subscribe => File['httpd.conf'], #通知其他的资源进行刷新操作

}

[root@node1 testmanifests]# puppet apply -v if.pp

Notice: Compiled catalog for node1.alren.com in environment production in 1.53 seconds

Info: Applying configuration version '1480594920'

Info: Computing checksum on file /etc/httpd/conf/httpd.conf

Info: FileBucket got a duplicate file {md5}8b01e334a6e975b659df5dd351923ccb

Info: /Stage[main]/Main/File[httpd.conf]: Filebucketed /etc/httpd/conf/httpd.conf to puppet with sum 8b01e334a6e975b659df5dd351923ccb

Notice: /Stage[main]/Main/File[httpd.conf]/content: content changed '{md5}8b01e334a6e975b659df5dd351923ccb' to '{md5}42566ec31df37e3d44429b285d015e1d'

Info: /Stage[main]/Main/File[httpd.conf]: Scheduling refresh of Service[httpd]

Notice: /Stage[main]/Main/Service[httpd]: Triggered 'refresh' from 1 events

Notice: Finished catalog run in 1.97 seconds

[root@node1 testmanifests]# ss -tnl

State      Recv-Q Send-Q Local Address:Port              Peer Address:Port

LISTEN    0      5      192.168.122.1:53                    *:*

LISTEN    0      128        *:22                    *:*

LISTEN    0      128    127.0.0.1:631                    *:*

LISTEN    0      100    127.0.0.1:25                    *:*

LISTEN    0      128      :::80                    :::*

LISTEN    0      32        :::21                    :::*

LISTEN    0      128      :::22                    :::*

LISTEN    0      128      ::1:631                  :::*

LISTEN    0      100      ::1:25                    :::*

[root@node1 testmanifests]#

实例五:puppet之case语句

case $osfamily {

"RedHat": { $webserver='httpd' }

/(?i-mx:debian)/: { $webserver='apache2' }

default: { $webserver='httpd' }

}

package{"$webserver":

ensure=> installed,

before=> [ File['httpd.conf'], Service['httpd'] ],

}

file{'httpd.conf':

ensure=> file,

path => '/etc/httpd/conf/httpd.conf',

source=> '/root/testmanifests/httpd.conf',

}

service{'httpd':

ensure=> running,

enable=> true,

restart => 'systemctl restart httpd.service',

subscribe=> File['httpd.conf'],

}

[root@node1 testmanifests]# puppet apply -v case.pp

Notice: Compiled catalog for node1.alren.com in environment production in 1.61 seconds

Info: Applying configuration version '1480595667'

Info: Computing checksum on file /etc/httpd/conf/httpd.conf

Info: FileBucket got a duplicate file {md5}42566ec31df37e3d44429b285d015e1d

Info: /Stage[main]/Main/File[httpd.conf]: Filebucketed /etc/httpd/conf/httpd.conf to puppet with sum 42566ec31df37e3d44429b285d015e1d

Notice: /Stage[main]/Main/File[httpd.conf]/content: content changed '{md5}42566ec31df37e3d44429b285d015e1d' to '{md5}3dfa14b023127a3766bddfe15fe14b9a'

Info: /Stage[main]/Main/File[httpd.conf]: Scheduling refresh of Service[httpd]

Notice: /Stage[main]/Main/Service[httpd]: Triggered 'refresh' from 1 events

Notice: Finished catalog run in 1.91 seconds

[root@node1 testmanifests]# #可根据自行的语言习惯选择适合的语法,如下所示同样可达到上诉的目的

$webserver = $osfamily ? {

"Redhat" => 'httpd',

/(?i-mx:debian)/ => 'apache2',

default => 'httpd',

}

package{"$webserver":

ensure  => installed,

before  => [ File['httpd.conf'], Service['httpd'] ],

}

file{'httpd.conf':

path    => '/etc/httpd/conf/httpd.conf',

source  => '/root/testmanifests/httpd.conf',

ensure  => file,

}

service{'httpd':

ensure  => running,

enable  => true,

restart => 'systemctl restart httpd.service',

subscribe => File['httpd.conf'],

}

四、puppet类与继承

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/464aded2de2fbfed27865d44ef8bd14f.html