欲忘树 发表于 2018-1-3 17:36:11

Saltstack cp.get 模块

语法
  

salt '*' cp.get_file salt://rr /etc/rr  

  cp.get_url可以从一个URL地址下载文件,URL可以是msater上的路径(salt://),也可以是http网址
  

salt '*' cp.get_url salt://my/file /tmp/mine  


master配置同步根目录

在开始saltstack的配置管理之前,要首先指定saltstack所有状态文件的根目录,在master上做如下操作:
首先修改master的配置文件,指定根目录,注意缩进全部使用两个空格来代替Tab(python规范)## 确定指定的目录是否存在,如果不存在,需要手动来创建目录  

master:node1  minion:node2
  

  

# vim /etc/salt/master  

  

406   file_roots:  

407   base:  

408       - /srv/salt  

409   dev:  

410       - /srv/salt/dev  

  创建目录
  

# mkdir -p /srv/salt/dev  

  重启master服务
  

# systemctl restart salt-master  

  在master上创建测试用的文件
  

# echo 'This is test file with saltstack module tocp.get_file' >/opt/getfile.txt  

  
[iyunv@node1
~]# cat /opt/getfile.txt  
This
is test file with saltstack module to cp.get_file  

  将文件拷贝到master的同步根目录下
  

# cp /opt/getfile.txt /srv/salt/  

  在master上执行文件下发到弄得 node2 主机上
  

# salt 'node2' cp.get_file salt://getfile.txt /tmp/getfile.txt  
node2:
  /tmp/getfile.txt
  

  登录到minion查看上查看同步情况
  

# cat /tmp/getfile.txt  
This
is test file with saltstack module tocp.get_file  

  压缩分发:使用gzip的方式进行压缩,数字越大,压缩率就越高,9代表最大的压缩率
  

# salt 'node2' cp.get_file salt://getfile.txt /tmp/getfile.txt gzip=9  
node2:
  /tmp/getfile.txt
  

  创建目录 makedirs(当分发的位置在目标主机上不存在时,自动创建该目录)
  

# salt 'node2' cp.get_file salt://getfile.txt /tmp/srv/getfile.txt makedirs=True  
node2:
  /tmp/srv/getfile.txt
  

  
# ll /tmp/srv/getfile.txt
  
-rw-r--r-- 1 root root 56 Aug 23 13:37 /tmp/srv/getfile.txt
  

  查看os是什么版本
  

#salt 'node2'grains.item os  
node2:
  ----------
  os:
  CentOS
  

  ping测试grains中的os值为CentOS的主机通信是否正常
  

# salt -G 'os:CentOS' test.ping  
node3:
  True
  
node2:
  True
  

  查看uadoop2主机的ip地址,注意这里不是items噢,而是item
  

# salt '*' grains.item ipv4  
node3:
----------  ipv4:
- 10.0.0.22  - 127.0.0.1
  
node2:
  ----------
  ipv4:
  - 10.0.0.21
  - 127.0.0.1
  

  目录同步

目录同步:cp.get_dir ;get_dir与get_file的用法十分相似,用来将整个目录分发到minions
创建测试文件  

#mkdir /srv/salt/test_dir  

  写入信息
  

# echo 'hello word !!' >>/srv/salt/test_dir/hello1.txt  
[iyunv@node1
~]# echo 'hello2 word !!' >>/srv/salt/test_dir/hello2.txt  

  
[iyunv@node1
~]# ll /srv/salt/test_dir/  
total
8  
-rw-r--r-- 1 root root 14 Aug 23 14:00 hello1.txt
  
-rw-r--r-- 1 root root 15 Aug 23 14:00 hello2.txt
  

  测试分发: 执行目录文件的分发,并使用压缩传输
  

# salt '*' cp.get_dir salt://test_dir /tmp gzip=9  
node2:
  - /tmp/test_dir/hello1.txt
  - /tmp/test_dir/hello2.txt
  
node3:
  - /tmp/test_dir/hello1.txt
  - /tmp/test_dir/hello2.txt
  

  登录到目标节点查看分发状态
  

# ll /tmp/test_dir/  
total
8  
-rw-r--r-- 1 root root 14 Aug 23 18:56 hello1.txt
  
-rw-r--r-- 1 root root 15 Aug 23 18:56 hello2.txt
  

  
# ll /tmp/test_dir/
  
total 8
  
-rw-r--r-- 1 root root 14 Aug 23 18:56 hello1.txt
  
-rw-r--r-- 1 root root 15 Aug 23 18:56 hello2.txt
  
页: [1]
查看完整版本: Saltstack cp.get 模块