ansible模块练习-------------------学习(archive模块+fetch+find)
准备:安装ansible
epel-release库安装
yum -y install ansible
做ssh无密码远程登陆:
ssh-keygen
ssh-copy-id IP地址
到/opt目录下面,创建工作目录ansible,然后在其下面创建如下ls的文件
[root@zabbix-agent ansible]# ls
ansible.cfg hosts yasuo.yml
[root@zabbix-agent ansible]# cat ansible.cfg
[defaults]
inventory=./hosts
ask_pass=false
[root@zabbix-agent ansible]# cat hosts
[master]
192.168.8.200
[slave]
bss.cmcc.com
[root@zabbix-agent ansible]#
======================================================================================
archive模块:tar打包
[root@zabbix-agent ansible]# cat yasuo.yml
---
- hosts: slave
tasks:
- name: yasuo
archive:
path: "/data/tomcat/logs"
dest: "/opt/files/tomcatlogs.tar.gz"
format: "gz"
unarchive模块: tar解压
剧本如下:
---
- hosts: slave
tasks:
- name: yasuo
archive:
path: "/data/tomcat/logs"
dest: "/opt/files/tomcatlogs.tar.gz"
format: "gz"
- name: 解压缩
unarchive:
src: "/opt/files/tomcatlogs.tar.gz"
dest: "/tmp"
=================================================================================
find+fetch模块:实现远程批量抓取文件到管理机上面::
上剧本:
---
- hosts: all
tasks:
- name: 245
find:
paths: /root #服务器文件存放路径
patterns: "*.rpm" #服务器端文件通配符取文件
recurse: no
register: file_245_fetch
- name: 从远程机器上面抓到管理机上面
fetch:
src: "{{ item.path }}"
dest: "/opt/files/" #本地端文件存放路径
flat: yes
with_items: "{{ file_245_fetch.files }}"
执行剧本:
实现:
[root@zabbix-agent ansible]# ls /opt/files/gitlab-ce-12.4.2-ce.0.el7.x86_64.rpm
ls: cannot access /opt/files/gitlab-ce-12.4.2-ce.0.el7.x86_64.rpm: No such file or directory