美文网首页
Linux_307_Ansible模块之copy

Linux_307_Ansible模块之copy

作者: 为宇绸缪 | 来源:发表于2022-11-14 10:10 被阅读0次

copy模块
作用:复制文件数据到远程主机

ansible-doc -s copy 查看copy
backup最好选择yes,进行备份

image.png

copy模块是远程推送数据的模块,只能把管理节点上的数据推送给远程节点,无法拉取数据到本地

实际案例
1、把主机的文件数据,发给被管理机器

2、先创建好需要数据复制的user group,批量创建用户,用户组,通过command模块或者shell模块,远程的执行命令即可

ansible yu -m command -a "useradd learn_ansible"
ansible yu -m command -a "id learn_ansible"

[root@yuweijie /]# ansible yu -m command -a "useradd learn_ansible"
192.168.178.122 | CHANGED | rc=0 >>

192.168.178.121 | CHANGED | rc=0 >>

[root@yuweijie /]# ansible yu -m command -a "id learn_ansible"
192.168.178.122 | CHANGED | rc=0 >>
uid=1001(learn_ansible) gid=1001(learn_ansible) groups=1001(learn_ansible)
192.168.178.121 | CHANGED | rc=0 >>
uid=1002(learn_ansible) gid=1002(learn_ansible) groups=1002(learn_ansible)

3、批量拷贝文件,分发给客户端节点

ansible yu -m copy -a "src=/test_file/my_linux.txt dest=/tmp/my_linux_ansible.txt owner=learn_ansible group=learn_ansible mode=0666 backup=yes"
[root@yuweijie test_file]# ansible yu -m copy -a "src=/test_file/my_linux.txt dest=/tmp/my_linux_ansible.txt owner=learn_ansible group=learn_ansible mode=0666 backup=yes"
192.168.178.122 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "checksum": "80bd3bdde6ed1807c22a6b89bc7bc4ea086d4d0c", 
    "dest": "/tmp/my_linux_ansible.txt", 
    "gid": 1001, 
    "group": "learn_ansible", 
    "md5sum": "9dce749936d1eea7ae359c67a7ffa399", 
    "mode": "0666", 
    "owner": "learn_ansible", 
    "size": 25, 
    "src": "/root/.ansible/tmp/ansible-tmp-1646560328.05-32463-239047538148407/source", 
    "state": "file", 
    "uid": 1001
}
192.168.178.121 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "checksum": "80bd3bdde6ed1807c22a6b89bc7bc4ea086d4d0c", 
    "dest": "/tmp/my_linux_ansible.txt", 
    "gid": 1002, 
    "group": "learn_ansible", 
    "md5sum": "9dce749936d1eea7ae359c67a7ffa399", 
    "mode": "0666", 
    "owner": "learn_ansible", 
    "size": 25, 
    "src": "/root/.ansible/tmp/ansible-tmp-1646560328.1-32461-88861964870166/source", 
    "state": "file", 
    "uid": 1002
}

远程检查拷贝后的信息直接使用命令查看

ansible yu -m command -a "ls -l /tmp/my_linux_ansible.txt"
[root@yuweijie test_file]# ansible yu -m command -a "ls -l /tmp/my_linux_ansible.txt"
192.168.178.122 | CHANGED | rc=0 >>
-rw-rw-rw- 1 learn_ansible learn_ansible 25 Mar  6 17:52 /tmp/my_linux_ansible.txt
192.168.178.121 | CHANGED | rc=0 >>
-rw-rw-rw- 1 learn_ansible learn_ansible 25 Mar  6 17:52 /tmp/my_linux_ansible.txt

相关文章

网友评论

      本文标题:Linux_307_Ansible模块之copy

      本文链接:https://www.haomeiwen.com/subject/fuklxdtx.html