rgr3255 发表于 2015-7-30 09:19:53

SaltSack入门(四)Salt常用模块使用

以下用一些实例说明salt一些常用的模块,进一步熟悉salt使用。
模块参考地址:http://docs.saltstack.cn/zh_CN/latest/ref/states/all/index.html
pkg模块:
1、安装一个软件

1
2
3
vim:
pkg.installed:
    - name: vim-enhanced




2、安装多个软件

1
2
3
4
5
vim:
pkg.installed:
    - names:            #多个值时,要修改为复数,names
      - vim-enhanced
      - lrzsz




service模块:
1、安装apache,并启动

1
2
3
4
5
6
7
8
httpd:
pkg.installed:
    - name: httpd
service.running:
    - enable: True
    - require:         
    #一个声明,用于定义状态之间的依赖,说明httpd如果安装了,才执行service.running模块
      - pkg: httpd   # pkg代表用的什么模块




2、将配置文件分发到apache服务器

1
2
3
file.managed:
- name: /etc/httpd/conf/httpd.conf
- source: salt://apache/conf/httpd.conf




3、如果apache配置文件有变动,就重新加载配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
httpd:
pkg.installed:
    - name: httpd
file.managed:
    - name: /etc/httpd/conf/httpd.conf
    - source: salt://test/httpd.conf
    - require:
      - pkg: httpd
service.running:
    - enable: True
    - reload: True
    - watch:            
    #监控httpd.confp文件是否发生变化,是就重新加载httpd服务
      - file: /etc/httpd/conf/httpd.conf
    - require:
      - pkg: httpd




cmd模块:
1、编译安装nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
nginx_source:
file.managed:
    - name: /tmp/nginx-1.6.2.tar.gz
    - unless: test -f /tmp/nginx-1.6.2.tar.gz   
    #在这里用到了unless,就是做了个判断,判断有没有这个文件,如果没有就执行nginx_source ID,否则跳过
    - source: salt://test/nginx-1.6.2.tar.gz
nginx_unzip:
cmd.run:
    - cwd: /tmp
    - names: tar zxf nginx-1.6.2.tar.gz
    - unless: test -d /tmp/nginx-1.6.2
    - require:
      - file: nginx_source
nginx_pkg:
pkg.installed:
    - names:
      - gcc
      - make
      - openssl-devel
      - pcre-devel
      - zlib-devel
nginx_install:
cmd.run:
    - cwd: /tmp/nginx-1.6.2
    - name: ./configure --prefix=/usr/local/nginx1.6 && make && make install && /usr/local/nginx1.6/sbin/nginx   
    #这一块为什么不用names呢,我也想用,可是他不是按顺序执行的,而是随机执行里面的命令的,容易造成了混乱。
    - require:   
    #依赖两个ID状态,也就是必须有/tmp/nginx-1.6.2这个目录,和安装了gcc、make、pcre等这些依赖包,才执行nginx_install这个ID
      - cmd: nginx_unzip               
      - pkg: nginx_pkg
    - unless: test -d /usr/local/nginx1.6




在这里介绍编译安装nginx思路,其中用到了file、cmd.run、pkg模块。

file模块:1、文件分发
1
2
3
4
file_cp:
file.managed:
   - name: /etc/httpd/conf/httpd.conf
   - source: salt://test/httpd.conf




2、目录分发
1
2
3
4
tmp_cp:
file.recurse:
    - name: /tmp
    - source: salt://tmp




3、创建目录并赋予权限
1
2
3
4
5
6
/opt/tmp:
file.directory:
    - user: nginx
    - group: nginx
    - file_mode: 744
    - makedirs: True




user模块:
1、创建用户nginx

1
2
3
4
5
6
7
8
9
create_user:
user.present:#用户不存在则创建,否则管理用户属性
    - name: nginx
    #- uid: 1501
    #- gid: 1501
    - createhome: False
    - shell: /sbin/nologin
    #- groups:
    #- nginx




2、删除用户nginx

1
2
3
del_user:
user.absent:
    - name: abc




group模块:
1、创建组abc

1
2
3
4
5
6
7
8
9
10
create_group:
group.present:
    - name: abc
    #- gid: 1501
    - addusers:   #添加哪些用户到此组
      - user1
      - user2
    #- delusers:   #从此组中删除哪些用户
    #- u1
    #- u2




2、删除组abc

1
2
3
del_group:
group.absent:
    - name: abc




cron模块:
1、添加定时任务计划

1
2
3
4
5
6
script_cron:
cron.present:
    - name: /bin/bash /opt/tmp/test.sh
    - user: root
    - minute: 01
    - hour: 0




注:minute:分
hour:时
daymonth:日
month:月
dayweek:周

页: [1]
查看完整版本: SaltSack入门(四)Salt常用模块使用