基于层次替换和jinja的saltstack配置模板

pepa的Python项目详细描述


PEPA

使用层次替换和jinja的saltstack配置模板。

drone.io build status

截至2014.7版,PEPA是Saltstack的一部分。

快速测试

您可以从命令行轻松测试pepa。

创建虚拟环境。并安装所需的模块。

virtualenv venv
cd venv
source bin/activate
pip install pepa

克隆并运行pepa。

git clone https://github.com/mickep76/pepa.git
cd pepa
pepa -c examples/master test.example.com -d

测试和验证模板。

pepa-test --config examples/master -d

看看输出。

pepa-test --config examples/master -d -s

安装PEPA

git clone https://github.com/mickep76/pepa.git
mkdir -p /srv/salt/ext/pillar
cp pillar/pepa.py /srv/salt/ext/pillar/pepa.py

配置PEPA

extension_modules:/srv/salt/extext_pillar:-pepa:resource:host# Name of resource directory and sub-key in pillarssequence:# Sequence used for hierarchical substitution-hostname:# Name of keyname:input# Alias used for template directorybase_only:True# Only use templates from Base environment, i.e. no staging-default:-environment:-location..region:name:region-location..country:name:country-location..datacenter:name:datacenter-roles:-osfinger:name:os-hostname:name:overridebase_only:Truesubkey:True# Create a sub-key in pillars, named after the resource in this case [host]subkey_only:True# Only create a sub-key, and leave the top level untouchedpepa_roots:# Base directory for each environmentbase:/srv/pepa/base# Path for base environmentdev:/srv/pepa/base# Associate dev with baseqa:/srv/pepa/qaprod:/srv/pepa/prod# Use a different delimiter for nested dictionaries, defaults to '..' since some keys may use '.' in the name#pepa_delimiter: ..# Supply Grains for Pepa, this should **ONLY** be used for testing or validation#pepa_grains:#  environment: dev# Supply Pillar for Pepa, this should **ONLY** be used for testing or validation#pepa_pillars:#  saltversion: 0.17.4# Enable debug for Pepa, and keep Salt on warning#log_level: debug#log_granular_levels:#  salt: warning#  salt.loaded.ext.pillar.pepa: debug

pepa也可用于无主盐堆设置。

命令行

usage: pepa [-h][-c CONFIG][-d][-g GRAINS][-p PILLAR][-n][-v]
            hostname

positional arguments:
  hostname              Hostname

optional arguments:
  -h, --help            show this help message and exit
  -c CONFIG, --config CONFIG
                        Configuration file
  -r RESOURCE, --resource RESOURCE
                        Resource, defaults to first resource
  -d, --debug           Print debug info
  -g GRAINS, --grains GRAINS
                        Input Grains as YAML
  -p PILLAR, --pillar PILLAR
                        Input Pillar as YAML
  -n, --no-color        No color output
  -v, --validate        Validate output

模板

模板是主机或软件的配置,可以使用来自颗粒或支柱的信息。然后可以使用它们进行层次替换。

示例文件:主机/输入/测试示例com.yaml

location..region:emealocation..country:nllocation..datacenter:foobarenvironment:devroles:-salt.masternetwork..gateway:10.0.0.254network..interfaces..eth0..hwaddr:00:20:26:a1:12:12network..interfaces..eth0..dhcp:Falsenetwork..interfaces..eth0..ipv4:10.0.0.3network..interfaces..eth0..netmask:255.255.255.0network..interfaces..eth0..fqdn:{{hostname}}cobbler..profile:fedora-19-x86_64

正如您在本例中看到的,您可以在模板中直接使用jinja。

示例文件:host/region/amer.yaml

network..dns..servers:-10.0.0.1-10.0.0.2time..ntp..servers:-ntp1.amer.example.com-ntp2.amer.example.com-ntp3.amer.example.comtime..timezone:America/Chihuahuayum..mirror:yum.amer.example.com

每个模板都使用小写字母以键的值命名,所有扩展字符都用下划线替换。

