草庐IT

0.16版本salt的安装与日常应用

dl528888 2023-03-28 原文
现在分享一下我对0.16版本salt的学习笔记

一、安装

1、在ubuntu系统安装

添加源

debhttp://debian.saltstack.com/debian wheezy-saltstack main

把这个源放到/etc/apt/sources.list

更新源

apt-get update

安装服务端或者客户端

apt-get installsalt-master

apt-get installsalt-minion

2、redhat或者centos的话,安装使用需要安装第三个源

如果你是5版本

wget http://dl.cpis-opt.com/huanw/shencan/epel-release-5-4.noarch.rpm && rpm -vih epel-release-5-4.noarch.rpm

如果是6版本,使用

wget http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm yum install salt-master yum install salt-minionThe Salt master communicates with the minions using an AES-encrypted ZeroMQ connection. These communications are done over ports 4505 and 4506, which need to be accessible on the master only. This document outlines suggested firewall rules for allowing these incoming connections to the master. Note No firewall configuration needs to be done on Salt minions. These changes refer to the master only.二、配置

在master端配置

vim /etc/salt/master interface: 192.168.56.102写成你本机的ip

在slave端配置

vim /etc/salt/minion master: 192.168.56.102写你服务端的ip

id: localhost是注明自己的标示。

客户端/etc/init.d/salt-minion start 日志文件默认是这个/var/log/salt/minion

服务端/etc/init.d/salt-master start 日志文件默认是这个/var/log/salt/master

[root@centos salt]# salt-key -L Accepted Keys: server.hadoop.com Unaccepted Keys: localhost Rejected Keys:查看你的key情况

同意加入localhost

[root@centos salt]# salt-key -L Accepted Keys: server.hadoop.com Unaccepted Keys: localhost Rejected Keys: [root@centos salt]# salt-key -a localhost Key for minion localhost accepted. [root@centos salt]# salt-key -L Accepted Keys: localhost server.hadoop.com Unaccepted Keys: Rejected Keys:查看一下网络连接情况(也就是看看能否连接客户端)

[root@centos salt]# salt '*' test.ping localhost: True server.hadoop.com: True先前使用*代表所有机器,如果想单独的话,可以使用

[root@centos salt]# salt 'localhost' cmd.run hostname localhost: centos如果想运行多个的话,可以使用-L

[root@centos salt]# salt -L 'server.hadoop.com,localhost' cmd.run hostname server.hadoop.com: server.hadoop.com localhost: centos还可以使用正则

[root@centos salt]# salt -E 'server*' cmd.run hostname server.hadoop.com: server.hadoop.com-G 这个参数很强大会根据默认的grain的结果来指定最新 grain这个东西就像puppet里面的facter这个东西

[root@centos salt]# salt -G 'os:Centos' test.ping localhost: True [root@centos salt]# salt -G 'os:Ubuntu' test.ping server.hadoop.com: True如果想查看哪个项的话

[root@centos test]# salt '*' grains.item os server.hadoop.com: os: Ubuntu localhost: os: CentOS执行python代码

[root@centos salt]# salt '*' cmd.exec_code python 'import sys;print sys.version' localhost: 2.6.6 (r266:84292, Feb 22 2013, 00:00:18) [GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] server.hadoop.com: 2.7.3 (default, Aug 1 2012, 05:14:39) [GCC 4.6.3]分组操作

master里配置

nodegroups: group1: 'localhost' group2: 'server.hadoop.com'可以把localhost分给group1,server.hadoop.com分给group2

然后重启salt-master

[root@centos salt]# salt -N group1 test.ping localhost: True [root@centos salt]# salt -N group2 test.ping server.hadoop.com: True查看网卡ip

[root@centos /]# salt 'localhost' network.interfaces localhost: ---------- eth0: ---------- hwaddr: 08:00:27:59:bb:1f inet: ---------- - address: 192.168.56.102 - broadcast: 192.168.56.255 - label: eth0 - netmask: 255.255.255.0 inet6: ---------- - address: fe80::a00:27ff:fe59:bb1f - prefixlen: 64 up: True eth1: ---------- hwaddr: 08:00:27:ba:ad:23 inet: ---------- - address: 192.168.14.182 - broadcast: 192.168.14.255 - label: eth1 - netmask: 255.255.255.0 inet6: ---------- - address: fe80::a00:27ff:feba:ad23 - prefixlen: 64 up: True lo: ---------- hwaddr: 00:00:00:00:00:00 inet: ---------- - address: 127.0.0.1 - broadcast: None - label: lo - netmask: 255.0.0.0 inet6: ---------- - address: ::1 - prefixlen: 128 up: True

