死磕salt系列-salt 常用modules

saltstack 常用模块介绍

file模块

被控主机文件常见操作,包括文件读写、权限、查找、校验等

salt '*' file.get_sum /etc/resolv.conf md5
salt '*' file.stats /etc/resolv.conf

file.managed-文件管理

先来一个实例

/etc/http/conf/http.conf:
  file.managed:
    - source: salt://apache/http.conf
    - user: root
    - group: root
    - mode: 644
    - template: jinja
    - defaults:
        custom_var: "default value"
        other_var: 123
{% if grains['os'] == 'Ubuntu' %}
    - context:
        custom_var: "override"
{% endif %}

source-指定源文件

source参数可以作为一个列表指定。如果这样做的话,那么会使用第一个匹配到的文件。这个特性允许你如果请求的文件不存在于salt文件服务器时有一个默认的文件回溯。

/etc/foo.conf:
  file.managed:
    - source:
      - salt://foo.conf.{{ grains['fqdn'] }}
      - salt://foo.conf.fallback
    - user: foo
    - group: users
    - mode: 644
    - backup: minion

source 参数同样可以指定一个在另一个Salt环境的文件。在这个例子中 foo.conf 将会使用 dev 环境中的来替代。

/etc/foo.conf:
  file.managed:
    - source:
      - salt://foo.conf?saltenv=dev
    - user: foo
    - group: users
    - mode: '0644'

file.directory-目录管理

创建目录

/srv/stuff/substuf:
  file.directory:
    - user: fred
    - group: users
    - mode: 755
    - makedirs: True

使用 dir_mode 和 file_mode,同时指定目录和文件的权限。

/srv/stuff/substuf:
  file.directory:
    - user: fred
    - group: users
    - file_mode: 744
    - dir_mode: 755
    - makedirs: True
    - recurse:
      - user
      - group
      - mode

file.recurse - 递归目录

同步目录

  file.recurse:
    - name: /data/app/prometheus/rules
    - source: salt://prometheus/files/rules
    - user: prometheus
    - group: prometheus
    - file_mode: 744
    - include_empty: True
    - clean: True
    - require:
      - cmd: prometheus-install
  • - clean: True: 目标路径有,但是源路径没有的,统统删除。
/etc/grub.conf:
  file.symlink:
    - target: /boot/grub/grub.conf

file.append - 文件后面追加内容

/etc/motd:
  file.append:
    - text:
      - Trust no one unless you have eaten much salt with him.
      - "Salt is born of the purest of parents: the sun and the sea."

官方帮助文档:https://docs.saltstack.com/en/latest/ref/states/all/salt.states.file.html

cmd模块

实现远程的命令行调用执行

run-执行命令

salt '*' cmd.run 'ss -lntup'

cmd模块执行windows命令

salt '*' cmd.run 'dir' shell=powershell

getpip:
  cmd.run:
    - name: /usr/bin/python /usr/local/sbin/get-pip.py
    - unless: which pip
    - require:
      - pkg: python
      - file: /usr/local/sbin/get-pip.py
    - reload_modules: True

script-执行脚本

salt://scripts/bar.sh:
  cmd.script:
    - env: "PATH=/some/path:$PATH"

pkg包管理模块

installed - 安装包

php.packages:
  pkg.installed:
    - fromrepo: wheezy-php55
    - pkgs:
      - php5-fpm
      - php5-cli
      - php5-curl
      - bar: '>=1.2.3-4'

也可以指定安装包的位置

mypkgs:
  pkg.installed:
    - sources:
      - foo: salt://rpms/foo.rpm
      - bar: http://somesite.org/bar.rpm
      - baz: ftp://someothersite.org/baz.rpm
      - qux: /minion/path/to/qux.rpm

命令行的方式安装包

salt '*' pkg.install nmap
salt '*' pkg.file_list nmap

service 服务模块

running 配置服务启动

redis:
  service.running:
    - enable: True
    - reload: True
    - watch:
      - pkg: redis

reloadwatch一起使用,watch监控到服务的变化,reload会自动重载服务。

dead 停止服务

redis:
  service.dead:
      - enable: True

确定redis服务已经停止,并加入开机自启动。

shell端执行命令

使sshd加入开机自启动
salt '*' service.enable sshd

使sshd加入开机自启动
salt '*' service.disable sshd

查看sshd的状态
salt '*' service.status sshd

停止sshd服务
salt '*' service.stop sshd

启动sshd服务
salt '*' service.start sshd

重启sshd服务
salt '*' service.restart sshd

重载sshd服务
salt '*' service.reload sshd

