关闭

SaltStack实战之配置管理-Jinja2模板

标签: saltstack
656人阅读 评论(0) 收藏 举报
分类:

SaltStack实战之配置管理-Jinja2模板

@(学习)[SaltStack]

1. Salt yaml配置文件使用Jinja2模板介绍

jinja2官方网站:
http://jinja.pocoo.org/

1.1 File状态使用template参数

- template: jinja

1.2 模板文件里面变量使用{{ 名称 }}

{{ PORT }}

1.3 变量列表

- defaults:
  PORT: 8080

2. 实战应用

添加监听主机和端口变量

vim /srv/salt/devfiles/httpd.conf

Listen {{ HOST }}:{{ PORT }}

# {{ MAC }}

配置salt master配置文件、pillar配置文件、lamp.sls添加jinja模板:

[root@salt-master112 dev]# egrep -v '^$|^[#]' /etc/salt/master
state_top: top.sls
file_roots:
  base:
    - /srv/salt
  dev:
    - /srv/salt/dev
  test:
    - /srv/salt/test
  prod:
    - /srv/salt/prod
pillar_roots:
  base:
    - /srv/salt/pillar
  dev:
    - /srv/salt/dev/pillar
[root@salt-master112 dev]# cat /srv/salt/pillar/top.sls 
dev:
  'node*.test.com':
    - apache
[root@salt-master112 dev]# cat /srv/salt/dev/pillar/apache.sls 
apache:
  HOST: {{ grains['fqdn_ip4'][0] }}
  PORT: 8080
  MAC: {{ salt['network.hw_addr']('eth0') }}
[root@salt-master112 dev]# salt '*' saltutil.refresh_pillar 
node1.test.com:
    True
master.test.com:
    True
node2.test.com:
    True
[root@salt-master112 dev]# salt '*' pillar.items            
master.test.com:
    ----------
node1.test.com:
    ----------
    apache:
        ----------
        HOST:
            10.1.0.109
        MAC:
            00:50:56:a4:44:7a
        PORT:
            8080
node2.test.com:
    ----------
    apache:
        ----------
        HOST:
            10.1.0.110
        MAC:
            00:50:56:a4:44:7a
        PORT:
            8080
[root@salt-master112 dev]# cat /srv/salt/dev/lamp.sls 
lamp-pkg-install:
  pkg.installed:
    - names:
      - php
      - php-fpm
      - mysql
      - php-mysql
      - php-pdo

apache-service:
  pkg.installed:
    - name: httpd
  file.managed:
    - name: /etc/httpd/conf/httpd.conf
    - source:
      - salt://files/httpd.conf
    - user: root
    - group: root
    - mode: 644
    - template: jinja
    - defaults:
      HOST: {{ pillar['apache']['HOST'] }}
      PORT: {{ pillar['apache']['PORT'] }}
      MAC: {{ pillar['apache']['MAC'] }}
    - require:
      - pkg: apache-service
  service.running:
    - name: httpd
    - enable: True
    - reload: True
    - watch:
      - file: apache-service

mysql-service:
  pkg.installed:
    - name: mysql-server
    - require_in:
      - file: mysql-service
  file.managed:
    - name: /etc/my.cnf
    - source:
      - salt://files/my.cnf
    - user: root
    - group: root
    - mode: 644
    - watch_in:
      - service: mysql-service
  service.running:
    - name: mysqld
    - enable: True

php-fpm-service:
  service.running:
    - name: php-fpm
    - enable: True
[root@salt-master112 dev]#  

执行结果:

[root@salt-master112 dev]# salt '*' state.highstate
master.test.com:
----------
          ID: /etc/resolv.conf
    Function: file.managed
      Result: True
     Comment: File /etc/resolv.conf is in the correct state
     Started: 17:54:00.390576
    Duration: 31.841 ms
     Changes:   

Summary for master.test.com
------------
Succeeded: 1
Failed:    0
------------
Total states run:     1
Total run time:  31.841 ms
node1.test.com:
----------
          ID: /etc/resolv.conf
    Function: file.managed
      Result: True
     Comment: File /etc/resolv.conf is in the correct state
     Started: 09:56:43.761673
    Duration: 34.809 ms
     Changes:   
----------
          ID: lamp-pkg-install
    Function: pkg.installed
        Name: php-fpm
      Result: True
     Comment: Package php-fpm is already installed
     Started: 09:56:44.225287
    Duration: 630.568 ms
     Changes:   
----------
          ID: lamp-pkg-install
    Function: pkg.installed
        Name: php-pdo
      Result: True
     Comment: Package php-pdo is already installed
     Started: 09:56:44.856050
    Duration: 0.522 ms
     Changes:   
----------
          ID: lamp-pkg-install
    Function: pkg.installed
        Name: php
      Result: True
     Comment: Package php is already installed
     Started: 09:56:44.856677
    Duration: 0.366 ms
     Changes:   
----------
          ID: lamp-pkg-install
    Function: pkg.installed
        Name: php-mysql
      Result: True
     Comment: Package php-mysql is already installed
     Started: 09:56:44.857143
    Duration: 0.386 ms
     Changes:   