下面是我的测试

[root@centos salt]# salt -C 'G@os:ubuntu' test.ping server.hadoop.com: True [root@centos salt]# salt -C 'E@server.\w+' test.ping server.hadoop.com: True [root@centos salt]# salt -C 'P@os:(centos)' test.ping localhost: True [root@centos salt]# salt -C 'P@os:(centos|ubuntu)' test.ping server.hadoop.com: True localhost: True [root@centos salt]# salt -C 'L@localhost,server.hadoop.com' test.ping server.hadoop.com: True localhost: True [root@centos salt]# salt -C 'S@192.168.56.0/24' test.ping server.hadoop.com: True localhost: True查看磁盘空间

[root@centos tmp]# salt 'localhost' disk.usage localhost: ---------- /: ---------- 1K-blocks: 28423176 available: 21572708 capacity: 21% filesystem: /dev/mapper/vg_centos-lv_root used: 5406628 /boot: ---------- 1K-blocks: 495844 available: 438658 capacity: 7% filesystem: /dev/sda1 used: 31586 /dev/shm: ---------- 1K-blocks: 510204 available: 510204 capacity: 0% filesystem: tmpfs used: 0 [root@centos tmp]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg_centos-lv_root 28G 5.2G 21G 21% / tmpfs 499M 0 499M 0% /dev/shm /dev/sda1 485M 31M 429M 7% /boot如果想查看所有minion的连接情况,可以使用salt-run manage.status

[root@centos apache]# salt '*' test.ping server.hadoop.com: True localhost: True [root@centos apache]# [root@centos apache]# [root@centos apache]# salt-run manage.status down: - 230 up: - localhost - server.hadoop.com如果想安装软件可以使用pkg.install

[root@centos echoping]# salt 'localhost' pkg.install dos2unix Loaded plugins: fastestmirror Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirror.esocc.com * epel: mirrors.vinahost.vn * extras: mirror.esocc.com * rpmforge: mirror1.hs-esslingen.de * updates: centosc6.centos.org Running rpm_check_debug Loaded plugins: fastestmirror localhost: ---------- dos2unix: ---------- new: 3.1-37.el6 old: [root@centos echoping]# rpm -qa|grep dos2unix dos2unix-3.1-37.el6.x86_64查看你已经安装的包

salt 'localhost' pkg.list_pkgs删除包

[root@centos tmp]# salt 'localhost' pkg.remove echoping Loaded plugins: fastestmirror Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirror.esocc.com * epel: mirror.neu.edu.cn * extras: mirror.esocc.com * rpmforge: mirrors.neusoft.edu.cn * updates: mirror.esocc.com Running rpm_check_debug Loaded plugins: fastestmirror localhost: - echoping [root@centos tmp]# rpm -qa|grep echoping查看你repos(由于内容过多,我就只列出命令了)

salt 'localhost' pkg.list_repos三、配置类似puppet操作

由于我的master上的存放信息目录是在/var/salt上

file_roots: base: - /var/salt/所以进入换个目录

下面是我的配置

[root@centos salt]# cat top.sls base: '*': #对象名,我使用*代表所有 - vim #资源文件名如果你的资源文件存放在一个目录里,比如在/var/salt/apache/vim.sls,

那么可以写为

-apache.vim代表apache目录下的vim.sls

下面测试

Top里内容为

[root@centos salt]# cat top.sls base: 'localhost': - echoping.echoping#代表echoping目录下的echoping.sls文件 [root@centos echoping]# pwd /var/salt/echoping [root@centos echoping]# cat echoping.sls echoping: #id宣告 pkg: #安装包管理 - name: echoping #安装哪个软件 - installed #要求是安装 service: #服务管理 - name: httpd #指定服务 - running #服务运行状态 - reload: True #是否重启 - watch: #如果下面文件发生变化,就重启 - file: /tmp/test_echoping.conf #监控的文件地址 /tmp/test_echoping.conf: #宣告 file.managed: #文件管理 - source: salt://echoping/test_echoping.conf #源数据在哪里 - user: root #用户 - group: root #组 - mode: 644 #权限 - backup: minion #备份一份运行的话,可以使用salt 'localhost' state.highstate

注意,如果需要把服务设置为开机启动可以使用- enable:True

由于我设置的是有变化就重启http,所以先查看http的状态

