SaltStack的配置管理--jinja
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SaltStack的配置管理--jinja相关的知识,希望对你有一定的参考价值。
SaltStack的配置管理--jinja需求场景:使用jinja模板,让各节点的httpd都监听在本机的ip
[[email protected] apache]# vim files/httpd.conf
配置文件修改监听的ip地址和端口以变量的形式
Listen {{ IPADDR }}:{{ PORT }}
[[email protected] apache]# vim init.sls
apache-install:
pkg.installed:
- name: httpd
apache-config:
file.managed:
- name: /etc/httpd/conf/httpd.conf
- source: salt://apache/files/httpd.conf
- user: root
- group: root
- mode: 644
- template: jinja------------------>声明这个是模板
- defaults:------------------------>定义默认的参数和值
PORT: 80------------------------->PORT和IPADDR都为变量
IPADDR: {{ grains[‘fqdn_ip4‘][0] }}---->通过grains返回数据,此处返回的是一个列表形式,增加[0]指定第一个
apache-auth:
pkg.installed:
- name: httpd-tools
cmd.run:
- name: htpasswd -bc /etc/httpd/conf/htpasswd_file admin admin
- unless: test -f /etc/httpd/conf/htpasswd_file
apache-service:
service.running:
- name: httpd
- enable: True
- reload: True
- watch:
- file: apache-config
[[email protected] apache]# salt -S "192.168.56.11" state.highstate
----------
ID: apache-config
Function: file.managed
Name: /etc/httpd/conf/httpd.conf
Result: True
Comment: File /etc/httpd/conf/httpd.conf updated
Started: 11:14:08.537957
Duration: 25.518 ms
Changes:
----------
diff:
---
+++
@@ -42,7 +42,7 @@
# prevent Apache from glomming onto all bound IP addresses.
#
#Listen 12.34.56.78:80
-Listen 80
+Listen 192.168.56.11:80---->更改为监听本机ip地址,端口为80
#
# Dynamic Shared Object (DSO) Support
[[email protected] apache]# netstat -tulnp|grep httpd
tcp 0 0 192.168.56.11:80 0.0.0.0:* LISTEN 6837/httpd
[[email protected] apache]# vim /srv/salt/base/top.sls
prod:
‘linux-node*.example.com‘:
- lamp
[[email protected] apache]# salt ‘*‘ state.highstate
[[email protected] ~]# netstat -tulnp |grep httpd
tcp 0 0 192.168.56.12:80 0.0.0.0:* LISTEN 16447/httpd
总结:
1.修改httpd.conf配置
2.修改sls增加:
- template: jinja
- defaults:
PORT: 80
IPADDR: {{ grains[‘fqdn_ip4‘][0] }}
以上是关于SaltStack的配置管理--jinja的主要内容,如果未能解决你的问题,请参考以下文章