一、saltstack搭建
配置yum源:
Master 在/var/www/html/添加salt
Minion:server2和server3
完善yum源:
[rhel-source]
name=Red Hat Enterprise Linux $releasever - $basearch - Source
baseurl=http://172.25.7.250/source6.5
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
[salt]
name=saltstack
baseurl=http://172.25.7.250/salt
enabled=1
gpgcheck=0
server1部署master,server2部署minion
[root@server1 ~]# yum install salt-master -y
[root@server1 ~]# /etc/init.d/salt-master start
[root@server2 salt]# yum install salt-minion -y
[root@server2 salt]# vim minion
master: 172.25.7.1
[root@server2 salt]# /etc/init.d/salt-minion start
[root@server1 ~]# salt-key -L
# server2是被拒绝的
[root@server1 ~]# salt-key -A
# 接受所有端口
测试,执行salt命令:
[root@server1 ~]# salt server2 test.ping
server2:
True
[root@server1 ~]# salt server2 cmd.run hostname
server2:
server2
[root@server1 ~]# salt server2 cmd.run 'df -h'
server2:
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root 19G 967M 17G 6% /
tmpfs 499M 16K 499M 1% /dev/shm
/dev/vda1 485M 33M 427M 8% /boot
二、对httpd进行安装和配置
在此之前我们先了解以下,yaml语言
YAML官方中文文档
默认的SLS文件的renderer是YAMLrenderer。YAML是一个有很多强大特性的标记性语言。Salt使用了一个YAML的小型子集,映射非常常用的数据结构,像列表和字典。YAMLrenderer的工作是将YAML数据格式的结构编译成为Python数据结构给Salt使用。
[root@server1 salt]# vim master
# 设置base目录
[root@server1 salt]# /etc/init.d/salt-master restart
[root@server1~]# mkdir /srv/salt
[root@server1 salt]# cd /srv/salt/
[root@server1 salt]# mkdir httpd
[root@server1 httpd]# vim install.sls
apache-install:
pkg.installed:
- pkgs:
- httpd
- php
[root@server1 httpd]# salt server2 state.sls httpd.install
# 进行推送
在server2查看,安装成功
[root@server2 salt]# rpm -q httpd
httpd-2.2.15-29.el6_4.x86_64
[root@server2 salt]# rpm -q php
php-5.3.3-26.el6.x86_64
再度完善.sls文件
[root@server1 httpd]# vim install.sls
apache-install:
pkg.installed:
- pkgs:
- httpd
- php
file.managed:
- name: /etc/httpd/conf/httpd.conf
- source: salt://httpd/files/httpd.conf
- mode: 644
- user: root
service.running:
- name: httpd
- enable: True
- reload: True
- watch:
- file: apache-install
[root@server1 httpd]# mkdir files
[root@server2salt]#scp /etc/httpd/conf/httpd.conf server1:/srv/salt/httpd/files
# 修改server1的httpd.conf
Listen 8080
对两边的文件进行比对
[root@server2 salt]# md5sum /etc/httpd/conf/httpd.conf
27a5c8d9e75351b08b8ca1171e8a0bbd /etc/httpd/conf/httpd.conf
[root@server1 files]# md5sum httpd.conf
b7ca7a0e786418ba7b5ad84efac70265 httpd.conf
# 结果不同,才会去更改server2的端口
[root@server1 files]# salt server2 state.sls httpd.install
# 进行推送
在server2查看
支持多种写法:
[root@server1 httpd]# vim install.sls
httpd:
pkg.installed:
php:
pkg.installed:
apache:
service.running:
- name: httpd
- enable: True
- reload: True
- watch:
- file: /etc/httpd/conf/httpd.conf
/etc/httpd/conf/httpd.conf
file.managed:
- source: salt://httpd/files/httpd.conf
- mode: 644
- user: root
[root@server1 httpd]# vim install.sls
apache-install:
pkg.installed:
- pkgs:
- httpd
- php
service.running:
- name: httpd
- enable: True
- reload: True
- watch:
- file: /etc/httpd/conf/httpd.conf
/etc/httpd/conf/httpd.conf:
file.managed:
- source: salt://httpd/files/httpd.conf
- mode: 644
- user: root