安装软件包:
jdk-install: pkg.installed: - names: - java-1.8.0-openjdk
配置管理:
apache-conf: file.managed: - name: /etc/httpd/conf/httpd.conf - source: salt://files/httpd.conf
管理服务启动:
apache-service: service.running: - name: httpd - enable: True - reload: True - watch: - file: apache-conf
计划任务:
cron_add: cron.present: - name: /bin/touch /tmp/test.txt - user: root - minute: '*' - hour: '*' - daymonth: '1' - month: '1' - dayweek: '*' cron_del: cron.absent: - name: /bin/touch /tmp/test.txt
state文件中使用Jinja模版:
# cat /salt/service/zookeeper/files/myid {{ hostname }} zookeeper-id: file.managed: - source: salt://service/zookeeper/files/myid - name: /usr/local/zookeeper/data/myid - template: jinja - defaults: {% if grains['id'] == 'master2' %} hostname: 1 {% elif grains['id'] == 'master3' %} hostname: 2 {% endif %}
向文件中追加内容:
env-path: file.append: - name: /etc/profile - text: - export STORM_HOME=/usr/local/apache-storm - export ZOOKEEPER_HOME=/usr/local/zookeeper - export PATH=$ZOOKEEPER_HOME/bin:$STORM_DIR/bin/:$PATH
修改/etc/sysctl.conf文件:
net.ipv4.ip_nonlocal_bind: sysctl.present: - value: 1
state文件中设置变量:
{% set keepalived_tar = 'keepalived-1.2.17.tar.gz' %} keepalived-install: file.managed: - name: /usr/local/src/{{ keepalived_tar }} - source: salt://service/keepalived/files/{{ keepalived_tar }} - mode: 755 - user: root - group: root
创建用户:
#在命令行生成密码作为password的值: openssl passwd -1 -salt '用户名' Jane: user.present: - shell: /bin/bash - uid: 600 - gid: 600 - gid_from_name: True - password: $1$Jane$A0GJ5mLtd4Ze3gm/0MYcB0 - createhome: True
批量添加用户:
{% set users = ['name1,name2'] %} {% for user in users %} {{ user }}: user.present: - shell: /bin/bash - home: /home/{{ user }} - password: ‘$1$Jane$A0GJ5mLtd4Ze3gm/0MYcB0’ group.present: - name: {{ user }} {% endfor %}
命令行:
# salt '*' cp.get_file salt://service/zookeeper/files/myid /usr/local/zookeeper/data ---- 将主服务器指定位置下的文件复制到minion # salt '*' cp.get_dir salt://serivce/zookeeper/data /usr/local/zookeeper/data ---- 将主服务器指定目录复制到minion # salt '*' cp.get_url http://www.baidu.com/.../....tar.gz /root/....tar.gz ---- 从指定url下载文件到minion指定位置 # salt '*' cron.raw_cron root ---- 查看root用户下的定时任务 # salt '*' cron.set_job root '*/30' '*' '*' '*' '*' 'date > /dev/null 2>&1' ---- 为root用户设置定时任务 # salt '*' cron.rm_job root 'date > /dev/null 2>&1' ---- 删除root用户的某个定时任务 # salt '*' saltutil.refresh_pillar ---- 刷新pillar # salt '*' saltutil.sync_grains ---- 刷新grains
获取帮助:
针对管理对象的操作:module # salt 'minion' sys.list_modules ---- 查看所有module列表 # salt 'minion' sys.list_functions cmd ---- 查看指定module(在此处为cmd)的所有function # salt 'minion' sys.doc cmd ---- 查看指定module用法 # salt 'minion' sys.doc cmd.run ---- 查看指定module,指定function的用法 配置管理:states # salt 'minion' sys.list_state_modules ---- 查看所有states列表 # salt 'minion' sys.list_state_functions file ---- 查看指定state(在此处为file)的所有function # salt 'minion' sys.state_doc file ---- 查看指定state用法 # salt 'minion' sys.state_doc file.managed ---- 查看指定state,指定function用法