1.修改配置文件
取消以下注释
file_roots:
base:
- /srv/salt
2.创建目录
[root@salt-master ~]# mkdir /srv/salt -p
[root@salt-master salt]# mkdir config_file
[root@salt-master config_file]# mkdir script
[root@salt-master script]# tree -L 3 /srv/salt/
/srv/salt/
└── config_file
└── script
└── test.sh
2 directories, 1 fil
[root@salt-master script]# salt 'salt-minion' cmd.script salt://config_file/script/test.sh
执行这个命令就可以在minion端执行这个脚本
3.写sls文件
top.sls
base:
'salt-minion':
- vsftpd
~
vsftpd.sls
/opt/vsftpd:
file.managed:
- source: salt://config_file/vsftpd.conf
- user: root
- group: root
- mode: 600
执行结果
[root@salt-master salt]# salt 'salt-minion' state.highstate#会采用top.sls的方式
salt-minion:
----------
ID: /opt/vsftpd
Function: file.managed
Result: True
Comment: File /opt/vsftpd updated
Started: 15:41:03.898742
Duration: 13.612 ms
Changes:
----------
diff:
New file
Summary
------------
Succeeded: 1 (changed=1)
Failed: 0
------------
Total states run: 1
[root@salt-master salt]# vim config_file/
script/ vsftpd.conf
[root@salt-master salt]# vim config_file/vsftpd.conf
[root@salt-master salt]#
[root@salt-master salt]# salt 'salt-minion' state.sls vsftpd#执行sls文件可在
salt-minion:
----------
ID: /opt/vsftpd
Function: file.managed
Result: True
Comment: File /opt/vsftpd updated
Started: 15:41:49.520423
Duration: 11.542 ms
Changes:
----------
diff:
---
+++
@@ -1,1 +1,2 @@
hello
+yys
Summary
------------
Succeeded: 1 (changed=1)
Failed: 0
------------
Total states run: 1
如果sls文件没有在salt路径的根目录执行sls也要相应添加路径
[root@salt-master salt]# salt 'salt-minion' state.sls sls_file.vsftpd
salt-minion:
----------
ID: /opt/vsftpd
Function: file.managed
Result: True
Comment: File /opt/vsftpd updated
Started: 15:46:29.673069
Duration: 11.47 ms
Changes:
----------
diff:
---
+++
@@ -1,2 +1,3 @@
hello
yys
+linux
Summary
------------
Succeeded: 1 (changed=1)
Failed: 0
------------
Total states run: 1
grains:
跟puppet的facter功能一样,主要负责采集客户端一些基本信息,也可自定义,可以在客户端自定义,然后自动汇报上来,也可以从服务端定义然后推下去,采集完成在汇报上来pillar跟grains比较的话灵活性更强
查看所有的grains项
[root@salt-master salt]# salt 'salt-minion' grains.ls
salt-minion:
- SSDs
- cpu_flags
- cpu_model
- cpuarch
- domain
- fqdn
- fqdn_ip4
- fqdn_ip6
- gpus
- host
- hwaddr_interfaces
- id
- init
- ip4_interfaces
- ip6_interfaces
- ip_interfaces
- ipv4
- ipv6
- kernel
- kernelrelease
- locale_info
- localhost
- lsb_distrib_codename
- lsb_distrib_id
- lsb_distrib_release
- master
- mdadm
- mem_total
- nodename
- num_cpus
- num_gpus
- os
- os_family
- osarch
- oscodename
- osfinger
- osfullname
- osmajorrelease
- osrelease
- osrelease_info
- path
- ps
- pythonexecutable
- pythonpath
- pythonversion
- saltpath
- saltversion
- saltversioninfo
- selinux
- server_id
- shell
- virtual
- zmqversion
列出所有grains的项和项的值
[root@salt-master salt]# salt 'salt-minion' grains.items
salt-minion:
----------
SSDs:
cpu_flags:
- fpu
- vme
- de
- pse
- tsc
- msr
- pae
- mce
- cx8
- apic
- sep
- mtrr
- pge
- mca
- cmov
- pat
- pse36
- clflush
- dts
- mmx
- fxsr
- sse
- sse2
- ss
- syscall
- nx
- pdpe1gb
- rdtscp
- lm
- constant_tsc
- up
- arch_perfmon
- pebs
- bts
- xtopology
- tsc_reliable
- nonstop_tsc
- aperfmperf
- unfair_spinlock
- pni
- pclmulqdq
- ssse3
- fma
- cx16
- pcid
- sse4_1
- sse4_2
- x2apic
- movbe
- popcnt
- tsc_deadline_timer
- aes
- xsave
- avx
- f16c
- rdrand
- hypervisor
- lahf_lm
- abm
- 3dnowprefetch
- ida
- arat
- xsaveopt
- pln
- pts
- dtherm
- hwp
- hwp_noitfy
- hwp_act_window
- hwp_epp
- fsgsbase
- bmi1
- avx2
- smep
- bmi2
- invpcid
- rdseed
- adx
cpu_model:
Intel(R) Core(TM) i5-6400 CPU @ 2.70GHz
cpuarch:
x86_64
domain:
fqdn:
salt-minion
fqdn_ip4:
- 192.168.1.201
fqdn_ip6:
gpus:
|_
----------
model:
SVGA II Adapter
vendor:
unknown
host:
salt-minion
hwaddr_interfaces:
----------
eth1:
00:0c:29:38:1e:f7
lo:
00:00:00:00:00:00
id:
salt-minion
init:
upstart
ip4_interfaces:
----------
eth1:
- 192.168.1.201
lo:
- 127.0.0.1
ip6_interfaces:
----------
eth1:
- fe80::20c:29ff:fe38:1ef7
lo:
- ::1
ip_interfaces:
----------
eth1:
- 192.168.1.201
- fe80::20c:29ff:fe38:1ef7
lo:
- 127.0.0.1
- ::1
ipv4:
- 127.0.0.1
- 192.168.1.201
ipv6:
- ::1
- fe80::20c:29ff:fe38:1ef7
kernel:
Linux
kernelrelease:
2.6.32-642.el6.x86_64
locale_info:
----------
defaultencoding:
UTF8
defaultlanguage:
en_US
detectedencoding:
UTF-8
localhost:
salt-minion
lsb_distrib_codename:
Final
lsb_distrib_id:
CentOS
lsb_distrib_release:
6.8
master:
salt-master
mdadm:
mem_total:
474
nodename:
salt-minion
num_cpus:
1
num_gpus:
1
os:
CentOS
os_family:
RedHat
osarch:
x86_64
oscodename:
Final
osfinger:
CentOS-6
osfullname:
CentOS
osmajorrelease:
6
osrelease:
6.8
osrelease_info:
- 6
- 8
path:
/sbin:/usr/sbin:/bin:/usr/bin
ps:
ps -efH
pythonexecutable:
/usr/bin/python2.6
pythonpath:
- /usr/bin
- /usr/lib64/python26.zip
- /usr/lib64/python2.6
- /usr/lib64/python2.6/plat-linux2
- /usr/lib64/python2.6/lib-tk
- /usr/lib64/python2.6/lib-old
- /usr/lib64/python2.6/lib-dynload
- /usr/lib64/python2.6/site-packages
- /usr/lib/python2.6/site-packages
pythonversion:
- 2
- 6
- 6
- final
- 0
saltpath:
/usr/lib/python2.6/site-packages/salt
saltversion:
2015.5.10
saltversioninfo:
- 2015
- 5
- 10
- 0
selinux:
----------
enabled:
True
enforced:
Permissive
server_id:
1259242440
shell:
/bin/sh
virtual:
VMware
zmqversion:
3.2.5
查看grains单个项的详细情况
[root@salt-master salt]# salt 'salt-minion' grains.item os
salt-minion:
----------
os:
CentOS
[root@salt-master salt]# salt 'salt-minion' grains.item shell
salt-minion:
----------
shell:
/bin/sh
[root@salt-master salt]# salt 'salt-minion' grains.item pythonpath
salt-minion:
----------
pythonpath:
- /usr/bin
- /usr/lib64/python26.zip
- /usr/lib64/python2.6
- /usr/lib64/python2.6/plat-linux2
- /usr/lib64/python2.6/lib-tk
- /usr/lib64/python2.6/lib-old
- /usr/lib64/python2.6/lib-dynload
- /usr/lib64/python2.6/site-packages
- /usr/lib/python2.6/site-packages
grains灵活应用:
对所有是centos系统进行test.ping
[root@salt-master salt]# salt -G 'os:CentOS' test.ping
salt-minion:
True
查询对所有系统cpu是64位的个数
[root@salt-master salt]# salt -G 'cpuarch:x86_64' grains.item num_cpus
salt-minion:
----------
num_cpus:
1
自定义grains:
在minion端的配置文件中加入
grains:
roles:
- webserver
- minion
deployment: datacenter4
cabinet: 12
cab_u: 14-15
在master上就可以收集到相应信息
[root@salt-master salt]# salt 'salt-minion' grains.item roles deployment cabinet cab_u
salt-minion:
----------
cab_u:
14-15
cabinet:
12
deployment:
datacenter4
roles:
- webserver
- minion
states:
salt状态系统的核心是sls,sls表示系统将会是什么样的一种状态,而且是以一种很简单的格式来包含数据,也被叫做配置管理
sls文件实际上只是一些:字典,列表,字符串,数字
yaml:
缩进:salt需要对每个缩进级别是两个空格,但不能使用tabs
键值对采用冒号进行配对,用一个空格隔开key: value
想要表示列表项,使用一个-+空格 - webserver
http://docs.saltstack.com/cn/topics/yaml/index.html
state模块列表
http://docs.saltstack.com/en/latest/ref/states/all/index.html
pillar:
pillar是salt的一个组件,他用于特定的minion定义任何你需要的数据,这些数据可以被salt其它组件使用,pillar解析完成是一个嵌套的dict结构.pillar数据是与特定minion关联的,也就是每一个minion都只能看到自己的数据,所以pillar可以用来传递敏感数据
.
用途:
敏感数据
变量 使用jinja模版,可在文件中使用{{ 变量}} 传递
targetting
定时执行
schedule:
highstate:
funtion: state.highstate
minute: 1
schedule:
sls:
funtion: state.sls
minute: 1
首先打开pillar
pillar_roots:
base:
- /srv/pillar
创建相应目录
mkdir /srv/pillar/nginx
编写top.sls
cd /srv/pillar
必须要有top.sls
[root@salt-master pillar]# vim top.sls
base:
'salt-minion':
- nginx.nginx
编写nginx.sls
[root@salt-master pillar]# vim nginx/nginx.sls
schedule:
nginx:
function: state.sls
minutes: 1
args:
- 'nginx'
[root@salt-master pillar]# salt 'salt-minion' pillar.data
salt-minion:
----------
schedule:
----------
nginx:
----------
args:
- nginx
function:
state.sls
minutes:
1
修改配置文件
[root@salt-master pillar]# vim ../salt/config_file/nginx/conf.d/default.conf
[root@salt-master pillar]# salt 'salt-minion' saltutil.refresh_pillar
salt-minion:
True
[root@salt-master pillar]# salt 'salt-minion' cmd.run 'netstat -anutlp|grep nginx'
salt-minion:
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 6535/nginx
tcp 0 0 0.0.0.0:8088 0.0.0.0:* LISTEN 6535/nginx
等待一分钟
以上需要注意的是具体应用的sls需要和实际命令一致
例如:
salt 'salt-minion' state.sls sls_file.nginx
schedule:
nginx:
function: state.sls
minutes: 1
args:
- 'nginx'
就应该为以下内容
schedule:
nginx:
function: state.sls
minutes: 1
args:
- 'sls_file.nginx'
top.sls为一个路由文件
base:
'salt-minion':
- nginx.nginx
补充:
例如:
nginx:
pkg:
- installed
service:
- running
- enable: True
- reload: True
- watch:
- pkg: nginx
- file: /etc/nginx/nginx.conf
- file: /etc/nginx/conf.d/default.conf
/etc/nginx/nginx.conf:
file.managed:
- source: salt://config_file/nginx/nginx.conf
- user: root
- group: root
- mode: 644
/etc/nginx/conf.d/default.conf:
file.managed:
- source: salt://config_file/nginx/conf.d/default.conf
- user: root
- group: root
- mode: 644
- template:jinja
- defaults:# - context:
port: 8080
在salt://config_file/nginx/conf.d/default.conf这个文件中
Listen {{ port }}
这样就可以在模版以变量的方式传递进去
也可以用控制
- context:
{% if grains['id'] == 'salt-minion-01' %}
port: 8080
{% elif grains['id'] == 'salt-minion-02' %}
port 8081
{% else %}
port: 8082
{% endif %}
另外还可以采用这种方式
pillar
nginx:
{% if grains['id'] == 'salt-minion-01' %}
port: 8080
{% elif grains['id'] == 'salt-minion-02' %}
port 8081
{% else %}
port: 8082
{% endif %}
salt '*' saltutil.refresh.pillar
salt '*' pillar.get nginx:port
- context:
port: {{ salt['pillar.get']('nginx:port', 80)}}
#port: pillar['nginx']['port']