什么是Pillar?

Pillar是在salt 0.9.8版本后才添加的功能组件。它跟grains的结构一样,也是一个字典格式,数据通过key/value的格式进行存储。在Salt的设计中,Pillar使用独立的加密sessiion,所以Pillar可以用来传递敏感的数据,例如ssh-key,加密证书等。


Pillar 是什么样子?


###  一台centos 6.5的 dell r420服务器的pillar
[root@yw_home _grains]# salt 192.168.0.100 pillar.data
192.168.0.100:
    ----------
    master:
        ----------
        auth_mode:
            1
        auto_accept:
            False
        cachedir:
            /var/cache/salt/master
        client_acl:
            ----------
        client_acl_blacklist:
            ----------
        cluster_masters:
        cluster_mode:
            paranoid
        conf_file:
            /etc/salt/master
        config_dir:
            /etc/salt
        cython_enable:
            False
        daemon:
            True
        default_include:
            master.d/*.conf
        enable_gpu_grains:
            False
        enforce_mine_cache:
            False
        enumerate_proxy_minions:
            False
        environment:
            None
        ext_job_cache:
            
        ext_pillar:
        extension_modules:
            /var/cache/salt/master/extmods
        external_auth:
            ----------
        external_nodes:
            
        failhard:
            False
        file_buffer_size:
            1048576
        file_client:
            local
        file_ignore_glob:
            None
        file_ignore_regex:
            None
        file_recv:
            False
        file_roots:
            ----------
            base:
                - /srv/salt
        fileserver_backend:
            - roots
        fileserver_followsymlinks:
            True
        fileserver_ignoresymlinks:
            False
        fileserver_limit_traversal:
            False
        gather_job_timeout:
            2
        gitfs_base:
            master
        gitfs_remotes:
        gitfs_root:
            
        hash_type:
            md5
        hgfs_base:
            default
        hgfs_branch_method:
            branches
        hgfs_remotes:
        hgfs_root:
            
        id:
            192.168.0.100
        interface:
            0.0.0.0
        ipv6:
            False
        jinja_lstrip_blocks:
            False
        jinja_trim_blocks:
            False
        job_cache:
            True
        keep_jobs:
            24
        key_logfile:
            /var/log/salt/key
        keysize:
            4096
        log_datefmt:
            %H:%M:%S
        log_datefmt_logfile:
            %Y-%m-%d %H:%M:%S
        log_file:
            /data/logs/salt_master.log
        log_fmt_console:
            [%(levelname)-8s] %(message)s
        log_fmt_logfile:
            %(asctime)s,%(msecs)03.0f [%(name)-17s][%(levelname)-8s] %(message)s
        log_granular_levels:
            ----------
        log_level:
            warning
        loop_interval:
            60
        master_ext_job_cache:
            
        master_roots:
            ----------
            base:
                - /srv/salt-master
        master_tops:
            ----------
        max_open_files:
            100000
        minion_data_cache:
            True
        nodegroups:
            ----------
            group_all:
                *
        open_mode:
            False
        order_masters:
            False
        outputter_dirs:
        peer:
            ----------
        permissive_pki_access:
            False
        pidfile:
            /var/run/salt-master.pid
        pillar_opts:
            True
        pillar_roots:
            ----------
            base:
                - /srv/salt/pillar
        pillar_version:
            2
        pki_dir:
            /etc/salt/pki/master
        pub_hwm:
            1000
        publish_port:
            4505
        publish_session:
            86400
        range_server:
            range:80
        reactor:
        renderer:
            yaml_jinja
        ret_port:
            4506
        root_dir:
            /
        runner_dirs:
        salt_transport:
            zeromq
        saltversion:
            2014.1.0
        search:
            
        search_index_interval:
            3600
        serial:
            msgpack
        sign_pub_messages:
            False
        sock_dir:
            /var/run/salt/master
        state_auto_order:
            True
        state_events:
            True
        state_output:
            full
        state_top:
            salt://top.sls
        state_verbose:
            True
        svnfs_remotes:
        svnfs_root:
            
        syndic_master:
            
        syndic_master_port:
            4506
        syndic_wait:
            1
        timeout:
            5
        token_dir:
            /var/cache/salt/master/tokens
        token_expire:
            43200
        user:
            root
        verify_env:
            True
        win_gitrepos:
            - https://github.com/saltstack/salt-winrepo.git
        win_repo:
            /srv/salt/win/repo
        win_repo_mastercachefile:
            /srv/salt/win/repo/winrepo.p
        worker_threads:
            10

pillar的数据是跟特定的minion关联的,可以用来传递minion自己的信息。管理员也可以自定义自己的pillar来对minion进行管理。

默认情况下,master配置文件中的所有数据都添加到Pillar中,且对所有minion可用。如果要禁用这一默认值,可以在master配置文件中添加如下数据,重启服务后生效:

pillar_opts: False


自定义Pillar数据  

在Master的SLS文件中定义

假设使用默认pillar_roots配置 /srv/pillar

vim /srv/pillar/top.sls
## 编辑pillar 的 top.sls 文件添加下面内容
base : 
  '*':
    - data
    - git
dev  :
  'os:CentOs':
    - git    

vim /srv/pillar/data/init.sls
## 添加下面内容
roles:webserver


vim  /srv/pillar/git.sls
## 添加下面内容
{% if grains['os'] == 'CentOs' %} 
git: git
{% elif grains['os'] == 'Debian' %} 
git: git-core
{% endif %}


pillar 更sls一样拥有自己的top.sls文件,通过top.sls文件作为入口,组织其它的pillar文件。

data定义了一个roles的值,如果不使用详细的data.init声明,则默认会使用data下的init.sls作为声明文件,如果同时存在data.sls跟data/init.sls文件,则会优先使用data.sls文件来访问。

git是使用grains的系统类型来进行不同的pillar定义。


在master上修改Pilla文件后,需要用以下命令刷新minion上的数据:

salt '192.168.0.100' saltutil.refresh_pillar


怎么使用Pillar ?

  • Targeting :

      Salt使用-I 选项来使用

salt -I 'roles:webserver' test.ping
  • SLS文件中使用

需要根据系统环境安装一个git软件

vim /srv/salt/git_env.sls
## 添加下面内容
git : 
  pkg.installed:
    - name : {{pillar['git']}}
    
## 或是添加成下面样式,设置默认的pillar值
git:
  pkg.installed:
    - name: {{ salt['pillar.get']('git', 'git') }}

 

参考文章: 

http://www.ituring.com.cn/article/42398

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