杂记:
删除记录
EXAMPLE:
# Prune records upto 1 month old:
rake reports:prune upto=1 unit=mon
rake -f /usr/share/puppet-dashboard/Rakefile RAILS_ENV=production reports:prune upto=1 unit=min
(Deleting reports before 2014-04-30 16:58 CST… 删除1分钟前的记录)
分析脚本可以通过
hiera 解决客户端 certname的问题
假如我想用 puppet agent –test –server=puppet.test.com –certname=test10.test.com 里面的certname直接去改puppet.conf文件中的certname的配置,我引入了hiera ,其实hiera 主要是节点定义的问题,解决庞大的node.pp的问题。
首先在要理解客户端带来的系统参数
http://www.stopro.info/p186/ (参考url)
Agent 客户机端:$environment, $clientcert, $clientversion。
根据客户端提供的clientcert 去更改
修改 hiera.yaml
—
:backends:
– yaml
:hierarchy:
# – defaults
# – “%{clientcert}”
– common
– “%{environment}”
– “%{osfamily}”
– “%{hostname}”
# – global
:yaml:
:datadir: /etc/puppet/hiera
在 /etc/puppet/hiera 下创建 common.yaml
—
puppetserver:
– ‘puppet.test.com’
certname:
– “%{clientcert}”
这句话的意思就是 puppetserver 是 puppet.test.com ,certname是各自的clientcert。测试下
[root@puppet hiera]# hiera certname puppetserver clientcert=test7.test.com
[“test7.test.com”]
[root@puppet hiera]# hiera puppetserver clientcert=test7.test.com
[“puppet.test.com”]
可以发现变量生效了
然后修改default的node
node default{
$certname = hiera(‘certname’)
$puppetserver = hiera(‘puppetserver’)
include puppet
}
这个就保证了 新的添加机器puppet配置文件的更改。
PS 本来也想更改hostname,但是hostname要从clientcert截取,不好处理,而且有时需要hostname 与 certname 未必一致,所以去掉了hostname的修改,只能指定参数,在尝试通过foreman去修改参数,指定hostname。
参考url:http://dreamfire.blog.51cto.com/418026/1399014
foreman 的 mysql存储
之前看网上文档有很多误导了。以为foreman很渣。后来发现都是自己的问题。
建议去官网看:http://theforeman.org/manuals/1.1/index.html#3.5.3DatabaseSetup
里面说了
Edit your config/database.yml and modify:
production:
adapter: mysql
database: puppet
username: puppet
password: password
host: localhost
socket: "/var/run/mysqld/mysqld.sock"
If you use foreman 1.0 with foreman-mysql2 use ‘adapter: mysql2’ instead
其中要求你装mysql2 所以配置中 adapter: mysql 换成 adapter: mysql2
首先你要安装 yum -y install foreman-mysql*
安装完之后 访问页面 会有 gem install activerecord-mysql2-adapter 的提示。
我通过gem install activerecord-mysql2-adapter 安装了,并且通说gem list 也看到安装了,一直还是报gem install activerecord-mysql2-adapter 的错误,之前一直没想通,后来发现 我的环境是 ruby193 而不是ruby 。我尝试 scl enable ruby193 ‘gem install rails’ 然后通过 scl enable ruby193 ‘gem list’ 看到已安装。 重新登录页面发现没问题了。
PS: 网上有跟多用 rake db:migrate 初始化的 ,我的rake好像有问题,一直没初始化,但是通过页面访问可以直接初始化,很智能。
感悟:
1:一定要看环境,如果程序还是提示同样的错误,可能你的环境不对。
2:要多看官网文档,网上的文档可能有时会比较老,从而误导你。
PS 切换ruby
scl enable ruby193 bash
Passenger报错的问题
在比较高版本Passenger的时候,RailsAutoDetect on 是不行的,会报 RailsAutoDetect 没有此命令。
Invalid command ‘RailsAutoDetect’, perhaps misspelled or defined by a module not included in the server configuration
换成 PassengerEnabled On 即可,一模一样。只是改了名字。
puppet 报错 Warning: Error 400 on SERVER: Could not retrieve facts for test16.test.com: Could not autoload puppet/indirector/facts/active_record: no such file to load — active_record
目前看来报错是由于配置文件 storeconfigs 造成了的,关闭即可。
有的是是说由于版本的问题 参考urlhttp://www.cnblogs.com/yuxc/archive/2012/09/08/2676648.html。暂时还没测试,先关闭了。后来测试降版本是可以的。
puppet hostname的问题
我在测试中发现,定义变量hostname,如果没赋值,会自动赋值客户端的hostname,很奇怪。
puppet 表达式匹配的问题
对参数进行字符串处理,解决hostname通过certname获取的问题
使用正则表达式与替换
puppet提供了regsubst函数来轻松处理文本,在字符串内搜索与替换,
或者从字符串提取匹配模式,例如,通常我们需要处理从facter获得的变量,
或者从外部程序获得的facter.
在下面的例子,我们看到如何使用regsubst提取一个IP地址的前三个8位字节
(网络的一部分,假设是一个C 类地址).
1.添加以下内容到你puppet代码里
$class_c = regsubst($ipaddress, "(.*)..*", "1.0")
notify { $ipaddress: }
notify { $class_c: }
2.运行puppet
notice: 10.0.2.15 notice: 10.0.2.0
参考他的 我写的是
$hostname=regsubst($certname,”.test.com”,””)
将.test.com屏蔽掉测试是可以的。
后来hostname发现是系统变量,果然得重新定义参数,去定义特殊的hostname,我这边定义的是hostname_t
if $hostname_t {
$hostname=$hostname_t
file { “/etc/sysconfig/network”:
content => template(“hostname/network”),
mode => 644,
}
exec { “hostname shell”:
command => “/bin/hostname $hostname”,
path => “/usr/bin:/usr/sbin:/bin:/sbin”,
logoutput => “true”,
subscribe => File[“/etc/sysconfig/network”],
refreshonly => true,
}
}
else {
$hostname=regsubst($certname,”.test.com”,””)
file { “/etc/sysconfig/network”:
content => template(“hostname/network”),
mode => 644,
}
exec { “hostname shell”:
command => “/bin/hostname $hostname”,
path => “/usr/bin:/usr/sbin:/bin:/sbin”,
logoutput => “true”,
subscribe => File[“/etc/sysconfig/network”],
refreshonly => true,
}
}
参考url:http://www.zhdba.com/mysqlops/2011/12/30/puppet-regular-substitutions/
http://docs.puppetlabs.com/references/stable/function.html (官方文档)
关于environment的一些BUG
本来我要做目录同步,所以用到了fileserverconfig,然后我想根据不同environment去找不同fileserverconfig。网上看到了文档。
[master]
certname = puppetmaster.kisspuppet.com
environments = kissdev,kisstmq,kissprd #添加三个环境的标签名称
[kissdev]
modulepath = $confdir/environments/kissdev/environment/modules:$confdir/environments/kissdev/application/modules #设置环境的搜索路径
manifest = $confdir/environments/kissdev/manifests/site.pp #设置环境的site.pp文件位置
fileserverconfig = /etc/puppet/fileserver.conf.kissdev #设置环境的fileserver
[kissmq]
modulepath = $confdir/environments/kissmq/environment/modules:$confdir/environments/kisstest/application/modules
manifest = $confdir/environments/kisstest/manifests/site.pp
fileserverconfig = /etc/puppet/fileserver.conf.kisstest
[kissprd]
modulepath = $confdir/environments/kissprd/environment/modules:$confdir/environments/kissprd/application/modules
manifest = $confdir/environments/kissprd/manifests/site.pp
fileserverconfig = /etc/puppet/fileserver.conf.kissprd
但是我在测试的时候,发现只有添加在master里面才可以。其他的不生效。一直很纠结。最后在官网文档找到了解答。
http://docs.puppetlabs.com/guides/environment.html#caveats
Caveats
Before you start, be aware that environments have some limitations, most of which are known bugs or vagaries of implementation rather than design choices.
- Puppet will only read the
modulepath,manifest,manifestdir, andtemplatedirsettings from environment config blocks; other settings in any of these blocks will be ignored in favor of settings in the[master]or[main]blocks. (Issue 7497) - File serving only works well with environments if you’re only serving files from modules; if you’ve set up custom mount points in
fileserver.conf, they won’t work in your custom environments. (Though hopefully you’re only serving files from modules anyway.) - Prior to Puppet 3, environments set by external node classifiers were not authoritative. If you are using Puppet 2.7 or earlier, you must set the environment in the agent node’s config file.
- Serving custom ruby code via pluginsync, including custom types, providers, facts and functions is not currently functional on a per-environment basis. You can still use custom ruby code to to extend puppet, but it should be identical for all environments. Non-identical custom code across modules can result in unpredictable behavior. See the following open issues for more information:
这也说明了为什么为什么templatedir 生效,而fileserverconfig不生效。好像file只能通过模块去获取,无法通过定义fileserverconfig去指定获取。第二条写的很清楚。
后来我通过environment变量去不同目录去拿,很纠结不知道为什么有人在老版本可以,而我的依然不行。
我后来变通的实现方法:
file { “/home/service/www.shanliao.com/config/”:
ensure => directory,
source => “puppet:///config/$environment/php/template/config/”,
mode => 644,
recurse => true,
purge => true,
force => true,
}