Pillar Walkthrough 翻译

官方文档地址:http://docs.saltstack.com/topics/tutorials/pillar.html

Pillar Walkthrough

Note

阅读这份手册之前,假定读者已经完成了Salt Stack基础 手册的阅读。


Pillar接口是Salt众多组件中最重要的部署组件,Pillar被用来为特别的从服务器生成特定数据的接口。在Pillar中生成的数据提供给所有的Salt组件并且被用来一下目的:

高度敏感数据:

所有通过pillar传输的信息能够保证被提交给目标主机,这使得Pillar引擎在Salt中被用来管理安全的信息,如,密码的Key和密码口令等。

从服务器配置:

从服务模块如远程执行模块,状态,反馈信息都可以使用存储在pillar中的数据来配置。

变量:

那些需要分配给特定的从服务器或者群组的变量可以在pillar中定义并且能够在SLS配置模板和稳重中使用。

任意数据:

Pillar能够包含任意基本的数据结构,如,列表数据,或者k/v存储。可以让数据容易在SLS文档中使用。

因此,Pillar是在使用Slat的过程中最重要系统之一,这篇教程设计用来在几分钟之内编写一个简单的pillar文件,然后运用这个Pillar 使它的数据是可用的在pillar文件中。


设置Pillar

默认情况下,Pillar 已经运行在了Salt系统中,在从服务器上的Pillar的数据能够通过下面的命令查看到:

# salt '*' pillar.data

默认情况下,主服务器的配置文件内容装载在fillar中对所用的从服务器来说,通过配置主服务器的配置文件来使全局的从服务器生效。

Pillar是类似内置的state 文件,它有sls文件组成且有一个视图文件,就像state tree(翻译为,state 文件)。相对于state文件,Pillar 存储在Slat主服务器上的不同目录。默认的目录是/srv/pillar


注意:

     Pillar的目录能够通过修改主服务配置文件中的pillar_roots选项进行更改。


在配置Pillar之前,需要创建/srv/pillar目录。

# mkdir /srv/pillar

一个简单的视图文件,格式类似state,需要创建,其内容如下:

/srv/pillar/top.sls

base:
  '*':
    - data

视图文件包含data.sls文件,这个文件在所有的从服务器上生效,/srv/pillar/data.sls文件如下:

/srv/pillar/data.sls

info: some data

至此,保存该文件,从服务器上的pillars信息将被更新:

# salt '*' pillar.data

info关键字的信息会出现在返回的pillar数据中


更多复杂的数据:

Pillar文件是SLS文件,像states,但是不想sates文件,他们不需要定义格式,数据可以是任意的,下面的例子中,通过UID设置用户数据,

/srv/pillar/usrs/init.sls

users:
  thatch: 1000
  shouse: 1001
  utahdave: 1002
  redbeard: 1003


注意:

相同的目录中查找 states文件在Pilla目录中,因此 users/init.sls文件能够在视图文件中被引用。


视图文件需要更新一下以引用这个sls文件:

/srv/pillar/top.sls

base:
  '*':
    - data
    - users

现在的话,这些数据对从服务器来说是可用的了。使用Jinjia 在state文件中引用pillar 中的数据。

/srv/salt/users/init.sls

{% for user, uid in pillar.get('users', {}).items() %}
{{user}}:
  user.present:
    - uid: {{uid}}
{% endfor %}
以上可以把users 安全的定义在一个pillar中,并且user 数据可以应用在sls文件中。



Pillar作为states 的参数

Pillar最重要的一个抽象功能就是能够作为states的参数。而不是定义宏或者函数,在states文件内容中,可以自由地参数化相对于所有的从服务器的pillar.

以上的话可以让Salt变的更加灵活,这意味着一些简单的sls格式的文件能够用作结构的参数而不需要重构state文件树。

一个简单的例子是根据不同的Linux 分发版本引导安装不同的软件包在pillar中定义。

/srv/pillar/pkg/init.sls

pkgs:
  {% if grains['os_family'] == 'Debian' %}
  apache: httpd
  vim: vim-enhanced
  {% elif grains['os_family'] == 'RedHat' %}
  apache: apache2
  vim: vim
  {% elif grains['os'] == 'Arch' %}
  apache: apache
  vim: vim
  {% endif %}

