一:jinja模版
利用httpd自动化部署来使用变量
以下的都是在httpd服务的service.sls文件中进行的添加变量的操作
(变量设置端口)
1:添加上jinja模版
[root@srever4 httpd]# vim service.sls
include:
- httpd.install
/etc/httpd/conf/httpd.conf:
file.managed:
- source: salt://httpd/files/httpd.conf
- template: jinja ###添加的jinja模版
port: 8080 ##指定端口为8080
httpd-service:
service.running:
- name: httpd
- enable: False
- reload: True
watch:
- file: /etc/httpd/conf/httpd.conf
2:设置端口变量
[root@srever4 httpd]# vim files/httpd.conf
Listen {{ port }}
3:master上进行推送
[root@srever4 httpd]# salt server5 state.sls httpd.service
4:minion端查看端口,发现为定义的8080端口
tcp6 0 0 :::8080 :::* LISTEN 2473/httpd
(变量设置主机)
[root@srever4 httpd]# vim service.sls
include:
- httpd.install
/etc/httpd/conf/httpd.conf:
file.managed:
- source: salt://httpd/files/httpd.conf
- template: jinja
port: 8080
host: 172.25.60.5 ##添加主机名
httpd-service:
service.running:
- name: httpd
- enable: False
- reload: True
watch:
- file: /etc/httpd/conf/httpd.conf
2:设主机名变量
[root@srever4 httpd]# vim files/httpd.conf
Listen {{ host }}:{{ port }}
3:master上进行推送
[root@srever4 httpd]# salt server5 state.sls httpd.service
4:minion进行查看,发现ip已经取出来了
tcp 0 0 172.25.60.5:8080 0.0.0.0:* LISTEN 3173/httpd
(3)使用grains加变量获取主机名
1:使用granis获取主机名
[root@srever4 httpd]# salt server5 grains.item ipv4
server5:
----------
ipv4:
- 127.0.0.1
- 172.25.60.5
2:编辑.sls文件
[root@srever4 httpd]# vim service.sls
include:
- httpd.install
/etc/httpd/conf/httpd.conf:
file.managed:
- source: salt://httpd/files/httpd.conf
- template: jinja
port: 8080
host: {{ grains['ipv4'][-1]}}
httpd-service:
service.running:
- name: httpd
- enable: False
- reload: True
watch:
- file: /etc/httpd/conf/httpd.conf
3:master端进行推送
[root@srever4 httpd]# salt server5 state.sls httpd.service
4:minion端进行查看 将主机ip成功的取出来了
tcp 0 0 172.25.60.5:8080 0.0.0.0:* LISTEN 3173/httpd
5:minion端的httpd服务配置文件中,监听的端口也被修改
[root@server5 ~]# vim /etc/httpd/conf/httpd.conf
Listen 172.25.60.5:8080
(4)使用pillar结合变量来获取主机ip
1:修改pillar目录下web.sls文件
[root@srever4 pillar]# vim web.sls
{% if grains['fqdn'] == 'server5' %}
webserver: httpd
IP: 172.25.60.5
{% elif grains['fqdn'] == 'server6' %}
webserver: nginx
IP: 172.25.60.6
{% endif %}
2:[root@srever4 httpd]# vim service.sls
include:
- httpd.install
/etc/httpd/conf/httpd.conf:
file.managed:
- source: salt://httpd/files/httpd.conf
- template: jinja
port: 8080
host: {{ pillar['IP'] }} ##使用pillar
httpd-service:
service.running:
- name: httpd
- enable: False
- reload: True
watch:
- file: /etc/httpd/conf/httpd.conf
3:推送
[root@srever4 httpd]# salt server5 state.sls httpd.service
该方法也可以
=======================================================================================
使用files/httpd.conf httpd服务的模版实现使用变量获取ip地址
方法1:
1:建立lib.sls文件
[root@srever4 httpd]# vim lib.sls
{% set host = '172.25.60.5' %}
2:修改httpd服务模版
[root@srever4 httpd]# vim files/httpd.conf
{% from 'httpd/lib.sls' import host %}
3:修改httpd服务service.sls文件
include:
- httpd.install
/etc/httpd/conf/httpd.conf:
file.managed:
- source: salt://httpd/files/httpd.conf
- template: jinja
port: 8080
host: 127.0.0.1 ##端口指定为本机
httpd-service:
service.running:
- name: httpd
- enable: False
- reload: True
watch:
- file: /etc/httpd/conf/httpd.conf
4:master上进行推送
[root@srever4 httpd]# salt server5 state.sls httpd.service
5:minion查看端口情况
tcp 0 0 172.25.60.5:8080 0.0.0.0:* LISTEN 3173/httpd
主机ip获取成功
方法2:
1:编辑模版
[root@srever4 httpd]# vim files/httpd.conf
删除之前的加的那句话
Listen {{ pillar['IP'] }}:{{ port }} ##添加
2:编辑service.sls文件
[root@srever4 httpd]# vim service.sls
include:
- httpd.install
/etc/httpd/conf/httpd.conf:
file.managed:
- source: salt://httpd/files/httpd.conf
- template: jinja
port: 80 ##知道端口为80
httpd-service:
service.running:
- name: httpd
- enable: False
- reload: True
watch:
- file: /etc/httpd/conf/httpd.conf
3:master端进行推送
[root@srever4 httpd]# salt server5 state.sls httpd.service
4:minion端查看端口状态
tcp 0 0 172.25.60.5:80 0.0.0.0:* LISTEN 3173/httpd
==================================================================================================
使用变量来替换自动化部署nginx服务的版本号
1:修改文件
[root@srever4 nginx]# vim install.sls
{% set nginx_ver = '1.15.8' %}
nginx-install:
pkg.installed:
- pkgs:
- pcre-devel
- zlib-devel
- gcc
- make
file.managed:
- name: /mnt/nginx-{{ nginx_ver }}.tar.gz
- source: salt://nginx/files/nginx-{{ nginx_ver }}.tar.gz
cmd.run:
- name: cd /mnt && tar zxf nginx-{{ nginx_ver }}.tar.gz && cd nginx-{{ nginx_ver }} && sed -i 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && ./configure --prefix=/usr/local/nginx &> /dev/null && make &> /dev/null && make install &> /dev/null && cd .. && rm -rf nginx-{{ nginx_ver }}
- creates: /usr/local/nginx
2:推送
[root@srever4 nginx]# salt server6 state.sls nginx.service