saltstack SLS 实例分析学习

Posted FRESHMANS

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了saltstack SLS 实例分析学习相关的知识,希望对你有一定的参考价值。

文件下载:https://github.com/unixhot/saltbook-code

 

首先看一下目录结构(没用的已经删掉)

[[email protected]_master saltbook-code-master]# tree saltstack-haproxy/
saltstack-haproxy/
├── pillar
│   └── base
│       ├── top.sls
│       └── zabbix
│           └── agent.sls
└── salt
    ├── base
    │   ├── init
    │   │   ├── audit.sls
    │   │   ├── dns.sls
    │   │   ├── env_init.sls
    │   │   ├── epel.sls
    │   │   ├── files
    │   │   │   ├── resolv.conf
    │   │   │   └── zabbix_agentd.conf
    │   │   ├── history.sls
    │   │   ├── sysctl.sls
    │   │   └── zabbix_agent.sls
    │   └── top.sls
    └── prod
        ├── cluster
        │   ├── files
        │   │   ├── haproxy-outside.cfg
        │   │   └── haproxy-outside-keepalived.conf
        │   ├── haproxy-outside-keepalived.sls
        │   └── haproxy-outside.sls
        ├── haproxy
        │   ├── files
        │   │   ├── haproxy-1.5.3.tar.gz
        │   │   └── haproxy.init
        │   └── install.sls
        ├── keepalived
        │   ├── files
        │   │   ├── keepalived-1.2.17.tar.gz
        │   │   ├── keepalived.init
        │   │   └── keepalived.sysconfig
        │   └── install.sls
        ├── libevent
        │   ├── files
        │   │   └── libevent-2.0.22-stable.tar.gz
        │   └── install.sls
        ├── memcached
        │   ├── files
        │   │   └── memcached-1.4.24.tar.gz
        │   ├── install.sls
        │   └── service.sls
        ├── nginx
        │   ├── files
        │   │   ├── nginx-1.9.1.tar.gz
        │   │   ├── nginx.conf
        │   │   └── nginx-init
        │   ├── install.sls
        │   └── service.sls
        ├── pcre
        │   ├── files
        │   │   └── pcre-8.37.tar.gz
        │   └── install.sls
        ├── php
        │   ├── files
        │   │   ├── init.d.php-fpm
        │   │   ├── memcache-2.2.7.tgz
        │   │   ├── php-5.6.9.tar.gz
        │   │   ├── php-fpm.conf.default
        │   │   ├── php.ini-production
        │   │   └── redis-2.2.7.tgz
        │   ├── install.sls
        │   ├── php-memcache.sls
        │   └── php-redis.sls
        ├── pkg
        │   └── pkg-init.sls
        ├── user
        │   └── www.sls
        └── web
            ├── bbs.sls
            └── files
                └── bbs.conf

 

1)、pillar

[[email protected]_master saltstack-haproxy]# tree pillar/
pillar/
└── base
    ├── top.sls
    └── zabbix
        └── agent.sls

2 directories, 2 files

[[email protected]_master base]# cat base/top.sls 
base:
  ‘*‘:
    - zabbix.agent
[[email protected]_master base]# cat base/zabbix/agent.sls     #配置zabbix server的变量及值
zabbix-agent:
  Zabbix_Server: 192.168.56.21

 

2)、salt(目录结构看上面)

[[email protected]_master base]# cat top.sls 
base:
  ‘*‘:
    - init.env_init                  ##首先跳到init目录下的环境初始化sls
prod:                           #然后跳到对所有主机安装haproxy 目录下的sls
  ‘*‘:
    - cluster.haproxy-outside
    - cluster.haproxy-outside-keepalived
    - web.bbs
  ‘saltstack-node2.example.com‘:          #最后对特定主机安装memcache服务
    - memcached.service

 

2.1)init目录(入口)

#目录结构:
[[email protected]_master base]# tree init/ init/ ├── audit.sls ├── dns.sls ├── env_init.sls ├── epel.sls ├── files │   ├── resolv.conf │   └── zabbix_agentd.conf ├── history.sls ├── sysctl.sls └── zabbix_agent.sls 1 directory, 9 files

 

入口sls文件为env_init.sls

[[email protected]_master init]# cat env_init.sls 
include:
  - init.dns    
  - init.history
  - init.audit
  - init.sysctl
  - init.epel
  - init.zabbix_agent

 

对初始化环境sls文件进行分析(根据eve_init.sls中的位置依次查看)

 

[[email protected]_master init]# cat dns.sls 
/etc/resolv.conf:
  file.managed:
    - source: salt://init/files/resolv.conf      #表示将source所指目录下的文件覆盖到/etc/resolv.conf
    - user: root
    - gourp: root
    - mode: 644

#其他用法
[[email protected]_master salt] cat test.sls
test:
file.managed:
- name: /tmp/aaa.txt        
- source: salt://top.sls              #表示将source目录下文件覆盖到/tmp/目录下并命名为aaa.txt(此处不能直接写目录名称)
[[email protected]_master init]# cat history.sls 
/etc/profile:
  file.append:
    - text:
      - export HISTTIMEFORMAT="%F %T `whoami` "

[[email protected]_master init]# cat audit.sls 
/etc/bashrc:
  file.append:
    - text:
      - export PROMPT_COMMAND=‘{ msg=$(history 1 | { read x y; echo $y; });logger "[euid=$(whoami)]":$(who am i):[`pwd`]"$msg"; }‘

[[email protected]_master init]# cat sysctl.sls 
net.ipv4.ip_local_port_range:
  sysctl.present:
    - value: 10000 65000
fs.file-max:
  sysctl.present:
    - value: 2000000
net.ipv4.ip_forward:
  sysctl.present:
    - value: 1
vm.swappiness:
  sysctl.present:
    - value: 0

[[email protected]_master init]# cat epel.sls 
yum_repo_release:
  pkg.installed:
    - sources:
      - epel-release: http://mirrors.aliyun.com/epel/6/x86_64/epel-release-6-8.noarch.rpm
    - unless: rpm -qa | grep epel-release-6-8

[[email protected]_master init]# cat zabbix_agent.sls 
zabbix-agent:
  pkg.installed:
    - name: zabbix22-agent
  file.managed:
    - name: /etc/zabbix_agentd.conf
    - source: salt://init/files/zabbix_agentd.conf
    - template: jinja
    - defaults:
      Server: {{ pillar[‘zabbix-agent‘][‘Zabbix_Server‘] }}
    - require:
      - pkg: zabbix-agent
  service.running:
    - enable: True
    - watch:
      - pkg: zabbix-agent
      - file: zabbix-agent
zabbix_agentd.conf.d:
  file.directory:
    - name: /etc/zabbix_agentd.conf.d
    - watch_in:
      - service: zabbix-agent
    - require:
      - pkg: zabbix-agent
      - file: zabbix-agent

 

  

  

  

以上是关于saltstack SLS 实例分析学习的主要内容,如果未能解决你的问题,请参考以下文章

SaltStack学习系列之State安装Nginx+PHP环境

saltstack/salt的state.sls的使用

SaltStack之sls文件

Saltstack的sls

SaltStack状态间的关系模块

SaltStack状态间的关系模块