舒畅 发表于 2018-7-31 07:58:32

SaltStack数据系统之Grains、Pillar

# vim /etc/salt/master  pillar_roots:
  base:
  - /srv/pillar/base
  prod:
  - /srv/pillar/prod
  #此段代码定义了base环境下的Pillar文件保存在/srv/pillar/base目录下。prod环境下的Pillar文件保存在/srv/pillar/prod下。
  # mkdir -p /srv/pillar/{base,prod}
  # tree /srv/pillar/
  /srv/pillar/
  ├── base
  └── prod
  # systemctl restart salt-master
  #创建base环境下的pillar文件为apache
  # vim /srv/pillar/base/apache.sls
  {% if grains['os'] == 'CentOS' %}
  apache: httpd
  {% elif grains['os'] == 'Debian' %}
  apache: apache2
  {% endif %}
  #与State相似,Pillar也有top file,也使用相同的匹配方式将数据应用到minion上。示例如下:
  # vim /srv/pillar/base/top.sls
  base:
  '*':
  - apache
  # salt '*' pillar.items
  linux-node1.example.com:
  ----------
  apache:
  httpd
  linux-node2.example.com:
  ----------
  apache:
  httpd
  #在base环境下,引用pillar
  # vim /srv/salt/base/web/apache.sls
  apache-install:
  pkg.installed:
  - name: {{ pillar['apache'] }}
  apache-service:
  service.running:
  - name: {{ pillar['apache'] }}
  - enable: True
  # salt '*' state.highstate
  linux-node2.example.com:
  ----------
  ID: apache-install
  Function: pkg.installed
  Name: httpd
  Result: True
  Comment: All specified packages are already installed
  Started: 15:15:13.424547
  Duration: 940.333 ms
  Changes:
  ----------
  ID: apache-service
  Function: service.running
  Name: httpd
  Result: True
  Comment: The service httpd is already running
  Started: 15:15:14.366780
  Duration: 55.706 ms
  Changes:
  Summary for linux-node2.example.com
  ------------
  Succeeded: 2
  Failed:    0
  ------------
  Total states run:   2
  Total run time: 996.039 ms
  linux-node1.example.com:
  ----------
  ID: apache-install
  Function: pkg.installed
  Name: httpd
  Result: True
  Comment: All specified packages are already installed
  Started: 15:15:14.648492
  Duration: 8242.769 ms
  Changes:
  ----------
  ID: apache-service
  Function: service.running
  Name: httpd
  Result: True
  Comment: The service httpd is already running
  Started: 15:15:22.891907
  Duration: 42.651 ms
  Changes:
  Summary for linux-node1.example.com
  ------------
  Succeeded: 2
  Failed:    0
  ------------
  Total states run:   2
  Total run time:   8.285 s
页: [1]
查看完整版本: SaltStack数据系统之Grains、Pillar