1.调用jinja
(1)脚本指定jinja参数
[root@server1 apache]# pwd
/srv/salt/apache
[root@server1 apache]# vim install.sls
httpd-install:
pkg.installed:
- pkgs:
- httpd
- php
- httpd-tools
file.managed:
- name: /etc/httpd/conf/httpd.conf
- source: salt://apache/files/httpd.conf
- user: root
- group: root
- mode: 644
- template: jinja
- context:
port: 80
host: 172.25.26.2
(2)配置文件中引入可变参数
[root@server1 apache]# vim files/httpd.conf
42 Listen {{ host }}:{{ port }}
(3)脚本运行并查看效果
[root@server1 apache]# salt server2 state.sls apache.service
server2:
----------
ID: httpd-install
Function: pkg.installed
Result: True
Comment: All specified packages are already installed
Started: 12:48:06.907168
Duration: 481.045 ms
Changes:
----------
ID: httpd-install
Function: file.managed
Name: /etc/httpd/conf/httpd.conf
Result: True
Comment: File /etc/httpd/conf/httpd.conf updated
Started: 12:48:07.390444
Duration: 51.021 ms
Changes:
----------
diff:
---
+++
@@ -39,7 +39,7 @@
# prevent Apache from glomming onto all bound IP addresses.
#
#Listen 12.34.56.78:80
-Listen 80
+Listen 172.25.26.2:80
#
# Dynamic Shared Object (DSO) Support
----------
ID: httpd-service
Function: service.running
Name: httpd
Result: True
Comment: Service reloaded
Started: 12:48:07.471402
Duration: 356.625 ms
Changes:
----------
httpd:
True
Summary for server2
------------
Succeeded: 3 (changed=2)
Failed: 0
------------
Total states run: 3
Total run time: 888.691 ms
2.jinja的结合使用
(1)查看节点的ip信息
[root@server1 apache]# salt server2 grains.item ipv4
server2:
----------
ipv4:
- 127.0.0.1
- 172.25.26.2
(2)更改配置文件中的可变参数
[root@server1 apache]# vim files/httpd.conf
42 Listen {{ grains['ipv4'][1] }}:{{ port }}
(3)运行脚本查看效果(效果与jinja模块相同)
[root@server1 apache]# salt server2 state.sls apache.service
server2:
----------
ID: httpd-install
Function: pkg.installed
Result: True
Comment: All specified packages are already installed
Started: 12:53:28.161169
Duration: 512.144 ms
Changes:
----------
ID: httpd-install
Function: file.managed
Name: /etc/httpd/conf/httpd.conf
Result: True
Comment: File /etc/httpd/conf/httpd.conf is in the correct state
Started: 12:53:28.675899
Duration: 124.974 ms
Changes:
----------
ID: httpd-service
Function: service.running
Name: httpd
Result: True
Comment: Service httpd is already enabled, and is running
Started: 12:53:28.802290
Duration: 320.86 ms
Changes:
----------
httpd:
True
Summary for server2
------------
Succeeded: 3 (changed=1)
Failed: 0
------------
Total states run: 3
Total run time: 957.978 ms
3.jinja结合grains使用
(1)更改默认配置文件
[root@server1 apache]# vim files/httpd.conf
42 Listen {{ host }}:{{ port }}
(2)在jinja模块中引入grains
[root@server1 apache]# vim install.sls
httpd-install:
pkg.installed:
- pkgs:
- httpd
- php
- httpd-tools
file.managed:
- name: /etc/httpd/conf/httpd.conf
- source: salt://apache/files/httpd.conf
- user: root
- group: root
- mode: 644
- template: jinja
- context:
port: 80
host: {{ grains['ipv4'][1] }}
(3)运行查看效果
[root@server1 apache]# salt server2 state.sls apache.service
server2:
----------
ID: httpd-install
Function: pkg.installed
Result: True
Comment: All specified packages are already installed
Started: 12:57:14.379034
Duration: 519.19 ms
Changes:
----------
ID: httpd-install
Function: file.managed
Name: /etc/httpd/conf/httpd.conf
Result: True
Comment: File /etc/httpd/conf/httpd.conf is in the correct state
Started: 12:57:14.900730
Duration: 84.098 ms
Changes:
----------
ID: httpd-service
Function: service.running
Name: httpd
Result: True
Comment: The service httpd is already running
Started: 12:57:14.986138
Duration: 32.318 ms
Changes:
Summary for server2
------------
Succeeded: 3
Failed: 0
------------
Total states run: 3
Total run time: 635.606 ms
4.jinja模块结合pillar进行使用
(1)在脚本文件的jinja模块引入pillar
[root@server1 apache]# vim install.sls
httpd-install:
pkg.installed:
- pkgs:
- httpd
- php
- httpd-tools
file.managed:
- name: /etc/httpd/conf/httpd.conf
- source: salt://apache/files/httpd.conf
- user: root
- group: root
- mode: 644
- template: jinja
- context:
port: {{ pillar['port'] }}
host: {{ pillar['ip'] }}
(2)在pillar的默认文件中写入jinja所需信息
[root@server1 apache]# vim /srv/pillar/vars.sls
{% if grains['fqdn'] == 'server2' %}
webserver: apache
state: master
ip: 172.25.26.2
port: 80
{% elif grains['fqdn'] == 'server3' %}
webserver: nginx
state: backup
ip: 172.25.26.3
port: 80
{% endif %}
(3)运行查看能否生效
[root@server1 apache]# salt server2 state.sls apache.service
server2:
----------
ID: httpd-install
Function: pkg.installed
Result: True
Comment: All specified packages are already installed
Started: 13:08:52.692769
Duration: 480.144 ms
Changes:
----------
ID: httpd-install
Function: file.managed
Name: /etc/httpd/conf/httpd.conf
Result: True
Comment: File /etc/httpd/conf/httpd.conf is in the correct state
Started: 13:08:53.175172
Duration: 36.505 ms
Changes:
----------
ID: httpd-service
Function: service.running
Name: httpd
Result: True
Comment: The service httpd is already running
Started: 13:08:53.212763
Duration: 29.687 ms
Changes:
Summary for server2
------------
Succeeded: 3
Failed: 0
------------
Total states run: 3
Total run time: 546.336 ms