这个新的pkg sls需要增加到视图文件中:

/srv/pillar/top.sls

base:
  '*':
    - data
    - users
    - pkg

至此,从服务器将会根据各自的操作系统自动对应在pillar 只内定义的软件安装包,所以sls文件可以安全的引用:

/srv/salt/apache/init.sls

apache:
  pkg.installed:
    - name: {{ pillar['pkgs']['apache'] }}

更或者,假如pillar不可以用,也可以这样设置缺省值:

NOte:

      在这个例子中使用的pillar.get 函数是在Salt 0.14.0版本中增加的。

/srv/salt/apache/init.sls

apache:
  pkg.installed:
    - name: {{ salt['pillar.get']('pkgs:apache', 'httpd') }}

在上面的例子中,假如  pillar 的变量pillar['pkgs'] ['apache'] 在从服务器上没有被设置,那么默认的'httpd'将会被使用。


Pillar使简单的states变的灵活

Pillar其中一个设计目标是使简单的sls格式变得易用和灵活,而不是需要复杂和重构state文件。

简单的格式:

/srv/salt/edit/vim.sls

vim:
  pkg:
    - installed

/etc/vimrc:
  file.managed:
    - source: salt://edit/vimrc
    - mode: 644
    - user: root
    - group: root
    - require:
      - pkg: vim

可以很容易地转换成一个强大的、参数化公式:

/srv/salt/edit/vim.sls

vim:
  pkg:
    - installed
    - name: {{ pillar['pkgs']['vim'] }}

/etc/vimrc:
  file.managed:
    - source: {{ pillar['vimrc'] }}
    - mode: 644
    - user: root
    - group: root
    - require:
      - pkg: vim

vimrc的源位置可以通过pillar 改变:

/srv/pillar/edit/vim.sls

{% if grain['id'].startswith('dev') %}
vimrc: salt://edit/dev_vimrc
{% elif grain['id'].startswith('qa') %}
vimrc: salt://edit/qa_vimrc
{% else %}
vimrc: salt://edit/vimrc
{% endif %}

确保正确的vimrc发送正确的从服务器上。

更多关于Pillar

Pillar的数据创建在Salt主服务器上并且安全的分发到从服务器上。Salt当定义pillar的时候 并不局限于pollar sls 文件,可以从其他额外的源取得数据。对于那些把基础结构信息存储在其他的地方会很有用。
参考信息和其他外部Pillar接口可以 在Salt 文档中找到:

Pillar

http://docs.saltstack.com/topics/pillar/index.html


















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Illustrated Guide to Learning Python is designed to bring developers and others who are anxious to learn Python up to speed quickly. Not only does it teach the basics of syntax, but it condenses years of experience. You will learn warts, gotchas, best practices and hints that have been gleaned through the years in days. You will hit the ground running and running in the right way. Learn Python Quickly Python is an incredible language. It is powerful and applicable in many areas. It is used for automation of simple or complex tasks, numerical processing, web development, interactive games and more. Whether you are a programmer coming to Python from another language, managing Python programmers or wanting to learn to program, it makes sense to cut to the chase and learn Python the right way. You could scour blogs, websites and much longer tomes if you have time. Treading on Python lets you learn the hints and tips to be Pythonic quickly. Packed with Useful Hints and Tips You'll learn the best practices without wasting time searching or trying to force Python to be like other languages. I've collected all the gems I've gleaned over years of writing and teaching Python for you. A No Nonsense Guide to Mastering Basic Python Python is a programming language that lets you work more quickly and integrate your systems more effectively. You can learn to use Python and see almost immediate gains in productivity and lower maintenance costs. What you will learn: •Distilled best practices and tips •How interpreted languages work •Using basic types such as Strings, Integers, and Floats •Best practices for using the interpreter during development •The difference between mutable and immutable data •Sets, Lists, and Dictionaries, and when to use each •Gathering keyboard input •How to define a class •Looping constructs •Handling Exceptions in code •Slicing sequences •Creating modular code •Using libraries •Laying out code •Community prescribed conventions

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值