puppet(單機模式)-基於模塊方式實現redis主從
實現步驟:
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
測試查看:-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
puppet(單機模式)-基於模塊方式實現redis主從