Saltstack 常用的模块及API

   Saltstack提供了非常丰富的功能模块,设计操作系统的基础功能,常用工具支持等,

官网模块介绍

http://docs.saltstack.com/ref/modules/all/index.html

 

一 列出当前版本支持的模块

[root@hzbj-salt-020 ~]# salt '*' sys.list_modules
hzbj-tomcat-021:
    - acl
    - aliases
    - alternatives
    - apache
    - archive
    - artifactory
    - blockdev
    - btrfs
    - buildout
    - cloud
    - cmd
    - composer
    - config
    - container_resource
    - cp
    - cron
    - data
    - defaults
    - devmap
    - disk
    - django
    - dnsmasq
    - dnsutil
    - drbd
    - elasticsearch
    - environ
    - etcd
    - event
    - extfs
    - file
    - gem
    - genesis
    - git
    - grains
    - group
    - grub
    - hashutil
    - hg
    - hipchat
    - hosts
    - http
    - img
    - incron
    - ini
    - introspect
    - ip
    - iptables
    - jboss7
    - jboss7_cli
    - key
    - kmod
    - locale
    - locate
    - logrotate
    - lowpkg
    - lvm
    - match
    - mine
    - modjk
    - mount
    - network
    - openstack_config
    - pagerduty
    - pillar
    - pip
    - pkg
    - pkg_resource
    - postfix
    - publish
    - pyenv
    - raid
    - random
    - random_org
    - rbenv
    - ret
    - rsync
    - runit
    - rvm
    - s3
    - saltutil
    - schedule
    - scsi
    - sdb
    - seed
    - serverdensity_device
    - service
    - shadow
    - slack
    - smtp
    - sqlite3
    - ssh
    - state
    - status
    - supervisord
    - sys
    - sysctl
    - syslog_ng
    - system
    - test
    - timezone
    - user
    - vbox_guest
    - virtualenv
    - webutil
    - xfs

所有主机saltstack支持的模块清单(打印部分)

二 模块及api

import salt.client
client = salt.client.LocalClient()
ret = client.cmd('*','test.ping')
print ret

 

(1) Archive模块

1)功能:实现系统层面的压压缩包调用,支持gunzip,gzip,rar,tar,unrar,unzip等。

 

2)示例:

 

 

 

(2)cmd模块

1)功能:实现远程的命令行调用执行(默认具备root操作权限,使用时需要评估风险)

2)示例:

#获取所有被控主机的内存使用情况
[root@hzbj-salt-020 ~]# salt '*' cmd.run "free -m"
hzbj-tomcat-022:
                 total       used       free     shared    buffers     cached
    Mem:           980        715        264          0         55        476
    -/+ buffers/cache:        184        796
    Swap:         1983          0       1983
hzbj-tomcat-021:
                 total       used       free     shared    buffers     cached
    Mem:           980        884         95          0         25        206
    -/+ buffers/cache:        652        327
    Swap:         1983         11       1972

#在hzbj-tomcat-022主机运行test.sh脚本,其中scripts/test.sh存放在file_roots指定的目录,
#改命令会做两个动作:首选同步test.sh到minion的cache目录(如同步到/var/cache/salt/ #minion/files/base/script/test.sh):其次运行改脚本
[root@hzbj-salt-020 ~]# salt 'hzbj-tomcat-01' cmd.script salt://script/test.sh

 

3)API调用

client.cmd('hzbj-tomcat-021','cmd.run',['free -m'])

 

(3)cp模块

1)功能:实现远程文件,目录的复制,以及下载URL文件等操作

 

2)示例:

 将注服务器file_roots指定位置下的目录复制到被控主机
 salt '*' cp.get_dir salt://path/to/dir /minion/dest
 
 将主服务器file_roots指定文职下的文件复制到被控主机
 salt '*' cp.get_file salt://path/to/file /minion/dest
 
下载URL内容到被控主机指定位置
salt '*' cp.get_url http://www.salshdot.org /tmp/index.html

 

3)API调用

client.cmd('hzbj-tomcat-021','cp.get_file',['salt://path/to/file','/minion/dest'])

 

(4)cron模块

1)功能:实现被控主机的crontab操作

2)示例

 查看指定被控主机,root用户的crontab清单
 [root@hzbj-salt-020 salt]# salt 'hzbj-tomcat-021' cron.raw_cron root
hzbj-tomcat-021:
    */2 * * * * ntpdate pool.ntp.org &>/dev/null
    */1 * * * * cd /usr/local/gse/; ./cron_gse.sh 1>/dev/null 2>&1

#指定的被控主机,root用户添加/usr/local/weekly任务作业
[root@hzbj-salt-020 salt]# salt 'hzbj-tomcat-021' cron.raw_cron root
hzbj-tomcat-021:
    */2 * * * * ntpdate pool.ntp.org &>/dev/null
    */1 * * * * cd /usr/local/gse/; ./cron_gse.sh 1>/dev/null 2>&1
    # Lines below here are managed by Salt, do not edit
    * * * * 1 /usr/local/weekly

#删除指定的被控主机,root用户添加/usr/local/weekly任务作业
[root@hzbj-salt-020 salt]# salt 'hzbj-tomcat-021' cron.rm_job root /usr/local/weekly
hzbj-tomcat-021:
    removed