useradd用户模块

absent - 删除用户

nginx:
  user.absent:
    - purge: True  
    - force: True

purge: 清楚用户家目录
force: 即使用户登录,也要删除用户(默认用户登录时,清除会失败)。

present - 创建用户

一、添加单个用户:
生成密码

openssl passwd -1 -salt 'nginx'
Password: 
$1$nginx$LrLRyngZUF2qJ8f3a31cN0
nginx: 
  user.present: 
    - fullname: nginx web
    - shell: /bin/bash
    - password: '$1$nginx$LrLRyngZUF2qJ8f3a31cN0'
    - home: /home/nginx
    - uid: 1000
    - gid: 1000
    - groups: 
      - nginx 
    - require:
      - group: nginx
  group.present: 
      - gid: 1000

创建一个不能登录,没有家目录的用户

nginx: 
  user.present: 
    - fullname: nginx web
    - shell: /sbin/nologin
    - password: '$1$nginx$LrLRyngZUF2qJ8f3a31cN0'
    - createhome: False
    - uid: 1000
    - gid: 1000
    - groups: 
      - nginx 
    - require:
      - group: nginx
  group.present: 
      - gid: 1000

用户ID一旦确定不能修改,修改后会报错。
解决办法:

id prometheus 
uid=5050(prometheus) gid=1080(prometheus) groups=1080(prometheus)

usermod -u 1080 prometheus  
groupmod -g 1080 prometheus 

id prometheus 
uid=1080(prometheus) gid=1080(prometheus) groups=1080(prometheus)

chown -R prometheus.prometheus  prometheus*
lrwxrwxrwx  1   5050   5050   39 Nov  3 10:40 prometheus -> /data/app/prometheus-1.8.1.linux-amd64/
drwxrwxr-x  6   5050   5050  147 Nov  6 14:28 prometheus-1.8.1.linux-amd64

gid_from_name: 如果设置为_True_,默认的组id将自动设置为和本用户同名的组id
groups: 分配给该用户的组列表(a list of groups). 如果组在minion上不存在,则本state会报错. 如果设置会空,将会删除本用户所属的除了默认组之外的其他组
optional_groups: 分配给用户的组列表。 如果组在minion上不存在,则state会忽略它.

配合jinja模板批量添加多个用户

{{ set userlist=['big','little','ant'] }}
{% for user in userlist %}
{{ user }}: 
  user.present: 
    - fullname: {{ user }} web
    - shell: /sbin/nologin
    - password: '$1$nginx$LrLRyngZUF2qJ8f3a31cN0'
    - createhome: False
{% endfor %}

cp模块

实现远程文件、目录的复制,以及下载URL文件等操作

将主服务器file_roots指定位置下的目录复制到被控主机
salt 'chuye.backup*' cp.get_dir salt://iis-works/files/ /tmp/

将主服务器file_roots指定位置下的文件复制到被控主机
salt 'chuye.backup*' cp.get_file salt://iis-works/files/web-config /tmp/

下载指定URL内容到被控主机指定位置
salt '*' cp.get_url http://xxx.xyz.com/download/0/files.tgz /root/files.tgz

cron模块

present - 管理crontab

实现被控主机的crontab操作

date > /tmp/crontest:
  cron.present:
    - user: root
    - minute: '*/5'
superscript > /tmp/crontest:
  cron.present:
    - identifier: SUPERCRON
    - user: root
    - minute: 3
    - hour: 4

identifier:文件的标示,默认是状态ID。

file - 为crontab提供类似文件管理的功能

foo_crontab:
  cron.file:
    - name: https://mydomain.tld/dir2/foo.txt
    - source_hash: https://mydomain.tld/hashes
    - source_hash_name: ./dir2/foo.txt

命令行执行

查看指定主机某用户的crontab
salt '*' cron.raw_cron root

为指定的被控主机、root用户添加crontab信息
salt '*' cron.set_job root '*/5' '*' '*' '*' '*' 'date >/dev/null 2>&1'

删除指定的被控主机、root用户的crontab信息
salt '*' cron.rm_job root 'date >/dev/null 2>&1'

dnsutil模块

实现被控主机通用DNS操作

为被控主机添加指定的hosts主机配置项
salt '*' dnsutil.hosts_append /etc/hosts 192.168.56.12 linux-node2

network模块

返回被控主机网络信息
salt '*' network.ip_addrs

salt '*' network.interfaces

参考链接

state modules

execution modules

posted @ 2018-04-08 11:20  biglittleant  阅读(2079)  评论(0编辑  收藏  举报