说明:puppet模块存放位置有固定要求,不过为了实验方便,先建一个临时目录来存放,当配置完成后移动到指定目录。

实现步骤:

1、实现如下目录结构

[root@manifests modules]# tree ~/modules

~/modules

└── redis

     ├── files

     │    └── redis-master.conf

     ├── lib

     ├── manifests

     │    ├── init.pp

     │    ├── master.pp

     │    └── slave.pp

     ├── spec

     ├── templates

     │    └── redis-slave.conf.erb

     └── tests

mkdir -pv modules/redis/{manifests,files,templates,tests,lib,spec}

files以及templates文件夹下的文件内容大致同默认的配置文件,只是修改模板文件的slaveof指向master主机


2、编辑所需的清单文件

vim manifests/init.pp

   class redis {

        package {'redis':

                ensure => installed,

        }

        service {'redis':

                ensure => running,

                enable => true,

                hasrestart => true,

                hasstatus => true,

        }

}

 

vim manifests/master.pp

    class redis::master inherits redis {

        file{'/etc/redis.conf':

                ensure => file,

                source => 'puppet:///modules/redis/redis-master.conf',

                owner => 'redis',

                group => 'root',

                mode => 0640,

        }

        Package['redis'] -> File['/etc/redis.conf'] ~>Service['redis']

}

 

vim manifests/slave.pp

    class redis::slave($master_ip,$master_port='6379') inherits redis {

        file{'/etc/redis.conf':

                ensure => file,

                content => template('redis/redis-slave.conf.erb'),

                owner => 'redis',

                group => 'root',

                mode => 0640,

        }

        Package['redis'] -> File['/etc/redis.conf'] ~>Service['redis']

}

 

3、将模块移动到指定的位置

cp -r redis/ /etc/puppet/module/

查看模块有哪些

puppet module list

blob.png 

测试查看:-e指明调用的代码

puppet apply -v --noop -e "include redis"

puppet apply -v --noop -e "include redis::master"

puppet apply -v --noop -e "include redis::slave"  由于slave中使用了参数,所以include调用会报错,应该使用下面的命令

puppet apply -v --noop -e "class{'redis::slave': master_ip => '172.16.13.134'}"

如果多个参数,那么逗号隔开即可。也可以编辑一个.pp文件来调用redis::slave类而不使用命令行测试

vim ~/manifests/redis3.pp

   class{'redis::slave':

        master_ip => '172.16.13.134',

   }

执行:puppet apply -v redis3.pp