Puppet 案例介绍

简介:

 这里我也就不去注重文章的布局,就按照我显示的情况进行了一些简单的配置,前提是一些简单的前期配置我们都已经部署好了。

一、授权比较麻烦,配置自动授权

1、创建自动签署文件

# cat /etc/puppet/autosign.conf 

1
2
*.comratings.com          // 对所以主机名*.comratings.com自动签署
cat  /etc/puppet/puppet .conf

2、填写配置文件使其生效

# cat /etc/puppet/puppet.conf 

1
2
3
[master]
     autosign =  true
     autosign =  /etc/puppet/autosign .conf


二、其实我是比较不喜欢主机配置ipv6地址的,我可以设定默认节点取消ipv6

1、首先应该创建好配置文件

# cat /etc/puppet/files/ipv6.conf

1
2
alias  net-pf-10 off
alias  ipv6 off

2、添加授权,要不客户端没有权限去获取文件

# cat /etc/puppet/fileserver.conf 

1
2
3
[files]
    path  /etc/puppet/files
    allow *.comratings.com

3、创建该资源的类

    我们可以把类直接放在site.pp里面,然后调用,或者放在模块里面调用,放在模块创建起来稍微麻烦一些,我们这里统一放在site.pp里面

# cat /etc/puppet/manifests/site.pp

1
2
3
4
5
6
7
8
9
node default {
    include ipv6
}
 
class ipv6 {
     file  { '/etc/modprobe.d/ipv6.conf' :
           source  =>  "puppet:///files/ipv6.conf" ,
     }
}

    客户端配置好hosts,配置文件就可以获取配置文件了,客户端重启之后就取消ipv6了,原理都是想通的,想要增加其他文件都可以这样操作,修改文件也可以,对文件内容进行追加也是可以的,具体.pp文件怎么写需要查看资源file的用法了。

    也可以直接在ipv6.pp里面填写如下内容:

1
2
3
4
5
6
class ipv6 {
     $mycontent =  "alias net-pf-10 off\nalias ipv6 off\n"
     file { '/root/a.txt' :
         content =>  "$mycontent" ,
     }
}


三、我想增加一个定时任务,对集群所有机器进行时间同步

1、创建时间同步类资源

# cat /etc/puppet/manifests/site.pp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
node default {
    include ipv6
    include ntpdate
}
 
class ipv6 {
    file  { '/etc/modprobe.d/ipv6.conf' :
           source  =>  "puppet:///files/ipv6.conf" ,
     }
}
 
class ntpdate {
    cron  { 'ntpdate' :
         ensure  => present,
         command  =>  '(/usr/sbin/ntpdate time.nist.gov && /sbin/hwclock -w) &> /dev/null' ,
         user    =>  'root' ,
         hour    =>  '*/12' ,
     }
}


    可以通过crontab -u root -l查看对应用户的定时任务情况,除了这个方法,我们也可以通过file资源对/etc/crontab里面进行追加添加定时内容。


四、很多时间我都是最小化安装linux系统,一些常用的软件包是没有安装的,我要安装绝大部分常用的软件

1、创建软件包资源文件

# cat /etc/puppet/manifests/site.pp 

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
node default {
    include ipv6
    include ntpdate
    include software
}
 
class ipv6 {
    file  { '/etc/modprobe.d/ipv6.conf' :
           source  =>  "puppet:///files/ipv6.conf" ,
     }
}
 
class ntpdate {
    cron  { 'ntpdate' :
         ensure  => present,
         command  =>  '(/usr/sbin/ntpdate time.nist.gov && /sbin/hwclock -w) &> /dev/null' ,
         user    =>  'root' ,
         hour    =>  '*/12' ,
     }
}
 
class software {
     package { [ "vim-enhanced" ,
                "openssh-clients" ,
                "ntpdate" ,
                "man" ,
                "lrzsz"  ]:
                ensure => installed,
    }
}


五、因为大部分机器都是内网的,为了避免一些问题的产生,我喜欢把防火墙配置规则清空,然后执行关闭,设置开机不自起。

1、这次我们创建一个类资源

# cat /etc/puppet/manifests/site.pp

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
node default {
    include ipv6
    include ntpdate
    include software
    include firewall
}
 
class ipv6 {
    file  { '/etc/modprobe.d/ipv6.conf' :
           source  =>  "puppet:///files/ipv6.conf" ,
     }
}
 
class ntpdate {
    cron  { 'ntpdate' :
         ensure  => present,
         command  =>  '(/usr/sbin/ntpdate time.nist.gov && /sbin/hwclock -w) &> /dev/null' ,
         user    =>  'root' ,
         hour    =>  '*/12' ,
     }
}
 
class software {
     package { [ "vim-enhanced" ,
                "openssh-clients" ,
                "ntpdate" ,
                "man" ,
                "lrzsz"  ]:
                ensure => installed,
    }
}
 
class firewall {
     exec  { 'iptables' :
            command  =>  '/sbin/iptables-save > /etc/sysconfig/iptables' ,
            onlyif  =>  "/etc/init.d/iptables status" ,
     }
     service { 'iptables' :
            ensure  => stopped,
            enable   =>  false ,
            before => Exec[ 'iptables' ],
     }
}