----------
          ID: lamp-pkg-install
    Function: pkg.installed
        Name: mysql
      Result: True
     Comment: Package mysql is already installed
     Started: 09:56:44.857623
    Duration: 0.349 ms
     Changes:   
----------
          ID: apache-service
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: Package httpd is already installed
     Started: 09:56:44.858061
    Duration: 0.343 ms
     Changes:   
----------
          ID: apache-service
    Function: file.managed
        Name: /etc/httpd/conf/httpd.conf
      Result: True
     Comment: File /etc/httpd/conf/httpd.conf updated
     Started: 09:56:44.858780
    Duration: 34.022 ms
     Changes:   
              ----------
              diff:
                  ---  
                  +++  
                  @@ -133,9 +133,9 @@
                   # prevent Apache from glomming onto all bound IP addresses (0.0.0.0)
                   #
                   #Listen 12.34.56.78:80
                  -Listen 0.0.0.0:8080
                  -
                  -# 00:50:56:a4:7a:70
                  +Listen 10.1.0.109:8080
                  +
                  +# 00:50:56:a4:44:7a
                   # Dynamic Shared Object (DSO) Support
                   #
                   # To be able to use the functionality of a module which was built as a DSO you
----------
          ID: apache-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: Service reloaded
     Started: 09:56:44.950689
    Duration: 87.451 ms
     Changes:   
              ----------
              httpd:
                  True
----------
          ID: mysql-service
    Function: pkg.installed
        Name: mysql-server
      Result: True
     Comment: Package mysql-server is already installed
     Started: 09:56:45.038390
    Duration: 0.739 ms
     Changes:   
----------
          ID: mysql-service
    Function: file.managed
        Name: /etc/my.cnf
      Result: True
     Comment: File /etc/my.cnf is in the correct state
     Started: 09:56:45.039596
    Duration: 17.182 ms
     Changes:   
----------
          ID: mysql-service
    Function: service.running
        Name: mysqld
      Result: True
     Comment: The service mysqld is already running
     Started: 09:56:45.057008
    Duration: 68.422 ms
     Changes:   
----------
          ID: php-fpm-service
    Function: service.running
        Name: php-fpm
      Result: True
     Comment: The service php-fpm is already running
     Started: 09:56:45.125649
    Duration: 53.97 ms
     Changes:   

Summary for node1.test.com
-------------
Succeeded: 13 (changed=2)
Failed:     0
-------------
Total states run:     13
Total run time:  929.129 ms
node2.test.com:
----------
          ID: /etc/resolv.conf
    Function: file.managed
      Result: True
     Comment: File /etc/resolv.conf is in the correct state
     Started: 09:56:44.357709
    Duration: 56.916 ms
     Changes:   
----------
          ID: lamp-pkg-install
    Function: pkg.installed
        Name: php-fpm
      Result: True
     Comment: Package php-fpm is already installed
     Started: 09:56:45.634382
    Duration: 1721.668 ms
     Changes:   
----------
          ID: lamp-pkg-install
    Function: pkg.installed
        Name: php-pdo
      Result: True
     Comment: Package php-pdo is already installed
     Started: 09:56:47.356467
    Duration: 1.34 ms
     Changes:   
----------
          ID: lamp-pkg-install
    Function: pkg.installed
        Name: php
      Result: True
     Comment: Package php is already installed
     Started: 09:56:47.358095
    Duration: 1.359 ms
     Changes:   
----------
          ID: lamp-pkg-install
    Function: pkg.installed
        Name: php-mysql
      Result: True
     Comment: Package php-mysql is already installed
     Started: 09:56:47.359721
    Duration: 1.553 ms
     Changes:   
----------
          ID: lamp-pkg-install
    Function: pkg.installed
        Name: mysql
      Result: True
     Comment: Package mysql is already installed
     Started: 09:56:47.361541
    Duration: 1.361 ms
     Changes:   
----------
          ID: apache-service
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: Package httpd is already installed
     Started: 09:56:47.363199
    Duration: 1.377 ms
     Changes:   
----------
          ID: apache-service
    Function: file.managed
        Name: /etc/httpd/conf/httpd.conf
      Result: True
     Comment: File /etc/httpd/conf/httpd.conf updated
     Started: 09:56:47.365880
    Duration: 84.28 ms
     Changes:   
              ----------
              diff:
                  ---  
                  +++  
                  @@ -133,9 +133,9 @@
                   # prevent Apache from glomming onto all bound IP addresses (0.0.0.0)
                   #
                   #Listen 12.34.56.78:80
                  -Listen 0.0.0.0:8080
                  -
                  -# 00:50:56:a4:00:5c
                  +Listen 10.1.0.110:8080
                  +
                  +# 00:50:56:a4:44:7a
                   # Dynamic Shared Object (DSO) Support
                   #
                   # To be able to use the functionality of a module which was built as a DSO you
----------
          ID: apache-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: Service reloaded
     Started: 09:56:47.617560
    Duration: 236.903 ms
     Changes:   
              ----------
              httpd:
                  True
