本文共 7254 字,大约阅读时间需要 24 分钟。
官方文档:
(1)安装salt-ssh[root@linux-node1 ~]# yum install -y salt-ssh(2)配置salt-ssh[root@linux-node1 ~]# vim /etc/salt/roster linux-node1: host: 192.168.56.11 user: root passwd: 123123linux-node2: host: 192.168.56.12 user: root passwd: 123123(3)使用ssh远程执行[root@linux-node1 ~]# salt-ssh '*' -r 'uptime'linux-node2: ---------- retcode: 0 stderr: stdout: root@192.168.56.12's password: 14:07:19 up 14 days, 8:41, 2 users, load average: 0.04, 0.08, 0.07linux-node1: ---------- retcode: 0 stderr: stdout: root@192.168.56.11's password: 14:07:20 up 23 days, 8:13, 2 users, load average: 2.86, 0.81, 0.34
所谓的状态就是希望系统运行某些命令之后的结果。描述状态使用YAML格式的文件。SLS:salt state
举例安装apache,如下:[root@linux-node1 ~]# vim /srv/salt/base/web/apache.sls apache: pkg.installed: - name: httpd service.running: - name: httpd file.managed: - name: /etc/httpd/conf/httpd.conf - source: salt://apache/files/httpd.conf - user: root - group: root - mode: 644解释说明:apache:id声明,在所有环境(base、prod)下全局唯一pkg:状态模块.:引用关系installed:模块中的方法::代表层级关系name:可以理解为参数,后面跟的是参数值file.managed:文件管理模块,必须要有source指定文件的来源路径source:文件的来源路径,salt://代表着环境的根路径,这的根路径为:/srv/salt/base/user、group、mode:分别指定文件的所属者,所属组和权限以上的文件还可以使用分id的写法:apache-install: pkg.installed: - name: httpdapache-service: service.running: - name: httpdapache-config: file.managed: - name: /etc/httpd/conf/httpd.conf - source: salt://apache/files/httpd.conf - user: root - group: root - mode: 644存在指定多个配置文件,还可以使用一下写法:(不适用name作为参数传递时,id就是name)/etc/httpd/conf/httpd.conf: file.managed: - source: salt://apache/files/httpd.conf - user: root - group: root - mode: 644/etc/httpd/conf/php.conf: file.managed: - source: salt://apache/files/php.conf - user: root - group: root - mode: 644
名称 | 软件包 | 配置文件 | 服务 |
---|---|---|---|
使用模块 | pkg | file | service |
LAMP | httpd、php、mariadb、mariadb-server、php-mysql、php-pdo、php-cli | /etc/httpd/conf/httpd.conf、/etc/php.ini | httpd、mysqld |
[root@linux-node1 prod]# pwd/srv/salt/prod[root@linux-node1 prod]# mkdir apache php mysql[root@linux-node1 prod]# tree .├── apache├── mysql└── php3 directories, 0 files[root@linux-node1 prod]# cd apache/[root@linux-node1 apache]# vim apache.sls #编写apache的状态模块apache-install: pkg.installed: - name: httpdapache-config: file.managed: - name: /etc/httpd/conf/httpd.conf - source: salt://apache/files/httpd.conf #salt://代表着环境的根路径 - user: root - group: root - mode: 644apache-service: service.running: - name: httpd - enable: True[root@linux-node1 apache]# mkdir files #创建source目录[root@linux-node1 apache]# cd files/[root@linux-node1 files]# cp /etc/httpd/conf/httpd.conf .[root@linux-node1 apache]# tree .├── apache.sls└── files └── httpd.conf1 directory, 2 files[root@linux-node1 apache]# salt 'linux-node1' state.sls apache.apache saltenv=prod
[root@linux-node1 prod]# cd php[root@linux-node1 php]# mkdir files[root@linux-node1 php]# vim init.slsphp-install: pkg.installed: - pkgs: - php - php-pdo - php-mysqlphp-config: file.managed: - name: /etc/php.ini - source: salt://php/files/php.ini - user: root - group: root - mode: 644[root@linux-node1 php]# cp /etc/php.ini files/[root@linux-node1 php]# tree .├── files│ └── php.ini└── init.sls1 directory, 2 files
[root@linux-node1 prod]# cd mysql/[root@linux-node1 mysql]# vim init.slsmysql-install: pkg.installed: - pkgs: - mariadb - mariadb-servermysql-config: file.managed: - name: /etc/my.cnf - source: salt://mysql/files/my.cnf - user: root - gourp: root - mode: 644mysql-service: service.running: - name: mariadb-server - enable: True[root@linux-node1 mysql]# mkdir files[root@linux-node1 mysql]# cp /etc/my.cnf files/[root@linux-node1 prod]# tree .├── apache│ ├── files│ │ └── httpd.conf│ └── init.sls├── mysql│ ├── files│ │ └── my.cnf│ └── init.sls└── php ├── files │ └── php.ini └── init.sls[root@linux-node1 prod]# salt -S '192.168.56.11' state.sls php.init saltenv=prodlinux-node1.example.com:---------- ID: php-install Function: pkg.installed Result: True Comment: The following packages were installed/updated: php-mysql The following packages were already installed: php-pdo, php Started: 10:30:14.780998 Duration: 118711.436 ms Changes: ---------- php-mysql: ---------- new: 5.4.16-43.el7_4 old:---------- ID: php-config Function: file.managed Name: /etc/php.ini Result: True Comment: File /etc/php.ini is in the correct state Started: 10:32:13.556562 Duration: 51.913 ms Changes: Summary for linux-node1.example.com------------Succeeded: 2 (changed=1)Failed: 0------------Total states run: 2Total run time: 118.763 s
[root@linux-node1 base]# pwd/srv/salt/base[root@linux-node1 base]# vim top.sls prod: 'linux-node1.example.com': - apache.init - php.init - mysql.init[root@linux-node1 base]# salt 'linux-node1*' state.highstatelinux-node1.example.com:---------- ID: apache-install Function: pkg.installed Name: httpd Result: True Comment: All specified packages are already installed Started: 10:39:04.214911 Duration: 762.144 ms Changes: ---------- ID: apache-config Function: file.managed Name: /etc/httpd/conf/httpd.conf Result: True Comment: File /etc/httpd/conf/httpd.conf is in the correct state Started: 10:39:04.979376 Duration: 13.105 ms Changes: ---------- ID: apache-service Function: service.running Name: httpd Result: True Comment: The service httpd is already running Started: 10:39:04.992962 Duration: 36.109 ms Changes: ---------- ID: php-install Function: pkg.installed Result: True Comment: All specified packages are already installed Started: 10:39:05.029241 Duration: 0.65 ms Changes: ---------- ID: php-config Function: file.managed Name: /etc/php.ini Result: True Comment: File /etc/php.ini is in the correct state Started: 10:39:05.029987 Duration: 10.642 ms Changes: ---------- ID: mysql-install Function: pkg.installed Result: True Comment: All specified packages are already installed Started: 10:39:05.040793 Duration: 0.422 ms Changes: ---------- ID: mysql-config Function: file.managed Name: /etc/my.cnf Result: True Comment: File /etc/my.cnf is in the correct state Started: 10:39:05.041301 Duration: 7.869 ms Changes: ---------- ID: mysql-service Function: service.running Name: mariadb Result: True Comment: The service mariadb is already running Started: 10:39:05.049284 Duration: 28.054 ms Changes: Summary for linux-node1.example.com------------Succeeded: 8Failed: 0------------Total states run: 8Total run time: 858.995 ms
转载于:https://blog.51cto.com/jinlong/2062769