@cdmonkey
2016-07-08T09:17:01.000000Z
字数 5869
阅读 1560
Saltstack
首先要创建状态文件:
http://docs.saltstack.cn/ref/states/all/salt.states.file.html#salt.states.file.managed
[root@test-ngx ~]# mkdir /etc/salt/states/zabbix[root@test-ngx ~]# cd /etc/salt/states/zabbix[root@test-ngx zabbix]# vim zabbix_agent.sls
[root@salt-master ~]# cd /etc/salt/states/init/# Create sls:[root@salt-master init]# vim zabbix_agent.sls/etc/resolv.conf:file.managed:- source: salt://zabbix/files/resolv.conf- user: root- group: root- mode: 755zabbix_agent: # ID declarationpkg.installed:- name: zabbix22-agent # RPM Package namefile.managed:- name: /etc/zabbix_agentd.conf- source: salt://zabbix/files/zabbix_agentd.conf- user: root- mode: 644- template: jinja # Define the template.- defaults: # Define the variables used in the template.Zabbix_Server: {{ pillar['zabbix-agent']['Zabbix_Server'] }}- require:- pkg: zabbix-agentservice.running:- name: zabbix-agentd- enable: True # 开机自动启动。- reload: True # 如果服务进程不支持重载,那么去掉这行即可,默认就会重新启动服务。- watch: # 被监控的内容,一旦产生变动,服务就会重启或重载。- file: zabbix_agent
因为需使用“Pillar”来获取数据,那么需要将你要管理的源文件(source)变成一个模板,其操作非常的简单:
[root@salt-master ~]# vim /etc/salt/states/init/files/zabbix_agentd.confServer=127.0.0.1 --> Server={{Zabbix_Server}}# 即将需要获取的数据用双大括号括起来,中间是定义的变量名称。# 如此进行修改后,该配置文件就成为了一个模板。
[root@salt-master ~]# vim /etc/salt/pillar/init/zabbix_agent.slszabbix-agent:Zabbix_Server: 172.16.1.21#Pillar的状态文件主要是设置自定义的变量。------------------#不要忘记修改Pillar的入口文件(指定的目标使用指定的状态文件):[root@salt-master ~]# vim /etc/salt/pillar/top.slsbase:'*':- init.rsyslog- init.zabbix_agent
最后需要修改入口文件top.sls,指定目标节点及其状态文件:
base:'node(2|3).cdmonkey.com':- match: pcre- init.pkg- init.limit- init.zabbix_agent
特别需要注意:“Pliiar”与状态管理的两个入口文件
top.sls在写法上完全相同,但作用范围不同。
整个过程如图所示:

