jinjia模板
需求:想让saltstack的file模块分发到minion端的配置文件监听minion端的IP和端口,如何用变量实现?看下面!!
1、jinja模板加grains使apache监听本地端口
# cat 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:
- template: jinja #指定使用jinja模板
- defaults: #变量列表
PORT: #端口变量
IPADDR: {{ grains['fqdn_ip4'][] }} #ip变量{此变量使用grains获取的IP地址}
- require:
- pkg: lamp-install apache-auth:
pkg.installed:
- name: httpd-tools
- require_in:
-cmd: apache-auth
cmd.run:
- name: htpasswd -bc /etc/httpd/conf/htpasswd_file admin admin
- unless: test -f /etc/httpd/conf/htpasswd_file apache-conf:
file.recurse:
- name: /etc/httpd/conf.d
- source: salt://web/files/apache-conf.d
- watch_in:
- service: lamp-service php-config:
file.managed:
- name: /etc/php.ini
- source: salt://web/files/php.ini
- user: root
- group: root
- mode:
- watch_in:
- service: lamp-service lamp-service:
service.running:
- name: httpd
- enable: True
- reload: True
- watch:
- file: apache-conf
- file: apache-config
1.1、修改apache-config状态下的source指定的配置文件
# sed -n '42p' /srv/salt/base/web/files/httpd.conf
Listen {{ IPADDR }}:{{ PORT }}
注:将之前的Listen 80改为Listen {{ IPADDR }}:{{ PORT }}