介紹一些常用的saltstack模組,更多模組參考官方網站
1、跟安裝包相關的模組:salt.states.pkg
salt.states.pkg.downloaded(name, version=None, pkgs=None, fromrepo=None, ignore_epoch=None, **kwargs)
當同時使用了name,pkgs那麼name將會被忽略
salt.states.pkg.installed(name, version=None, refresh=None, fromrepo=None, skip_verify=False, skip_suggestions=False, pkgs=None, sources=None, allow_updates=False, pkg_verify=False, normalize=True, ignore_epoch=False, reinstall=False, update_holds=False, **kwargs)
salt.states.pkg.removed(name, version=None, pkgs=None, normalize=True, ignore_epoch=False, **kwargs)
removed解除安裝某包
2、遠端執行命令的模組
salt.states.cmd.run(name, onlyif=None, unless=None, creates=None, cwd=None, runas=None, shell=None, env=None, stateful=False, umask=None, output_loglevel='debug', quiet=False, timeout=None, ignore_timeout=False, use_vt=False, **kwargs)
name:The command to execute, remember that the command will execute with the path and permissions of the salt-minion.
cwd:The current working directory to execute the command in, defaults to /root
unless:當執行的命令為false時,那麼才執行name中的命令
require:執行name中命令的依賴關係:
pkg:必須安裝了python包
file:該file檔案路徑下有這個py檔案
3、檔案管理操作方面的模組
salt.states.file.managed(name, source=None, source_hash='`, source_hash_name=None, user=None, group=None, mode=None, template=None, makedirs=False, dir_mode=None, context=None, replace=True, defaults=None, backup='`, show_changes=True, create=True, contents=None, tmp_ext='`, contents_pillar=None, contents_grains=None, contents_newline=True, contents_delimiter=':', encoding=None, encoding_errors='strict', allow_empty=True, follow_symlinks=True, check_cmd=None, skip_verify=False, win_owner=None, win_perms=None, win_deny_perms=None, win_inheritance=True, **kwargs)
file.managed:檔案管理,多用於複製,如果沒有就建立該檔案
salt.states.file.append(name, text=None, makedirs=False, source=None, source_hash=None, template='jinja', sources=None, source_hashes=None, defaults=None, context=None, ignore_whitespace=True)
append-memcache-php: file.append: - name: /usr/local/php/etc/php.ini - text: - extension=memcache.so cmd.run: - name: /etc/init.d/php-fpm restart
file.append:向某檔案中新增內容text=內容,name為需要新增的檔案
salt.states.file.directory(name, user=None, group=None, recurse=None, max_depth=None, dir_mode=None, file_mode=None, makedirs=False, clean=False, require=None, exclude_pat=None, follow_symlinks=False, force=False, backupname=None, allow_symlink=True, children_only=False, win_owner=None, win_perms=None, win_deny_perms=None, win_inheritance=True, **kwargs)
file.directory:建立一個檔案目錄
/etc/keepalived: file.directory: - user: root - group: root - mode: 755
4、跟系統服務相關:state.service
salt.states.service.running(name, enable=None, sig=None, init_delay=None, no_block=False, unmask=False, unmask_runtime=False, **kwargs)
service.running:開啟系統某服務enable為新增到自啟動,name表示該啟動的服務名稱
service.running: - name: nginx - enable: True - reload: True - require: - file: /etc/init.d/nginx - cmd: nginx-init - watch: - file: /usr/local/nginx/conf/nginx.conf
5、跟系統核心相關的設定:
salt.states.sysctl.present(name, value, config=None) Ensurethatthenamedsysctlvalueissetinmemoryandpersistedtothenamedconfigurationfile. Thedefault sysctl configuration file is /etc/sysctl.conf name The name of the sysctl value to edit value The sysctl value to apply config Thelocationofthesysctlconfigurationfile. Ifnotspecified,theproperlocationwillbedetectedbased on platform.
sysctl.present:修改系統核心引數
[root@node1 init]# cat sysctl.sls vm.swappiness: sysctl.present: - value: 0 net.ipv4.ip_local_port_range: sysctl.present: - value: 10000 61000 fs.file-max: sysctl.present: - value: 186981
6、建立使用者user和group用到的模組
www-user-group: group.present: - name: www - gid: 1000 user.present: - name: www - fullname: www - shell: /sbin/nologin - uid: 1000 - gid: 1000
7、狀態配置中require、unless詳解:
[root@node1 base]# cat if_unless.sls getshell: file.managed: - name: /tmp/echo.sh - source: salt://init/files/echo.sh cmd.run: - name: /bin/bash /tmp/echo.sh - unless: test -f /tmp/if_unless.sh - require: - file: /tmp/echo.sh
詳解:
require:require要求下面的file必須事先經過了file.managed進行了設定,才能使用作為依賴條件
unless:測試tmp下面是否有if_unless.sh這個檔案,如果測試成功有那麼不會再執行,如果測試命令返回的code不是0那麼才會執行
unless:一行表示,如果該檔案存在,就不需要再執行name中的命令了(包如果安裝了就需要再安裝)
8、使用template: jinja來定義變數
zabbix-agent-service: file.managed: - name: /etc/zabbix/zabbix_agentd.conf - source: salt://init/files/zabbix_agentd.conf - template: jinja - defaults: Server: {{ pillar['zabbix-agent']['Zabbix_Server'] }}
在該salt://init/files/zabbix_agentd.conf配置檔案中將Server={{ Server }}引用上面定義的變數即可
或者使用grains和pillar使用變數:
keepalived-service: file.managed: - name: /etc/keepalived/keepalived.conf - source: salt://cluster/files/haproxy-outside-keepalived.conf - user: root - group: root - mode: 644 - template: jinja {% if grains['fqdn'] == 'node1' %} - ROUTEID: haproxy_node1 - STATEID: MASTER - PRIORITYID: 150 {% elif grains['fqdn'] == 'node2' %} - ROUTEID: haproxy_node2 - STATEID: BACKUP - PRIORITYID: 100 {% endif %}