实际的生产场景中,对于从来没有执行过的状态,一定要先测试一下:
# 仅仅是进行测试,而并不真正的去同步状态:[root@salt-master ~]# salt '*' state.highstate test=True# 仅显示一个节点返回的信息以作实例:node3.cdmonkey.com:----------ID: init.pkgFunction: pkg.installed...----------ID: limit-confFunction: file.managedName: /etc/security/limits.confResult: TrueComment: The file /etc/security/limits.conf is in the correct stateStarted: 18:47:27.315907 # 显示该文件处于正确的状态,也就是说处于与控制端一致的状态。Duration: 5.709 msChanges:----------ID: zabbix_agentFunction: pkg.installedName: zabbix22-agentResult: NoneComment: The following packages are set to be installed/updated: zabbix22-agentStarted: 18:47:27.321777 # 显示说明该软件将会被安装。Duration: 4428.429 msChanges:----------ID: zabbix_agentFunction: file.managedName: /etc/zabbix_agentd.confResult: NoneComment: The file /etc/zabbix_agentd.conf is set to be changedStarted: 18:47:31.754157 # 显示说明该文件将会被修改。Duration: 4.279 msChanges:----------newfile:/etc/zabbix_agentd.conf----------ID: zabbix_agentFunction: service.runningName: zabbix_agentdResult: FalseComment: The named service zabbix_agentd is not availableStarted: 18:47:31.761976 # 由于根本没有安装相应的服务,所以这里出现服务不存在很正常。Duration: 16.192 msChanges:Summary------------Succeeded: 5 (unchanged=2, changed=1)Failed: 1------------Total states run: 6
测试完成后就可以进行状态同步了:
[root@salt-master ~]# salt '*' state.highstate# 以下是服务正常被启动后的输出内容:ID: zabbix_agentFunction: service.runningName: zabbix-agentdResult: TrueComment: Service zabbix-agentd has been enabled, and is runningStarted: 19:54:24.276550Duration: 345.981 msChanges:----------zabbix-agentd:True
下面是“Zabbix”客户端的主配文件被修改后的输出内容:
...ID: zabbix_agentFunction: file.managedName: /etc/zabbix_agentd.confResult: TrueComment: File /etc/zabbix_agentd.conf updatedStarted: 16:17:30.059533Duration: 19.03 msChanges:----------diff:---+++@@ -78,7 +78,7 @@# Default:# Server=-Server=172.16.1.21+Server=172.16.1.1 #被控端配置文件的内容进行了相应的修改。### Option: ListenPort----------ID: zabbix_agentFunction: service.runningName: zabbix-agentdResult: TrueComment: Service restarted #由于配置文件被修改,所以服务被重启。Started: 16:17:30.170500Duration: 300.727 msChanges:----------zabbix-agentd:True...
上面的这种“Pillar”的写法属于有层级关系的,当然也可以直接写成键值对的形式,而没有上层的名称。
- template: jinja- defaults:Zabbix_Server: {{ pillar['Zabbix_Server'] }}-----------------#相对应的,Pillar状态文件中只包含键值对:[root@salt-master ~]# cat /etc/salt/pillar/init/zabbix_agent.slsZabbix_Server: 172.16.1.21
还有更为简单的写法,那就是不使用“Pillar”,而是将模板中用到的变量直接在pillar file中进行定义并赋值:
- template: jinja- defaults:Zabbix_Server: 172.16.1.21
创建状态文件:
[root@salt-master ~]# vim /etc/salt/states/php/php_fastcgi.slsinclude: #将现有的状态文件加载进来。- init.pkgphp-install:file.managed:- name: /usr/local/src/php-5.5.13.tar.gz- source: salt://php/files/php-5.5.13.tar.gz- user: root- group: root- mode: 755cmd.run:- name: cd /usr/local/src && tar zxf php-5.5.13.tar.gz && cd php-5.5.13 && ./configure --prefix=/usr/local/php-fastcgi --with-mysql --with-jpeg-dir --with-png-dir --with-zlib --enable-xml --with-libxml-dir --with-curl --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --enable-mbregex --with-openssl --enable-mbstring --with-gd --enable-gd-native-ttf --enable-sockets --with-xmlrpc --enable-zip --enable-soap --disable-debug --enable-opcache --enable-zip --with-config-file-path=/usr/local/php-fastcgi/etc --enable-fpm --with-fpm-user=www --with-fpm-group=www && make && make install- unless: test -d /usr/local/php-fastcgi #条件判断:如果该目录存在(证明安装过)则不再进行安装。require: #在这里指明如果要进行该步操作,依赖的是什么。- file: php-install #依赖于指定的文件必须存在。- pkg.installed: php-installpdo-plugin:cmd.run:- name: cd /usr/local/src/php-5.5.13/ext/pdo_mysql/ && /usr/local/php-fastcgi/bin/- unless: test -f /usr/local/php-fastcgi/lib/php/extensions/no-debug-non-zts-require:- cmd: php-install
创建目录并上传源码包:
[root@salt-master ~]# mkdir -p /etc/salt/states/php/files#上传源码包到该目录。[root@salt-master ~]# ll /etc/salt/states/php/files/total 16752-rw-r--r-- 1 root root 17151674 Apr 25 18:16 php-5.5.13.tar.gz
[root@salt-master ~]# vim /etc/salt/states/init/pkg.slsphp-pkg: #不能够直接写模块和方法,要不就是像这样声明一个名称,那不就是写上“name”。pkg.installed:- names:- gcc- gcc-c++- glibc- make- autoconf- libjpeg-turbo- libjpeg-turbo-devel- libpng- libpng-devel- freetype- freetype-devel- libxml2- libxml2-devel- zlib- zlib-devel- libcurl- libcurl-devel- openssl- openssl-devel
以上内容的第二种写法:
gcc: #也可以这样写,首先是软件包名,下面是模块和方法。如果是有很多包,上面的写法更合适。pkg.installed...
修改入口文件:
[root@salt-master ~]# vim /etc/salt/states/top.slsbase:'node(2|3).cdmonkey.com':- match: pcre- init.pkg- init.limit- init.zabbix_agent- php.php_fastcgi