[root@centos salt]# ps -ef|grep httpd root 1430 1 0 17:03 ? 00:00:00 /usr/sbin/httpd apache 1436 1430 0 17:03 ? 00:00:00 /usr/sbin/httpd apache 1469 1430 0 17:03 ? 00:00:00 /usr/sbin/httpd apache 1470 1430 0 17:03 ? 00:00:00 /usr/sbin/httpd apache 1471 1430 0 17:03 ? 00:00:00 /usr/sbin/httpd apache 1472 1430 0 17:03 ? 00:00:00 /usr/sbin/httpd apache 1473 1430 0 17:03 ? 00:00:00 /usr/sbin/httpd apache 1474 1430 0 17:03 ? 00:00:00 /usr/sbin/httpd apache 1475 1430 0 17:03 ? 00:00:00 /usr/sbin/httpd apache 1476 1430 0 17:03 ? 00:00:00 /usr/sbin/httpd root 1886 1782 0 17:04 pts/0 00:00:00 grep httpd [root@centos salt]# date Fri Aug 9 17:04:54 CST 2013在17:04启动,然后在运行salt 'localhost' state.highstate

[root@centos salt]# salt 'localhost' state.highstate Info: Running a benchmark to measure system clock frequency... Info: Finished RDTSC test. To prevent the startup delay from this benchmark, set the environment variable RDTSC_FREQUENCY to 2495 on this system. This value is dependent upon the CPU clock speed and architecture and should be determined separately for each server. localhost: ---------- State: - file Name: /tmp/test_echoping.conf Function: managed Result: True Comment: File /tmp/test_echoping.conf updated Changes: diff: New file ---------- State: - pkg Name: echoping Function: installed Result: True Comment: The following packages were installed/updated: echoping. Changes: echoping: { new : 5.2.0-1.2.el6.rf old : } ---------- State: - service Name: httpd Function: running Result: True Comment: Service restarted Changes: httpd: True可以看到已经安装了echoping,并且更新了/tmp/test_echoping.conf

在查看一下httpd情况

[root@centos salt]# ps -ef|grep httpd root 2025 1 0 17:06 ? 00:00:00 /usr/sbin/httpd apache 2028 2025 0 17:06 ? 00:00:00 /usr/sbin/httpd apache 2031 2025 0 17:06 ? 00:00:00 /usr/sbin/httpd apache 2032 2025 0 17:06 ? 00:00:00 /usr/sbin/httpd apache 2033 2025 0 17:06 ? 00:00:00 /usr/sbin/httpd apache 2034 2025 0 17:06 ? 00:00:00 /usr/sbin/httpd apache 2035 2025 0 17:06 ? 00:00:00 /usr/sbin/httpd apache 2036 2025 0 17:06 ? 00:00:00 /usr/sbin/httpd apache 2037 2025 0 17:06 ? 00:00:00 /usr/sbin/httpd apache 2038 2025 0 17:06 ? 00:00:00 /usr/sbin/httpd root 2043 1782 3 17:06 pts/0 00:00:00 grep httpd [root@centos salt]# date Fri Aug 9 17:06:57 CST 2013可以看到已经重启了。

在查看一下文件传输情况

源文件

[root@centos salt]# cat /var/salt/echoping/test_echoping.conf this is test echoping this twice test生成的文件

[root@centos salt]# cat /tmp/test_echoping.conf this is test echoping this twice test查看echoping是否安装

[root@centos salt]# rpm -qa|grep echoping echoping-5.2.0-1.2.el6.rf.x86_64已经安装了

在看看下面的用户与权限

[root@centos salt]# ll /tmp/test_echoping.conf -rw-r--r-- 1 root root 38 Aug 9 17:05 /tmp/test_echoping.conf也是我们定义的

如果在给/var/salt/echoping/test_echoping.conf修改了,在运行

[root@centos echoping]# salt 'localhost' state.highstate localhost: ---------- State: - file Name: /tmp/test_echoping.conf Function: managed Result: True Comment: File /tmp/test_echoping.conf updated Changes: diff: --- +++ @@ -1,2 +1,3 @@ this is test echoping this twice test +this is 3 ---------- State: - service Name: httpd Function: running Result: True Comment: Service restarted Changes: httpd: True然后服务也重启了

