实验目标
简单部署tomcat及安装java环境
实现步骤
编写salt的状态模块
[root@linux-node1 web]# pwd /srv/salt/base/web [root@linux-node1 web]# cat tomcat.sls jdk-install: pkg.installed: - name: java-1.8.0-openjdk #rpm包安装不需要添加环境变量 tomcat-install: file.managed: - name: /usr/local/src/apache-tomcat-8.0.46.tar.gz - source: salt://web/files/apache-tomcat-8.0.46.tar.gz - user: root - group: root - mode: 755 cmd.run: - name: cd /usr/local/src && tar zxf apache-tomcat-8.0.46.tar.gz && mv apache-tomcat-8.0.46 /usr/local/ && ln -s /usr/local/apache-tomcat-8.0.46 /usr/local/tomcat - unless: test -L /usr/local/tomcat && test -d /usr/local/apache-tomcat-8.0.46
上传apache-tomcat-8.0.46到salt的文件管理目录下
[root@linux-node1 src]# ll /srv/salt/base/web/files/apache-tomcat-8.0.46.tar.gz -rw-r--r-- 1 root root 9352747 Oct 16 2017 /srv/salt/base/web/files/apache-tomcat-8.0.46.tar.gz
执行状态模块
[root@linux-node1 web]# salt "linux-node2*" state.sls web.tomcat linux-node2.example.com: ---------- ID: jdk-install Function: pkg.installed Name: java-1.8.0-openjdk Result: True Comment: The following packages were installed/updated: java-1.8.0-openjdk Started: 00:50:55.027164 Duration: 46213.48 ms Changes: ---------- chkconfig: ---------- new: 1.7.4-1.el7 old: 1.3.61-5.el7_2.1 copy-jdk-configs: ---------- new: 2.2-3.el7 old: giflib: ---------- new: 4.1.6-9.el7 old: java-1.8.0-openjdk: ---------- new: 1:1.8.0.144-0.b01.el7_4 old: java-1.8.0-openjdk-headless: ---------- new: 1:1.8.0.144-0.b01.el7_4 old: javapackages-tools: ---------- new: 3.4.1-11.el7 old: libICE: ---------- new: 1.0.9-9.el7 old: libSM: ---------- new: 1.2.2-2.el7 old: libXcomposite: ---------- new: 0.4.4-4.1.el7 old: libXext: ---------- new: 1.3.3-3.el7 old: libXfont: ---------- new: 1.5.2-1.el7 old: libXi: ---------- new: 1.7.9-1.el7 old: libXrender: ---------- new: 0.9.10-1.el7 old: libXtst: ---------- new: 1.2.3-1.el7 old: libfontenc: ---------- new: 1.1.3-3.el7 old: lksctp-tools: ---------- new: 1.0.17-2.el7 old: nspr: ---------- new: 4.13.1-1.0.el7_3 old: 4.11.0-1.el7_2 nss: ---------- new: 3.28.4-12.el7_4 old: 3.21.0-9.el7_2 nss-pem: ---------- new: 1.0.3-4.el7 old: nss-softokn: ---------- new: 3.28.3-8.el7_4 old: 3.16.2.3-14.2.el7_2 nss-softokn-freebl: ---------- new: 3.28.3-8.el7_4 old: 3.16.2.3-14.2.el7_2 nss-sysinit: ---------- new: 3.28.4-12.el7_4 old: 3.21.0-9.el7_2 nss-tools: ---------- new: 3.28.4-12.el7_4 old: 3.21.0-9.el7_2 nss-util: ---------- new: 3.28.4-3.el7 old: 3.21.0-2.2.el7_2 python-javapackages: ---------- new: 3.4.1-11.el7 old: python-lxml: ---------- new: 3.2.1-4.el7 old: ttmkfdir: ---------- new: 3.0.9-42.el7 old: tzdata-java: ---------- new: 2017b-1.el7 old: xorg-x11-font-utils: ---------- new: 1:7.5-20.el7 old: xorg-x11-fonts-Type1: ---------- new: 7.5-9.el7 old: ---------- ID: tomcat-install Function: file.managed Name: /usr/local/src/apache-tomcat-8.0.46.tar.gz Result: True Comment: File /usr/local/src/apache-tomcat-8.0.46.tar.gz updated Started: 00:51:41.243020 Duration: 496.457 ms Changes: ---------- diff: New file mode: 0755 ---------- ID: tomcat-install Function: cmd.run Name: cd /usr/local/src && tar zxf apache-tomcat-8.0.46.tar.gz && mv apache-tomcat-8.0.46 /usr/local/ && ln -s /usr/local/apache-tomcat-8.0.46 /usr/local/tomcat Result: True Comment: Command "cd /usr/local/src && tar zxf apache-tomcat-8.0.46.tar.gz && mv apache-tomcat-8.0.46 /usr/local/ && ln -s /usr/local/apache-tomcat-8.0.46 /usr/local/tomcat" run Started: 00:51:41.740160 Duration: 1288.003 ms Changes: ---------- pid: 9742 retcode: 0 stderr: stdout: Summary ------------ Succeeded: 3 (changed=3) Failed: 0 ------------ Total states run: 3
总结
1.添加状态模块的时候,应该自己得先在别的机器上面跑一遍流程在添加
2.状态模块中- source: salt://web/files/apache-conf.d #支持ftp或者http上面 base环境的路径
3.使用的时候要先测试 test=True
4.ID 有不同表示方式
5.状态模块需要执行模块执行才能生效
6.相同的业务用目录进行分类管理
7.sls文件是从上往下按照顺序执行
8.一个ID下面一个状态只能出现一次
9.pkg是虚拟的包管理,在不同系统下面包的安装命令不同 比如 CentOS 和Ubuntu
10.watch 和watch_in require和require_in 在状态模块中可以统一用其中的一种
11.salt默认是有缓存的,可以无关紧要的配置重启进程刷新缓存
salt任务执行过程中不删除缓存目录里面的文件 缓存目录默认为/var/cache/salt
附 https://github.com/unixhot/saltbook-code/tree/master