文件同步:
其实在做openstack的运维对一些文件的同步其实是很繁琐。有一个配置项或者一行代码的源码文件进行同步。那么现在我们就开始介绍saltstack的文件同步功能
环境说明:操作系统版本:rhel6.5x64
1、master配置同步根目录
在开始saltstack的配置管理之前,要首先指定saltstack所有状态文件的根目录,在master上做如下操作
## 首先修改master的配置文件,指定根目录,注意缩进全部使用两个空格来代替Tab(python规范)## 确定指定的目录是否存在,如果不存在,需要手动来创建目录
[[email protected] ~]# vim /etc/salt/master file_roots: base: - /srv/salt dev: - /srv/salt/dev/ [[email protected] ~]# mkdir -p /srv/salt/dev [[email protected] ~]# ls -ld /srv/salt/dev drwxr-xr-x 2 root root 4096 Feb 3 21:49 /srv/salt/dev
重启master服务
[[email protected] ~]# service salt-master restart Stopping salt-master daemon: [ OK ] Starting salt-master daemon: [ OK ]
2、介绍cp.get_file
首先介绍cp.get_file,用来从master端下载文件到minion的指定目录下,如下
## 在master上创建测试用的文件
[[email protected] ~]# echo ‘This is test file with saltstack module to cp.get_file‘ >/opt/getfile.txt [[email protected] ~]# cat /opt/getfile.txt This is test file with saltstack module to cp.get_file
将文件拷贝到master的同步根目录下
[[email protected] ~]# cp /opt/getfile.txt /srv/salt/
在master上执行文件下发
[[email protected] ~]# salt ‘computer3‘ cp.get_file salt://getfile.txt /tmp/getfile.txt computer3: /tmp/getfile.txt
登录到computer3上查看同步情况
[[email protected] ~]# cat /tmp/getfile.txt This is test file with saltstack module to cp.get_file
分发文件的一些属性:
(1)压缩 gzip
使用gzip的方式进行压缩,数字越大,压缩率就越高,9代表最大的压缩率
[[email protected] ~]# salt ‘computer8‘ cp.get_file salt://getfile.txt /tmp/getfile.txt gzip=9 computer8: /tmp/getfile.txt
(2)创建目录 makedirs(当分发的位置在目标主机上不存在时,自动创建该目录)
[[email protected] ~]# salt ‘computer8‘ cp.get_file salt://getfile.txt /tmp/srv/getfile.txt makedirs=True computer8: /tmp/srv/getfile.txt [[email protected] opt]# ll /tmp/srv/getfile.txt -rw-r--r-- 1 root root 56 Feb 3 22:14 /tmp/srv/getfile.txt
3、grains
先介绍一下grains,这个接口的作用是在minion端的minion服务启动时,调用这个接口,收集minion端的信息,这些信息数据可以在salt的其他模块中直接使用,需要注意的是,这个接口只在minion端的minion服务启动时被调用一次,所以收集的数据是静态的,不会改变的,除非你重启了minion端的服务
grains的基本用法:
[[email protected] ~]# salt ‘computer3‘ grains.ls computer3: - biosreleasedate - biosversion - cpu_flags - cpu_model - cpuarch - defaultencoding - defaultlanguage - domain - fqdn - fqdn_ip4 - fqdn_ip6 - gpus - host - hwaddr_interfaces - id - ip_interfaces - ipv4 - ipv6 - kernel - kernelrelease - localhost - manufacturer - master - mem_total - nodename - num_cpus - num_gpus - os - os_family - osarch - oscodename - osfinger - osfullname - osmajorrelease - osrelease - path - productname - ps - pythonpath - pythonversion - saltpath - saltversion - saltversioninfo - serialnumber - server_id - shell - virtual - zmqversion
使用grains.items模块列出所有可用grains的具体数据
[[email protected] ~]# salt ‘computer3‘ grains.items computer3: biosreleasedate: 08/28/2013 biosversion: 2.10.0 cpu_flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good xtopology nonstop_tsc aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic popcnt aes lahf_lm arat dts tpr_shadow vnmi flexpriority ept vpid cpu_model: Intel(R) Xeon(R) CPU E7- 4820 @ 2.00GHz cpuarch: x86_64 defaultencoding: UTF8 defaultlanguage: en_US domain: fqdn: computer3 fqdn_ip4: 192.168.100.23 fqdn_ip6: gpus: {‘model‘: ‘MGA G200eW WPCM450‘, ‘vendor‘: ‘unknown‘} host: computer3 hwaddr_interfaces: {‘lo‘: ‘00:00:00:00:00:00‘, ‘tap002cf093-0c‘: ‘fe:16:3e:cf:43:28‘, ‘em4‘: ‘f0:1f:af:90:38:65‘, ‘eth1.2‘: ‘f0:1f:af:90:37:fd‘, ‘em3‘: ‘f0:1f:af:90:38:63‘, ‘brq8f15ee7f-54‘: ‘f0:1f:af:90:37:fd‘, ‘brqadf94242-74‘: ‘f0:1f:af:90:37:fd‘, ‘eth1.400‘: ‘f0:1f:af:90:37:fd‘, ‘eth1‘: ‘f0:1f:af:90:37:fd‘, ‘eth0‘: ‘f0:1f:af:90:37:fb‘} id: computer3 ip_interfaces: {‘lo‘: [‘127.0.0.1‘], ‘tap002cf093-0c‘: [], ‘em4‘: [], ‘eth1.2‘: [], ‘em3‘: [], ‘brq8f15ee7f-54‘: [], ‘brqadf94242-74‘: [], ‘eth1.400‘: [], ‘eth1‘: [], ‘eth0‘: [‘192.168.100.23‘]} ipv4: 127.0.0.1 192.168.100.23 ipv6: ::1 fe80::60f7:96ff:feab:3d44 fe80::f21f:afff:fe90:37fb fe80::f21f:afff:fe90:37fd fe80::f8e7:cdff:fe54:7d02 fe80::fc16:3eff:fecf:4328 kernel: Linux kernelrelease: 2.6.32-431.el6.x86_64 localhost: computer3 manufacturer: Dell Inc. master: 192.168.100.200 mem_total: 225995 nodename: computer3 num_cpus: 64 num_gpus: 1 os: RedHat os_family: RedHat osarch: x86_64 oscodename: Santiago osfinger: Red Hat Enterprise Linux Server-6 osfullname: Red Hat Enterprise Linux Server osmajorrelease: 6 5 osrelease: 6.5 path: /sbin:/usr/sbin:/bin:/usr/bin productname: PowerEdge M910 ps: ps -efH pythonpath: /usr/bin /usr/lib64/python26.zip /usr/lib64/python2.6 /usr/lib64/python2.6/plat-linux2 /usr/lib64/python2.6/lib-tk /usr/lib64/python2.6/lib-old /usr/lib64/python2.6/lib-dynload /usr/lib64/python2.6/site-packages /usr/lib64/python2.6/site-packages/gtk-2.0 /usr/lib/python2.6/site-packages /usr/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg-info pythonversion: 2.6.6.final.0 saltpath: /usr/lib/python2.6/site-packages/salt saltversion: 2014.1.10 saltversioninfo: 2014 1 10 0 serialnumber: XXXXXX server_id: 111111111 shell: /bin/bash virtual: physical zmqversion: 4.0.5
ping测试grains中os的值为RedHat的主机通信是否正常
[[email protected] ~]# salt -G ‘os:RedHat‘ test.ping computer5: True computer8: True computer6: True computer7: True computer4: True computer3: True
查看uadoop2主机的ip地址,注意这里不是items噢,而是item
[[email protected] ~]# salt ‘*‘ grains.item ipv4 computer5: ipv4: 127.0.0.1 192.168.100.25 computer7: ipv4: 127.0.0.1 192.168.100.27 computer4: ipv4: 127.0.0.1 192.168.100.24 computer3: ipv4: 127.0.0.1 192.168.100.23 computer8: ipv4: 127.0.0.1 192.168.100.28 computer6: ipv4: 127.0.0.1 192.168.100.26
未完待续.....