[root@centos echoping]# ps -ef|grep httpd root 2352 1 0 17:21 ? 00:00:00 /usr/sbin/httpd apache 2354 2352 0 17:21 ? 00:00:00 /usr/sbin/httpd apache 2355 2352 0 17:21 ? 00:00:00 /usr/sbin/httpd apache 2356 2352 0 17:21 ? 00:00:00 /usr/sbin/httpd apache 2357 2352 0 17:21 ? 00:00:00 /usr/sbin/httpd apache 2358 2352 0 17:21 ? 00:00:00 /usr/sbin/httpd apache 2359 2352 0 17:21 ? 00:00:00 /usr/sbin/httpd apache 2360 2352 0 17:21 ? 00:00:00 /usr/sbin/httpd apache 2361 2352 0 17:21 ? 00:00:00 /usr/sbin/httpd apache 2362 2352 0 17:21 ? 00:00:00 /usr/sbin/httpd root 2372 2183 0 17:22 pts/1 00:00:00 grep httpd [root@centos echoping]# date Fri Aug 9 17:23:01 CST 2013如果想让salt能想puppet那样定时自动的获取配置,可以在/etc/salt/minion里配置

schedule: highstate: function: state.highstate minutes: 60然后重启salt-minion

请注意,在服务端可以使用salt 'localhost' state.highstate,在客户端的话,使用salt-callstate.highstate

如果使用grains来区分不同的系统安装不同的东东,可以使用下面(比如安装apache,在centos里安装httpd,在ubuntu里安装apache2)

[root@centos apache]# cat apache.sls apache: pkg: {% if grains['os'] == 'CentOS'%} - name: httpd {% elif grains['os'] == 'Ubuntu'%} - name: apache2 {% endif %} - installed service: {% if grains['os'] == 'CentOS'%} - name: httpd {% elif grains['os'] == 'Ubuntu'%} - name: apache2 {% endif %} - running - reload: True - watch: - pkg: apache - file: /tmp/test.conf /tmp/test.conf: file.managed: - source: salt://apache/test.conf - user: root - group: root - mode: 644 [root@centos apache]# cat test.conf this is test apache this is 2然后运行更新

[root@centos apache]# salt 'server.hadoop.com' state.highstate server.hadoop.com: ---------- State: - file Name: /tmp/test.conf Function: managed Result: True Comment: File /tmp/test.conf updated Changes: diff: New file ---------- State: - pkg Name: apache2 Function: installed Result: True Comment: Package apache2 is already installed Changes: ---------- State: - service Name: apache2 Function: running Result: True Comment: Service restarted Changes: apache2: True

注意,如果你想使用命令的话,可以使用cmd.wait

echo-msg: cmd.wait: - name: echo 'this is test' >/tmp/echo-msg - user: root - watch: - pkg: apache自定义模块

查看你master上的file_root路径,比如我的为

file_roots: base: - /var/salt/所以在/var/salt里创建个_modules

mkdir /var/salt/_modules然后进入目录编写模块

cd /var/salt/_modules [root@centos _modules]# cat dl.py def msg(): msg='this is test message' return msg def time(): import time a=time.asctime() return a必须以.py结尾

然后同步到minion里(使用saltutil.sync_all)

[root@centos _modules]# salt '*' saltutil.sync_all server.hadoop.com: |_ - modules.dl |_ |_ |_ |_ |_ localhost: |_ - modules.dl |_ |_ |_ |_ |_下面测试

[root@centos _modules]# salt '*' dl.msg localhost: this is test message server.hadoop.com: this is test message [root@centos _modules]# salt '*' dl.time server.hadoop.com: Tue Aug 13 15:25:32 2013 localhost: Tue Aug 13 15:25:29 2013当然还可以直接调用salt的模块

调用先有的module来显现自定义module中需要的功能saltsalt内置的一个字典,包含了所有的salt的moudle

def cmd(cmd): return __salt__['cmd.run'](cmd)同步

之后测试

[root@centos _modules]# salt '*' saltutil.sync_all server.hadoop.com: |_ |_ |_ |_ |_ |_ localhost: |_ - modules.dl |_ |_ |_ |_ |_ [root@centos _modules]# salt 'localhost' dl.cmd ls localhost: 1.log 1.py 111.py 1111.log 2.log 3.log anaconda-ks.cfg install.log install.log.syslog install_openstack.sh install_zabbix_agent.sh svn_install.sh test test5 test7.py zatree [root@centos _modules]# salt 'localhost' dl.cmd hostname localhost: centos下面是一些关于client的描述

Python client API Salt is written to be completely API centric, Salt minions and master can be built directly into third party applications as a communication layer. The Salt client API is very straightforward.运行单个命令

