8-saltstack(5)_通过Jinja2模块以及Grain和Pillar扩展主机状态_一

用Jinja2配合Grains扩展SLS配置文件

不同系统安装httpd

install_httpd:
  pkg.installed:
{% if grains['os_family'] == 'Debian' %}
  - names: apache2
{% elif grains['os_family'] == 'RedHat' %}
  - names: httpd
{% endif %}

多系统安装vim

vim:
  pkg:
    - installed
    {% if grains['os_family'] == 'RedHat' %}
    - name: vim-enhanced
    {% elif grains['os_family'] == 'Debian' %}
    - name: vim-nox
    {% endif %}
{% if grains['os'] == 'Arch' %}
/etc/vimrc:
  file:
    - managed
    - source: salt://vim/vimrc
    - user: root
    - group: root
    - mode: 644
    - template: jinja
    - makedirs: True
    - require:
      - pkg: vim
{% endif %}
vim:
  pkg.installed:
    {% if grains['os_family'] == 'RedHat' %}
    - name: vim-enhanced
    {% elif grains['os_family'] == 'Debian' %}
    - name: vim-nox
    {% endif %}
{% if grains['os'] == 'Arch' %}
/etc/vimrc:
  file.managed:
    - source: salt://vim/vimrc
    - user: root
    - group: root
    - mode: 644
    - template: jinja
    - makedirs: True
    - require:
      - pkg: vim
{% endif %}

用Jinja2配合Pillar扩展SLS配置文件

## mkdir -pv /srv/pillar && cd /srv/pillar

## vim user133.sls
user:
  - user01
  - user02
  - user03
## vim user134.sls
user:
  - user1
  - user2
  - user3
  
## vim top.sls  =====> saltstack的操作是从base标签开始的,这里是筛选条件
base:
  '192.168.184.133':
    - user133                     ## 指定user133.sls是192.168.184.133minion端的数据
  '192.168.184.134':
    - user134                     ## 指定user134.sls是192.168.184.134minion端的数据

## salt 192.168.184.133,192.168.184.134 saltutil.refresh_pillar    ===>刷新pillar数据

## salt 192.168.184.133,192.168.184.134 pillar.item user   
192.168.184.133:
    ----------
    user:
        - user01
        - user02
        - user03
192.168.184.134:
    ----------
    user:
        - user1
        - user2
        - user3
        
## 如上,那么我们就可以根据minion各自的pillar值建立相关的用户,代码请看:
{% for i in pillar['user'] %}
add_{{ i }}:
  user.present:
    - name: {{ i }}
{% endfor %}

你可能感兴趣的:(8-saltstack(5)_通过Jinja2模块以及Grain和Pillar扩展主机状态_一)