----------
          ID: mysql-service
    Function: pkg.installed
        Name: mysql-server
      Result: True
     Comment: Package mysql-server is already installed
     Started: 09:56:47.855453
    Duration: 6.781 ms
     Changes:   
----------
          ID: mysql-service
    Function: file.managed
        Name: /etc/my.cnf
      Result: True
     Comment: File /etc/my.cnf is in the correct state
     Started: 09:56:47.865735
    Duration: 72.983 ms
     Changes:   
----------
          ID: mysql-service
    Function: service.running
        Name: mysqld
      Result: True
     Comment: The service mysqld is already running
     Started: 09:56:47.939601
    Duration: 169.518 ms
     Changes:   
----------
          ID: php-fpm-service
    Function: service.running
        Name: php-fpm
      Result: True
     Comment: The service php-fpm is already running
     Started: 09:56:48.109622
    Duration: 157.914 ms
     Changes:   

Summary for node2.test.com
-------------
Succeeded: 13 (changed=2)
Failed:     0
-------------
Total states run:     13
Total run time:    2.514 s
[root@salt-master112 dev]# 

node1和node2的apache配置文件,Listen位置内容也有相应变化。

[root@im109 ~]# vim /etc/httpd/conf/httpd.conf 
Listen 10.1.0.109:8080

# 00:50:56:a4:44:7a
[root@im110 ~]# vim /etc/httpd/conf/httpd.conf 
Listen 10.1.0.110:8080

# 00:50:56:a4:44:7a
0
0
查看评论

3.saltstack使用指南:Jinja使用技巧

saltstack中使用Jinjajinja简介Jinja是基于python的模板引擎,在saltstack中我们使用yaml_jinja渲染器来根据模板生产对应的配置文件,对于不同的操作系统或者不同的情况通过jinja可以让配置文件或者操作形成一种模板的编写方式。一般在diango或者saltst...
  • linux_player_c
  • linux_player_c
  • 2016-12-05 16:27
  • 1520

配置管理工具Saltstack 之 自定义grains

salt master配置 # cat /etc/salt/master|grep -v '#' interface: 192.168.xxx.xxx worker_threads: 15 auto_accept: True file_roots: b...
  • qq942477618
  • qq942477618
  • 2016-12-13 11:51
  • 492

saltstack配置管理

vim /etc/salt/master mkdir /srv/salt -p
  • xiegh2014
  • xiegh2014
  • 2016-08-07 13:12
  • 668

实践SaltStack安装和配置管理

1
  • picture1213
  • picture1213
  • 2015-01-22 15:06
  • 793

Saltstack配置状态同步管理

SaltStack State的数据有两种表现形式,分别是High State与Low State。High State是用户可见的数据,而Low State则是由Salt提取并使用的数据。其它这是同一份数据,只不过在不同场合使用了不同的展示形式。 服务、安装包或配置文件的状态同步,是State模...
  • watermelonbig
  • watermelonbig
  • 2017-09-15 08:56
  • 275

后端_第六课_开始使用JINJA2模板

JINJA2套路
  • weixin_38244232
  • weixin_38244232
  • 2017-11-30 12:11
  • 35

Flask零基础到项目实战(三)Jinja2模板系统 (二)

一、if语句(1)语法 {% if xxx %} {% else %} {% endif %}if的使用,可以和python中相差无几。(2)应用main.py#encoding: utf-8 from flask import Flask,render_templateapp...
  • qq_28877125
  • qq_28877125
  • 2017-08-27 16:35
  • 489

基于saltstack实现的配置集中化管理

Saltstack是一个具备puppet与func功能为一身的集中化管理平台,saltstack基于python实现,功能十分强大,各模块融合度及复用性极高,官方极力推荐作为云计算平台的基础架构。轻松维护成千上万台服务器不是问题,现分享作者基于saltstack实现一个集中化的配置管理平台,以Ngi...
  • cnbird2008
  • cnbird2008
  • 2013-09-16 13:11
  • 5457

用flask开发个人博客(10)—— Jinja2模板中的控制结构

前面介绍了Jinja2模板中的变量,下面介绍下模板中的控制结构。 if语句: {% if condition %} ... {% else %} ... {% endif %} 在模板文件中实现,输入用户名时浏览器上显示Hello 用户名,若不输入用户名显示Hello Stra...
  • lzhui1987
  • lzhui1987
  • 2016-09-19 22:57
  • 1533

关于Python的web模板jinja2的简单例子

''' 上一篇博客简单介绍了Python对web框架flask运用的一个实例:http://blog.csdn.net/three_co/article/details/78408792 我们发现使用web框架flask的时候,我们只需要去实现一个个函数 ,函数返回一个...
  • three_co
  • three_co
  • 2017-11-01 23:48
  • 1073
    个人资料
    • 访问:53589次
    • 积分:1048
    • 等级:
    • 排名:千里之外
    • 原创:92篇
    • 转载:3篇
    • 译文:0篇
    • 评论:9条
    文章分类
    最新评论