示例:

奥斯芬格:Fedora-19

将变成:

软呢帽19.yaml

嵌套词典

为了创建作为输出的嵌套字典,可以使用双点“。”作为分隔符。您可以使用“pepa_delimiter”更改此设置,我们选择了双点,因为在某些模块中,单点已经被键名使用,使用“:”需要在yaml中引用。

示例:

network..dns..servers:-10.0.0.1-10.0.0.2network..dns..options:-timeout:2-attempts:1-ndots:1network..dns..search:-example.com

将变成:

network:dns:servers:-10.0.0.1-10.0.0.2options:-timeout:2-attempts:1-ndots:1search:-example.com

运算符

运算符可用于合并/取消设置列表/哈希或将键设置为不可变,因此无法对其进行更改。

OperatorDescription
merge()Merge list or hash
unset()Unset key
immutable()Set the key as immutable, so it can’t be changed
imerge()Set immutable and merge
iunset()Set immutable and unset

示例:

network..dns..search..merge():-foobar.com-dummy.nlowner..immutable():Operationshost..printers..unset():

测试

pepa还提供了一个用于模板的测试/验证工具。这允许您测试有效的jinja/yaml并验证键值。

命令行

usage: pepa-test [-h][-c CONFIG][-r RESOURCE][-d][-s][-t][-n]

optional arguments:
  -h, --help            show this help message and exit
  -c CONFIG, --config CONFIG
                        Configuration file
  -r RESOURCE, --resource RESOURCE
                        Configuration file, defaults to first resource
  -d, --debug           Print debug info
  -s, --show            Show result of template
  -t, --teamcity        Output validation in TeamCity format
  -n, --no-color        No color output

测试

测试是模板的一组输入值,如果有jinja if语句,通常最好为每个结果创建一个单独的测试。

示例:host/default/tests/default-1.yaml

grains..osfinger:Fedora-20location..region:emea

您还可以在测试中使用jinja,例如,如果您不想遍历测试值。

模式

架构是每个键/值的一组验证规则。架构使用Cerberus模块进行验证。

示例:host/schemas/pkgrepo.yaml

{%set hostname = '^([a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?\.)+[a-zA-Z]{2,6}$' %}{%set url = '(http|https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)' %}pkgrepo..mirror:type:stringregex:{{hostname}}pkgrepo..type:type:stringallowed:yumpkgrepo..osabbr:type:stringregex:^(fc|rhel)[0-9]+${%for repo in['base','everything','updates']%}pkgrepo..repos..{{ repo }}..name:type:stringregex:^[A-Za-z\ 0-9\-\_]+$pkgrepo..repos..{{ repo }}..baseurl:type:stringregex:{{url}}{%endfor %}

您还可以在模式中使用jinja,例如,如果您不想遍历不同键的列表。

您可以在密钥下创建复杂的数据结构,但最好将其拆分为几个 使用嵌套数据结构的分隔符的键。

错误

network:interfaces:eth0:ipv4:192.168.1.2netmask:255.255.255.0

network..interfaces..eth0..ipv4:192.168.1.2network..interfaces..eth0..netmask:255.255.255.0

第一个示例无法正确使用替换,并且定义架构变得更加复杂。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何将字符串转换为自定义对象   java如何从socket方法获取数据?   Java中的soap读取回车和新行   java在单击时替换图像   java推荐的使用RXJava执行异步任务的方法   java MySql连接器JDBC驱动程序不支持连接池吗?   java将活动堆栈清理到顶部   java计数用户输入的数量   java从webservice下载大文件导致应用程序性能问题   JavaLocalDate。EPOCH不可用   java如何在使用Selenium等待一定时间后,在页面无法加载(get(url))时自动刷新页面   java Calendar setLenient方法不允许检查年份字段的健全性   java Eclipse和intelliJ 安卓 SDK问题   java为什么我可以在没有super关键字的情况下调用父方法?   java iText的PDF格式不好