>>> import salt.client >>> a=salt.client.LocalClient() >>> a <salt.client.LocalClient object at 0x1ad8f10> >>> a.cmd("localhost","test.ping") {'localhost': True} >>> a.cmd("*","test.ping") {'server.hadoop.com': True, 'localhost': True} >>> a.cmd("*","dl.time") {'server.hadoop.com': 'Wed Aug 14 09:53:22 2013', 'localhost': 'Wed Aug 14 09:53:22 2013'}运行多个命令

>>> a.cmd('*',['cmd.run','test.ping','dl.time'],[['hostname'],[],[]]) {'server.hadoop.com': {'test.ping': True, 'dl.time': 'Wed Aug 14 10:01:35 2013', 'cmd.run': 'server.hadoop.com'}, 'localhost': {'test.ping': True, 'dl.time': 'Wed Aug 14 10:01:35 2013', 'cmd.run': 'centos'}}具体参考http://docs.saltstack.com/ref/python-api.html


有关0.16版本salt的安装与日常应用的更多相关文章

  1. ruby - 将差异补丁应用于字符串/文件 - 2

    对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl

  2. ruby - 在 64 位 Snow Leopard 上使用 rvm、postgres 9.0、ruby 1.9.2-p136 安装 pg gem 时出现问题 - 2

    我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po

  3. ruby - 完全离线安装RVM - 2

    我打算为ruby​​脚本创建一个安装程序,但我希望能够确保机器安装了RVM。有没有一种方法可以完全离线安装RVM并且不引人注目(通过不引人注目,就像创建一个可以做所有事情的脚本而不是要求用户向他们的bash_profile或bashrc添加一些东西)我不是要脚本本身,只是一个关于如何走这条路的快速指针(如果可能的话)。我们还研究了这个很有帮助的问题:RVM-isthereawayforsimpleofflineinstall?但有点误导,因为答案只向我们展示了如何离线在RVM中安装ruby。我们需要能够离线安装RVM本身,并查看脚本https://raw.github.com/wayn

  4. ruby-on-rails - Rails 应用程序之间的通信 - 2

    我构建了两个需要相互通信和发送文件的Rails应用程序。例如,一个Rails应用程序会发送请求以查看其他应用程序数据库中的表。然后另一个应用程序将呈现该表的json并将其发回。我还希望一个应用程序将存储在其公共(public)目录中的文本文件发送到另一个应用程序的公共(public)目录。我从来没有做过这样的事情,所以我什至不知道从哪里开始。任何帮助,将不胜感激。谢谢! 最佳答案 无论Rails是什么,几乎所有Web应用程序都有您的要求,大多数现代Web应用程序都需要相互通信。但是有一个小小的理解需要你坚持下去,网站不应直接访问彼此

  5. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  6. ruby-on-rails - rails 目前在重启后没有安装 - 2

    我有一个奇怪的问题:我在rvm上安装了ruby​​onrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(

  7. ruby - 如何为 emacs 安装 ruby​​-mode - 2

    我刚刚为fedora安装了emacs。我想用emacs编写ruby。为ruby​​提供代码提示、代码完成类型功能所需的工具、扩展是什么? 最佳答案 ruby-mode已经包含在Emacs23之后的版本中。不过,它也可以通过ELPA获得。您可能感兴趣的其他一些事情是集成RVM、feature-mode(Cucumber)、rspec-mode、ruby-electric、inf-ruby、rinari(用于Rails)等。这是我当前用于Ruby开发的Emacs配置:https://github.com/citizen428/emacs

  8. ruby-on-rails - Rails 应用程序中的 Rails : How are you using application_controller. rb 是新手吗? - 2

    刚入门rails,开始慢慢理解。有人可以解释或给我一些关于在application_controller中编码的好处或时间和原因的想法吗?有哪些用例。您如何为Rails应用程序使用应用程序Controller?我不想在那里放太多代码,因为据我了解,每个请求都会调用此Controller。这是真的? 最佳答案 ApplicationController实际上是您应用程序中的每个其他Controller都将从中继承的类(尽管这不是强制性的)。我同意不要用太多代码弄乱它并保持干净整洁的态度,尽管在某些情况下ApplicationContr

  9. ruby-on-rails - 无法在centos上安装therubyracer(V8和GCC出错) - 2

    我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e

  10. ruby - 通过 RVM (OSX Mountain Lion) 安装 Ruby 2.0.0-p247 时遇到问题 - 2

    我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search

随机推荐