功能实现:
1.使用SaltStack部署apache和php,
2.使用salt管理httpd.conf配置文件配置访问info.php使用账户密码
3.在salt里面增加对conf.d目录进行配置管理
4.如何使用salt在追加文件内容
5.学会如何使用 watch require unless
项目环境
系统环境 :CentOS6.8 两台
主控端
IP:192.168.139.130
hostname:centos6-1 (salt-master和salt-minion)
被控端
IP:192.168.139.131
hostname:centos6-3 (salt-minion)
实现步骤
1.修改master的配置文件,指定base环境路径,base环境是必须指定的
[root@centos6-1 base]# grep -9 ^file_roots /etc/salt/master |grep -v ^#
file_roots:
base:
- /srv/salt/base
dev:
- /srv/salt/dev
test:
- /srv/salt/test
prod:
- /srv/salt/prod
2.创建目录
[root@centos6-1 salt]# mkdir -p /srv/salt/{base,dev,test,prod}
[root@centos6-1 base]# tree /srv/salt/
/srv/salt/
├── base
├── dev
├── prod
└── test
3.重启master
[root@centos6-1 salt]# service salt-master restart
4.在base目录下面创建一个web目录用于存放web相关的sls文件
[root@centos6-1 base]# mkdir -p web
5.cd到bash/web目录里面创建apache.sls文件
[root@centos6-1 base]# cd web/
[root@centos6-1 web]# cat apache.sls
apache-install: #id 名字自己取 需要形象一点, 一个id下面一个状态只能出现一次
pkg.installed: #pkg 是状态模块,installed 是模块里面的方法
- name: httpd #方法里面的参数
apache-service:
service.running:
- name: httpd
- enable: True #设置开机自动启动
#yaml里面格式有严格的要求,注释用#号,不能有table,- 两边需要空格,缩进用2个空格层级关系后面要加分号
6.执行状态模块部署服务
state.sls模块可以指定sls文件执行,“env=”可以指定你的执行环境默认是base,web.apache 指web文件夹下的apache文件
[root@centos6-1 base]# salt "centos6-3" state.sls web.apache
centos6-3:
----------
ID: apache-install
Function: pkg.installed
Name: httpd
Result: True
Comment: Package httpd is already installed.
Started: 14:58:09.228934
Duration: 633.681 ms
Changes:
----------
ID: apache-service
Function: service.running
Name: httpd
Result: True
Comment: Service httpd is already enabled, and is running
Started: 14:58:09.863302
Duration: 310.567 ms
Changes:
----------
httpd:
True
Summary
------------
Succeeded: 2 (changed=1)
Failed: 0
------------
Total states run: 2
#此时node2 上面已经部署好了apache
7.高级状态的使用 需要在master配置文件里面打开 state_top: top.sls并重启master
[root@centos6-1 base]# grep -n ^state_top /etc/salt/master
329:state_top: top.sls
[root@centos6-1 salt]# service salt-master restart
8.在bese环境目录下面添加top.sls
[root@centos6-1 base]# more top.sls
base:
'centos6-3':
- web.apache
[root@centos6-1 base]# pwd
/srv/salt/base
9.执行高级模块方法,高级方法到 base下面找top.sls 文件编排告诉每个minion需要干什么,一般生产环境用高级状态多些
[root@centos6-1 base]# salt "*" state.highstate
centos6-3:
----------
ID: apache-install
Function: pkg.installed
Name: httpd
Result: True
Comment: Package httpd is already installed.
Started: 15:23:09.171596
Duration: 721.901 ms
Changes:
----------
ID: apache-service
Function: service.running
Name: httpd
Result: True
Comment: Service httpd is already enabled, and is in the desired state
Started: 15:23:09.894209
Duration: 221.615 ms
Changes:
Summary
------------
Succeeded: 2
Failed: 0
------------
Total states run: 2
10.上面我们使用了2个状态模块pkg和service,下面我们使用file文件配置模块
在base/web目录下面添加一个lamp.sls,一般在添加里面的内容之前需要在外面找一台服务器进行测试拿到准确的包信息后再进行配置
[root@centos6-1 web]# cp /etc/httpd/conf/httpd.conf /srv/salt/base/web/files/
[root@centos6-1 web]# cp /etc/php.ini /srv/salt/base/web/files/
执行状态模块部署服务
[root@centos6-1 web]# salt "*" state.sls web.lamp
centos6-3:
----------
ID: lamp-install
Function: pkg.installed
Result: True
Comment: All specified packages are already installed.
Started: 15:43:58.566172
Duration: 611.409 ms
Changes:
----------
ID: apache-config
Function: file.managed
Name: /etc/httpd/conf/httpd.conf
Result: True
Comment: File /etc/httpd/conf/httpd.conf is in the correct state
Started: 15:43:59.180091
Duration: 4.063 ms
Changes:
----------
ID: php-config
Function: file.managed
Name: /etc/php.ini
Result: True
Comment: File /etc/php.ini is in the correct state
Started: 15:43:59.184248
Duration: 3.803 ms
Changes:
----------
ID: lamp-service
Function: service.running
Name: httpd
Result: True
Comment: Service httpd is already enabled, and is in the desired state
Started: 15:43:59.188496
Duration: 208.1 ms
Changes:
Summary
------------
Succeeded: 4
Failed: 0
------------
Total states run: 4
11. 使用file模块下面的recurse方法进行apache的conf.d目录管理配置如下
apache-conf:
file.recurse:
- name: /etc/httpd/conf.d
- source: salt://web/files/apache-conf.d
12.创建salt源目录,并拷贝数据导源文件目录,数据文件来源根据自己业务的实际情况
[root@centos6-1 ~]# mkdir /srv/salt/base/web/files/apache-conf.d
[root@centos6-1 ~]# cd /srv/salt/base/web/files/apache-conf.d/
[root@centos6-1 apache-conf.d]# cp -a /etc/httpd/conf.d/* .
[root@centos6-1 apache-conf.d]# ls
autoindex.conf php.conf README userdir.conf welcome.conf
13.验证目录管理是否生效
可以先使用test=True 只做测试
,不会在minion节点上面真正执行,确认无问题后再让minion去执行
[root@centos6-1 files]# salt "centos6-3" state.highstate test=True
14.使用watch在apache配置文件发送变化时,重新加载apache配置
增加下面的三行#部分
more lamp.sls
lamp-install:
pkg.installed:
- pkgs:
- httpd
- php
- php-pdo
- php-mysql
apache-config:
file.managed:
- name: /etc/httpd/conf/httpd.conf
- source: salt://web/files/httpd.conf
- user: root
- group: root
- mode: 644
php-config:
file.managed:
- name: /etc/php.ini
- source: salt://web/files/php.ini
- user: root
- group: root
- mode: 644
lamp-service:
service.running:
- name: httpd
- enable: True
- reload: True #如果不加reload 默认会重启服务
- watch: #增加
- file: apache-config #监控上面的apache-config ID 所以说 一个ID在一个状态只能出现一次
apache-conf:
file.recurse:
- name: /etc/httpd/conf.d
- source: salt://web/files/apache-conf.d
另外一种watc_in写法,我们只需要掌握一种就可以
...
lamp-service:
service.running:
- name: httpd
- enable: True
- reload: True
- watch:
- file: apache-config
apache-conf:
file.recurse:
- name: /etc/httpd/conf.d
- source: salt://web/files/apache-conf.d
- watch_in:
- service: lamp-service
...
15.修改一下配置文件进行验证成功
16. 使用require可以让各ID之间产生依赖关系,避免无效执行
比如执行apache-config ID之前要确保 lamp-install ID已经成功的完成了 添加下面#部分:
lamp-install:
pkg.installed:
- pkgs:
- httpd
- php
- php-pdo
- php-mysql
apache-config:
file.managed:
- name: /etc/httpd/conf/httpd.conf
- source: salt://web/files/httpd.conf
- user: root
- group: root
- mode: 644
- require:#
- pkg: lamp-install#
17.测试apache php环境工作是否正常,在2个节点上的apache工作目录下创建phpinfo文件
[root@centos6-3 conf.d]# cd /var/www/html/
[root@centos6-3 html]# mkdir admin
[root@centos6-3 html]# cd admin/
[root@centos6-3 admin]# vi info.php
[root@centos6-3 admin]# cat info.php
<?php
phpinfo()
?>
尝试浏览器打开。