六、每次配置都是Agent去向Master同步,我想实现修改某一配置后主动推送到Agent端,这就需要用到puppet kick

     puppet客户端默认每30分钟跟服务器通讯一次,但是有时,我们希望服务端能给客户端紧急推送一些任务,于是就有了puppet kick    

     puppet kick是一个远程管理控制工具,通过它可以主动连接agent的进程,并要求这些被连接的机器主动更新配置。

     通过puppet kick命令主动更新Agent状态前,需要确定Agent是否已经监听了TCP 8139端口,可以修改/etc/puppet/puppet.conf文件,追加如下内容或设置--listen参数。

# cat /etc/puppet/puppet.conf

1
2
[agent]
listen =  true

在Agent端编辑或新建文件/etc/puppet/namespaceauth.conf,包含下面内容

1
2
[puppetrunner] 
allow *.comratings.com

在Agent端编辑文件auth.conf

1
2
3
4
5
6
7
8
9
path  /run 
method save 
allow monitor.comratings.com    
# puppet master
  
# this one is not stricly necessary, but it has the merit 
# to show the default policy which is deny everything else 
path / 
auth any

推送方法,在服务端运行命令

puppet kick -p 10 --host 客户端 

或 

puppetrun -p 10 --host 客户端 

# puppet kick -p 10 --host shabi.comratings.com

1
2
3
4
5
Triggering shabi.comratings.com
Getting status
status is success
shabi.comratings.com finished with  exit  code 0
Finished


七、通过ERB模板配置Apache虚拟主机

1、在Master上创建配置文件和目录

# mkdir -p /etc/puppet/modules/httpd/{manifests,files,templates}

2、编辑init.pp文件

init.pp文件是模块的入库文件

# cat /etc/puppet/modules/httpd/manifests/init.pp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class apache::parameter{
     $listenaddress =  "${ipaddress}"
     $server_admin  =  "admin@comratings.com"
     $server_name   =  "www.comratings.com"
     $document_root =  "/var/www/html/puppet"
}
class httpd inherits apache::parameter{
    package { "httpd" :
       ensure => installed,
    } ->
    file  { "/etc/httpd/conf/httpd.conf" :
       mode    => '777' ,
       content => template( 'httpd/httpd.conf.vhost.erb' ),
       notify  => Service[ 'httpd' ],
    }
    service {  "httpd" :
        ensure     => running,
        hasrestart =>  true ,
        hasstatus  =>  true ,
    }
}

  apache::parameter类存放了虚拟主机的变量值。

  ->代表资源先后顺序,先安装好httpd之后我们才修改配置文件。

  ~>代表资源之间的通知,相当于上面的notify。

  template用法如下:

    template('ModuleName/TemplateName'),中间一些目录可以省去,这里就省去了template目录。

3、准备虚拟主机模板

# cat /etc/puppet/modules/httpd/templates/httpd.conf.vhost.erb

由于内容比较多,我们只截取了虚拟主机的配置部分

1
2
3
4
5
<VirtualHost <%= @listenaddress%>:80>
     ServerAdmin <%= @server_admin %>
     DocumentRoot  "<%= @document_root %>"
     ServerName <%= @server_name %>
< /VirtualHost >

修改好httpd.conf.vhost.erb文件后,就可以通过ruby提供的ERB工具来确认文件的语法是否正确。

# erb -P -x -T '-' httpd.conf.vhost.erb |ruby -c

Syntax OK

4、调用模板

# cat /etc/puppet/manifests/site.pp

1
2
3
node  'shabi.comratings.com'  {
     include httpd
}

当这个节点访问Master的时候,会自动加载httpd模块。


八、我只想修改某一台设备的root密码

1、首先生成密码,密码是md5加密的

# openssl passwd -1 -salt `openssl rand -hex 4`

1
2
Password: 
$1$b992de1e$MOk9NWbErVcazA /BBD .ea/

2、编辑.pp文件

# cat /etc/puppet/manifests/site.pp

1
2
3
4
5
6
7
8
9
node  "shabi.comratings.com"  {
    include root_passwd
}
 
class root_passwd {
     user { 'root' :
           password =>  '$1$b992de1e$MOk9NWbErVcazA/BBD.ea/' ,
    }
}

根据node节点,我们只修改了主机名为shabi.comratings.com的这台主机的密码。





     本文转自 wzlinux 51CTO博客,原文链接:http://blog.51cto.com/wzlinux/1711718,如需转载请自行联系原作者



相关文章
|
网络安全 开发工具
|
关系型数据库 Shell
puppet案例
实例一、文件分发描述:通过puppet服务端可以向被管理机(客户端)上推送文件,方法是使用file类型的source属性第一步:#vi /etc/puppet/fileserver.conf  1    [files]2    path /opt/              # 设置文件推送到目录3    allow 192.
690 0
|
6月前
|
运维 Linux Apache
Puppet 作为一款强大的自动化运维工具,被广泛应用于配置管理领域。通过定义资源的状态和关系,Puppet 能够确保系统始终处于期望的配置状态。
Puppet 作为一款强大的自动化运维工具,被广泛应用于配置管理领域。通过定义资源的状态和关系,Puppet 能够确保系统始终处于期望的配置状态。
215 3
|
安全 Linux 网络协议
puppet yum模块、配置仓储、mount模块
转载:http://blog.51cto.com/ywzhou/1577335 作用:自动为客户端配置YUM源,为使用yum安装软件包提供便捷。 1、服务端配置yum模块 (1)模块清单 [root@puppet ~]# tree /etc/puppe...
1165 0