saltstack通过jinja模板,grains方式将配置的变量值写入到配置文件中?
需求描述:
在通过saltstack进行jinja模板获取值的时候,可以通过grains的方式,获取一些操作系统相关的信息,比如,OS,ip地址等,在这里演示下,做个记录。
演示:
1.修改sls文件,增加jinja模板,选项
[root@testvm01 lamp]# cat lamp.sls lamp-pkgs: pkg.installed: - pkgs: - httpd - php - mysql - mysql-server - php-mysql - php-cli - php-mbstring apache-config: file.managed: - name: /etc/httpd/conf/httpd.conf - source: salt://lamp/files/httpd.conf - user: root - group: root - mode: 644 - template: jinja - defaults: Listen_Port: 86 OS: {{ grains['os'] }} #通过grains的方式获取os的值 php-config: file.managed: - name: /etc/php.ini - source: salt://lamp/files/php.ini - user: root - group: root - mode: 644 apache-service: service.running: - name: httpd - enable: True - reload: True - watch: - file: apache-config mysql-service: service.running: - name: mysqld - enable: True - reload: True
2.修改httpd配置文件,应用这个变量值
#
# OS is {{ OS }} #在httpd.conf配置文件中,增加个注释,为了演示是否可以获取该变量值
3.执行state模块
[root@testvm01 lamp]# salt 'testvm03' state.sls lamp.lamp testvm03: ---------- ID: lamp-pkgs Function: pkg.installed Result: True Comment: All specified packages are already installed Started: 16:41:23.280481 Duration: 483.52 ms Changes: ---------- ID: apache-config Function: file.managed Name: /etc/httpd/conf/httpd.conf Result: True Comment: File /etc/httpd/conf/httpd.conf updated Started: 16:41:23.767025 Duration: 78.04 ms Changes: ---------- diff: --- +++ @@ -6,7 +6,7 @@ # <URL:http://httpd.apache.org/docs/2.2/mod/directives.html> # for a discussion of each configuration directive. # -# +# OS is RedHat #通过这个部分看,已经获取到grains中os的值 # Do NOT simply read the instructions in here without understanding # what they do. They're here only as hints or reminders. If you are unsure # consult the online docs. You have been warned. ---------- ID: php-config Function: file.managed Name: /etc/php.ini Result: True Comment: File /etc/php.ini is in the correct state Started: 16:41:23.845276 Duration: 12.077 ms Changes: ---------- ID: apache-service Function: service.running Name: httpd Result: True Comment: Service reloaded Started: 16:41:23.893749 Duration: 81.956 ms Changes: ---------- httpd: True ---------- ID: mysql-service Function: service.running Name: mysqld Result: True Comment: The service mysqld is already running Started: 16:41:23.976028 Duration: 113.57 ms Changes: Summary for testvm03 ------------ Succeeded: 5 (changed=2) Failed: 0 ------------ Total states run: 5 Total run time: 769.163 ms
备注:并且在watch的监控下,发现配置文件有变化,也执行了httpd服务的重启。这样,就完成了,通过grains的方法获取某些值,然后将这些值放入到配置文件中。
文档创建时间:2019年3月11日16:43:21