# salt本地管理 无master架构

# file_client: remote # 将 remote 改为 local
salt-call --local state.sls web.tomcat

# zabbix-agent 案例
/srv/salt/
├── base # 公共的
│ ├── init # ---初始化
│ │ ├── files
│ │ │ └── epel-7.repo
│ │ └── yum-repo.sls
│ ├── logstash # ---logstash
│ ├── top.sls
│ ├── web
│ │ ├── apache.sls
│ │ ├── apache-tomcat-8.0.46.tar.gz
│ │ ├── append.sls
│ │ ├── lamp.sls
│ │ └── tomcat.sls
│ └── zabbix # ---zabbix
│ ├── files
│ │ └── zabbix_agentd.conf
│ └── zabbix-agent.sls
├── dev
├── prod
└── test

1. vim yum-repo.sls
/etc/yum.repos.d/epel-7.repo
file.managed:
- source: salt://init/files/epel-7.repo
- user: root
- group: root
- mode: 644

2. vim zabbix-agent.sls
#include:
#- init: yum-repo

zabbix-agent:
pkg.installed:
- name: zabbix40-agent
#- require:
# - file: /etc/yum.repos.d/epel.repo
file.managed:
- name: /etc/zabbix_agentd.conf
- source: salt://zabbix/files/zabbix_agentd.conf
- user: root
- group: root
- mode: 644
- template: jinja
- defaults:
ZABBIX_SERVER: 192.168.2.103
AGENT_HOSTNAME: {{ grains['fqdn'] }}
- require:
- pkg: zabbix-agent
service.running:
- name: zabbix-agent
- enable: True
- watch:
- file: zabbix-agent
- pkg: zabbix-agent

zabbix_agent.conf.d:
file.directory:
- name: /etc/zabbix_agentd.conf.d
- watch_in:
- service: zabbix-agent
- require:
- pkg: zabbix-agent
- file: zabbix-agent
3. zabbix_agentd.conf
Server={{ ZABBIX_SERVER }}
Hostname={{ AGENT_HOSTNAME }}

Include=/etc/zabbix_agentd.conf.d/ # 去掉注释
4. salt '*' state.sls zabbix.zabbix-agent test=True
5. salt '*' state.sls zabbix.zabbix-agent
# 执行效果如下:
#---------------------------------------------------------------------------
192.168.2.102:
----------
ID: zabbix-agent
Function: pkg.installed
Name: zabbix40-agent
Result: True
Comment: Package zabbix40-agent is already installed.
Started: 23:38:15.756715
Duration: 15205.207 ms
Changes:
----------
ID: zabbix-agent
Function: file.managed
Name: /etc/zabbix_agentd.conf
Result: True
Comment: The file /etc/zabbix_agentd.conf is in the correct state
Started: 23:38:30.963639
Duration: 20.409 ms
Changes:
----------
ID: zabbix_agent.conf.d
Function: file.directory
Name: /etc/zabbix_agentd.conf.d
Result: True
Comment: The directory /etc/zabbix_agentd.conf.d is in the correct state
Started: 23:38:30.987142
Duration: 1.938 ms
Changes:
----------
ID: zabbix-agent
Function: service.running
Result: True
Comment: Service zabbix-agent is already enabled, and is in the desired state
Started: 23:38:30.989642
Duration: 1308.499 ms
Changes:

Summary
------------
Succeeded: 4
Failed: 0
------------
Total states run: 4
192.168.2.104:
----------
ID: zabbix-agent
Function: pkg.installed
Name: zabbix40-agent
Result: True
Comment: Package zabbix40-agent is already installed.
Started: 05:25:14.603753
Duration: 13347.962 ms
Changes:
----------
ID: zabbix-agent
Function: file.managed
Name: /etc/zabbix_agentd.conf
Result: True
Comment: The file /etc/zabbix_agentd.conf is in the correct state
Started: 05:25:28.106326
Duration: 120.26 ms
Changes:
----------
ID: zabbix_agent.conf.d
Function: file.directory
Name: /etc/zabbix_agentd.conf.d
Result: True
Comment: The directory /etc/zabbix_agentd.conf.d is in the correct state
Started: 05:25:28.227367
Duration: 0.364 ms
Changes:
----------
ID: zabbix-agent
Function: service.running
Result: True
Comment: Service zabbix-agent is already enabled, and is in the desired state
Started: 05:25:28.227845
Duration: 1109.733 ms
Changes:

Summary
------------
Succeeded: 4
Failed: 0
------------
Total states run: 4
#---------------------------------------------------------------------------

6. 系统初始化
DNS file.managed
防火墙 service.dead
limit设置 file.managed
SSH useDNS设置,修改端口 file.managed
systemctl 内核参数调优 systemctl
关闭不需要的服务 service
时间同步 file.managed cron
基础软件包 pkg.installed
include:
- init.yum-repo

base-pkg:
pkg.installed:
- pkg:
- screen
- lrzsz
- vim
yum源 file.managed