注解: 如果需要写入sls文件里面

/usr/sbin/ntpdate pool.ntp.org >>/dev/null 2>&1:
  cron.present:
    - user: root
    - minute: 10
minute      #分钟
hour        #小时
daymonth    #日
month       #月
dayweek     #0~6是周一到周六,周日是7

   默认执行用户“root”,时间不填写默认是“*,周期执行“*/2,随机执行“random”,identifier    是任务标识符,类似于任务ID,更改任务时指定“标识符”可方便修改

 

3)API调用

client.cmd('hzbj-tomcat-021','cron.set_job',['root','*','*','*','*','*','/usr/echo'])

 

(5)dnsutil模块

1)功能:实现被控主机通用DNS相关操作。

 

2)示例:

[root@hzbj-salt-020 ~]# salt '*' dnsutil.hosts_append /etc/hosts 127.0.0.1 ad1.yuk.com

删除指定被控主机hosts的主机批准
[root@hzbj-salt-020 ~]# salt '*' dnsutil.hosts_remove /etc/hosts ad1.yuk.com

 

3)API调用

client.cmd('hzbj-tomcat-021','dnsutil.hosts_append',['/etc/hosts','127.0.01','ad1.yuk.co'])

 

(6)file模块

1)功能:被控主机文件操作,包括文件读写,权限,查找,校验等。

 

 

 

 

(9)pkg包管理模块

1)功能:被控主机程序包管理,如yum,apt-get

 

2)示例:

#为所有被控主机安装PHP环境,根据不同系统发现版调用不同安装工具进行部署,如redhat
平台的yum,等价于yum -y install php
salt '*' pkg.install php
#卸载所有被控主机的PHP
salt '*' pkg.remove php
#升级所有被控主机的软件包
salt '*' pkg.upgrade

 

3)API调用

client.cmd('hzbj-tomcat-021','pkg.remove',['php'])

 

(10)Service服务模块

1)功能:被控主机程序包服务管理

 

2)示例:

#开启(enable),禁用(disable)nginx开机自启动服务

salt '*' service.enable nginx
salt '*' service.disable nginx

#针对nginx服务的reload,restart,start,stop,ss操作
salt '*' service.reload nginx
salt '*' service.restart nginx
salt '*' service.start nginx
salt '*' service.stop nginx
salt '*' service.status nginx

3)API调用

client.cmd('hzbj-tomcat-021','service.stop',['nginx'])

 

(11) User模块

user.present

user.present: 确保指定的账户名存在,并指定其对应的属性. 这些属性包括如下内容:

name: 指定需要管理的账户名.

uid: 指定uid, 如果不设置将配自动分配下一个有效的uid.

gid: 指定默认的组id(group id)

gid_from_name: 如果设置为_True_,默认的组id将自动设置为和本用户同名的组id

groups: 分配给该用户的组列表(a list of groups). 如果组在minion上不存在,则本state会报错. 如果设置会空,将会删除本用户所属的除了默认组之外的其他组

optional_groups: 分配给用户的组列表。 如果组在minion上不存在,则state会忽略它.

home: 关于用户的家目录(home directory).

password: 设置用户hash之后的密码.

enforce_password: 当设置为_False_时,如果设置的_password_与用户原密码不同,将保持原密码不做更改.如果没有设置_password_选项,该选项将自动忽略掉.

shell: 指定用户的login shell。 默认将设置为系统默认shell。

unique: UID唯一,默认为True.

system: 从_FIRST_SYSTEM_UID_和_LAST_SYSTEM_UID_间选择一个随机的UID.

 user.absent

本部分为译者依据官方手册进行的补充,原文中并没有相关内容
user.absent 用于删除用户.其有以下选项:

name: 指定需要删除的用户名.

purge: 设置清除用户的文件(家目录)

force: 如果用户当前已登录,则absent state会失败. 设置_force_选项为True时,就算用户当前处于登录状态也会删除本用户.

当管理用户时,至少需要指定_user.present_或_user.absent_。 其他选项是可选的,比如_uid_、gid、_home_等选项没有指定是,将自动使用下一个有效的或者系统默认的.

 

例子:

如果需要设置密码 首选hash密码

openssl passwd -1 -salt 'cedwards'

 

创建用户

# cat cedwards.sls 
cedwards:
  user.present:
    - fullname: Christer Edwards
    - password: '$1$cedward$KcHcszCMgNJkDwSID7QRH.' 
    - shell: /bin/bash
    - home: /home/cedwards
    - uid: 1002
    - gid: 1002
    - groups:
      - wheel

 

删除用户

# cat userdel.sls 
cedwards:
  user.absent:
    - purge: Ture
    - force: Ture

 

批量创建用户

# cat useradd.sls 
{% set users = ['jim','hwg','hyxc'] %}
{% for user in users %}
{{ user }}:
  user.present:
    - shell: /bin/bash
    - home: /home/{{ user }}
    - password: '$1$cedwards$Sy0q724HkzgJsIWMsQIkq.'
{% endfor %}

 

posted @ 2016-11-16 12:47  叶小宠  阅读(1961)  评论(0编辑  收藏  举报