一、LAMP架构案例
说明:该案例在
prod生产
环境配置1)环境准备,定义
file_roots
环境
[root@salt-master ~]# vim /etc/salt/master
file_roots:
base:
- /srv/salt/base
dev:
- /srv/salt/dev
prod:
- /srv/salt/prod
2)创建对应环境目录
[root@salt-master ~]# mkdir -p /srv/salt/{base,dev,prod}
[root@salt-master ~]# mkdir /srv/salt/prod/{httpd,php,mysql,files}
3)配置文件准备及测试文件准备
[root@salt-master ~]# cp /etc/my.cnf /srv/salt/prod/files/
[root@salt-master ~]# cp /etc/httpd/conf/httpd.conf /srv/salt/prod/files/
[root@salt-master ~]# cp /etc/php.ini /srv/salt/prod/files/
[root@salt-master ~]# echo "<h1>LAMP html</h1>" >>/srv/salt/prod/files/index.html
[root@salt-master ~]# echo "<?php phpinfo(); ?>" >> /srv/salt/prod/files/index.php
4)编写state sls状态文件
#httpd
[root@salt-master ~]# cat /srv/salt/prod/httpd/init.sls
apache-install:
pkg.installed:
- pkgs:
- httpd
- httpd-tools
apache-config:
file.managed:
- name: /etc/httpd/conf/httpd.conf
- source: salt://files/httpd.conf
- user: root
- group: root
- mode: 644
apache-service:
service.running:
- name: httpd
- enable: True
#php
[root@salt-master ~]# cat /srv/salt/prod/php/init.sls
php-install:
pkg.installed:
- pkgs:
- php
- php-mysql
- php-pdo
- php-cli
php-config:
file.managed:
- name: /etc/php.ini
- source: salt://files/php.ini
- user: root
- group: root
- mode: 644
#mysql
[root@salt-master ~]# cat /srv/salt/prod/mysql/init.sls
mariadb-install:
pkg.installed:
- pkgs:
- mariadb-server
- mariadb
mariadb-config:
file.managed:
- name: /etc/my.cnf
- source: salt://files/my.cnf
- user: root
- group: root
- mode: 644
mariadb-service:
service.running:
- name: mariadb
- enable: True
#测试文件
[root@salt-master ~]# cat /srv/salt/prod/testfile.sls
/var/www/html/index.html:
file.managed:
- source: salt://files/index.html
/var/www/html/index.php:
file.managed:
- source: salt://files/index.php
6)topfile文件编写
[root@salt-master ~]# cat /srv/salt/base/top.sls
prod:
'salt-minion*': # All minions with a minion_id that begins with 'salt-minion'
- httpd.init
- php.init
- mysql.init
- testfile
7)部署LAMP整体state文件查看
[root@salt-master ~]# tree /srv/salt/
/srv/salt/
├── base
│ └── top.sls
├── dev
└── prod
├── files
│ ├── httpd.conf
│ ├── index.html
│ ├── index.php
│ ├── my.cnf
│ └── php.ini
├── httpd
│ └── init.sls
├── mysql
│ └── init.sls
├── php
│ └── init.sls
└── testfile.sls
8)执行topfile
[root@salt-master ~]# salt '*' state.highstate
二、States状态依赖
通过上面的
lamp
可以看出已经可以使用state
模块来定义minion
的状态了,但是如果一个主机涉及多个状态,并且状态之间相互关联,在执行顺序上有先后之分,那么必须引用requisites
来进行控制关系说明:
1、require
我依赖某个状态,我依赖谁
2、require_in
我被某个状态依赖,谁依赖我
3、watch
我关注某个状态,当状态发生改变,进行restart
或者reload
操作
4、watch_in
我被某个状态关注
5、include
我引用谁1)修改上面
lamp
状态间依赖关系
#httpd
[root@salt-master ~]# cat /srv/salt/prod/httpd/init.sls
apache-install:
pkg.installed:
- pkgs:
- httpd
- httpd-tools
apache-config:
file.managed:
- name: /etc/httpd/conf/httpd.conf
- source: salt://files/httpd.conf
- user: root
- group: root
- mode: 644
- require:
- pkg: apache-install #表示上面apache-install执行成功,才能执行apache-config
apache-service:
service.running:
- name: httpd
- enable: True
- require:
- file: apache-config
- watch:
- file: apache-config
#php
[root@salt-master ~]# cat /srv/salt/prod/php/init.sls
php-install:
pkg.installed:
- pkgs:
- php
- php-mysql
- php-pdo
- php-cli
- reqiure_in:
- file: php-config
php-config:
file.managed:
- name: /etc/php.ini
- source: salt://files/php.ini
- user: root
- group: root
- mode: 644
#mysql
[root@salt-master ~]# cat /srv/salt/prod/mysql/init.sls
mariadb-install:
pkg.installed:
- pkgs:
- mariadb-server
- mariadb
mariadb-config:
file.managed:
- name: /etc/my.cnf
- source: salt://files/my.cnf
- user: root
- group: root
- mode: 644
- require:
- pkg: mariadb-install
mariadb-service:
service.running:
- name: mariadb
- enable: True
- reload: True
- require:
- file: mariadb-config
- watch:
- file: mariadb-config
2)修改引用关系后include
[root@salt-master ~]# tree /srv/salt/
/srv/salt/
├── base
│ └── top.sls
├── dev
└── prod
├── files
│ ├── httpd.conf
│ ├── index.html
│ ├── index.php
│ ├── my.cnf
│ └── php.ini
├── httpd
│ └── init.sls
├── lamp.sls
├── mysql
│ └── init.sls
├── php
│ └── init.sls
└── testfile.sls
[root@salt-master ~]# cat /srv/salt/prod/lamp.sls
include:
- httpd.init
- php.init
- mysql.init
- testfile
[root@salt-master ~]# cat /srv/salt/base/top.sls
prod:
'salt-minion*':
- lamp
3)编写
SLS
技巧1、按照状态分类,如果单独使用,清晰明了 2、按照服务分类,可以被其它SLS引用
三、Jinja模板使用
配置文件一般灵活多变,比如配置
apache
的IP
地址或者端口PORT
等,则可以动态传值。
Jinja2
模板包含变量和表达式,变量用 {{ … }} 包围,表达式用 {% … %} 包围。变量使用示例:
[root@salt-master ~]# cat /srv/salt/base/var.sls
{% set var= 'hello world!' %}
test_var:
cmd.run:
- name: echo "测试变量 {{ var }}"
[root@salt-master ~]# salt 'salt-minion01' state.sls var
salt-minion01:
----------
ID: test_var
Function: cmd.run
Name: echo "测试变量 hello world!"
Result: True
Comment: Command "echo "测试变量 hello world!"" run
Started: 14:50:58.302424
Duration: 12.358 ms
Changes:
----------
pid:
22510
retcode:
0
stderr:
stdout:
测试变量 hello world!
Summary for salt-minion01
------------
Succeeded: 1 (changed=1)
Failed: 0
------------
Total states run: 1
Total run time: 12.358 ms
jinja2 常用变量
1、字符串类型
{% set var = 'test' %} #定义变量
{{ var }} #调用变量
2、列表类型
{% set list = ['one', 'two', 'three'] %}
{{ list[1] }} #获取变量的第一个值
3、字典类型
{% set dict = {'key1':'value1', 'key2':'value2'} %}
{{ dict['key1'] }} #获取'key1'的值
示例1:Saltstack使用jinja模块配置apache监听端口
#1.告诉file状态模块,需要使用jinja
- template: jinja
#2.列出参数列表
- defaults:
PORT: 8000
#3.配置文件引用jinja模板
{{ PORT }}
# 配置示例
apache-config:
file.managed:
- name: /etc/httpd/conf/httpd.conf
- source: salt://files/httpd.conf
- user: root
- group: root
- mode: 644
- template: jinja
- defaults:
PORT: 8000
# 修改httpd.conf配置文件引用变量
Listen {{ PORT }}
示例2:使用grinas 方式进行赋值
#配置示例
apache-config:
file.managed:
- name: /etc/httpd/conf/httpd.conf
- source: salt://files/httpd.conf
- user: root
- group: root
- mode: 644
- template: jinja
- defaults:
PORT: 8000
IPADDR: {{ grains['fqdn_ip4'][0] }}
# 修改httpd.conf配置文件引用变量
Listen {{ IPADDR }}:{{ PORT }}
示例3:通过jinja+grains根据系统不同安装apache
[root@salt-master ~]# cat /srv/salt/base/httpd.sls
#根据grains获取的值判别系统后安装软件
httpd-install:
pkg.installed:
{% if grains['os'] == 'CentOS' %}
- name: httpd
{% elif grains['OS'] == 'Debin' %}
- name: apache2
{% endif %}
执行结果如果报错,可以在minion节点上查看日志 : tail -f /var/log/salt/minion