美文网首页Ansible
Linux_311_Ansible模块之copy文件

Linux_311_Ansible模块之copy文件

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

    Ansible模块之copy文件
    远程批量复制文件,备份,追加内容
    1、批量远程的生成文件和内容
    ansible yu -m shell -a "echo hello > /tmp/day.txt"
    ansible yu -m shell -a "cat /tmp/day.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
    [root@yuweijie test_file]# ansible yu -m shell -a "echo hello > /tmp/day.txt"
    192.168.178.121 | CHANGED | rc=0 >>
    
    192.168.178.122 | CHANGED | rc=0 >>
    
    [root@yuweijie test_file]# ansible yu -m shell -a "cat /tmp/day.txt"
    192.168.178.122 | CHANGED | rc=0 >>
    hello
    192.168.178.121 | CHANGED | rc=0 >>
    hello
    

    2、批量的实现了文件远程拷贝,且定义了新的内容放入文件中,并且针对目标机器的源数据做了一个备份
    ansible yu -m copy -a "content='My name is YU' dest=/tmp/day.txt backup=yes"
    远程批量验证文件结果
    ansible yu -m shell -a "ls -l /tmp/day*"

    [root@yuweijie test_file]# ansible yu -m copy -a "content='My name is YU' dest=/tmp/day.txt backup=yes"
    192.168.178.122 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        }, 
        "backup_file": "/tmp/day.txt.4888.2022-03-06@18:00:18~", 
        "changed": true, 
        "checksum": "1470df7a3c753da749536efff84b68f88aa805de", 
        "dest": "/tmp/day.txt", 
        "gid": 0, 
        "group": "root", 
        "md5sum": "b6fe510ab81db0811d79e84f7343dcf4", 
        "mode": "0644", 
        "owner": "root", 
        "size": 13, 
        "src": "/root/.ansible/tmp/ansible-tmp-1646560814.04-32629-31366915945124/source", 
        "state": "file", 
        "uid": 0
    }
    192.168.178.121 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        }, 
        "backup_file": "/tmp/day.txt.101369.2022-03-06@18:00:18~", 
        "changed": true, 
        "checksum": "1470df7a3c753da749536efff84b68f88aa805de", 
        "dest": "/tmp/day.txt", 
        "gid": 0, 
        "group": "root", 
        "md5sum": "b6fe510ab81db0811d79e84f7343dcf4", 
        "mode": "0644", 
        "owner": "root", 
        "size": 13, 
        "src": "/root/.ansible/tmp/ansible-tmp-1646560814.01-32627-94926239481271/source", 
        "state": "file", 
        "uid": 0
    }
    [root@yuweijie test_file]# ansible yu -m shell -a "ls -l /tmp/day*"
    192.168.178.122 | CHANGED | rc=0 >>
    -rw-r--r-- 1 root root 13 Mar  6 18:00 /tmp/day.txt
    -rw-r--r-- 1 root root  6 Mar  6 17:58 /tmp/day.txt.4888.2022-03-06@18:00:18~
    192.168.178.121 | CHANGED | rc=0 >>
    -rw-r--r-- 1 root root 13 Mar  6 18:00 /tmp/day.txt
    -rw-r--r-- 1 root root  6 Mar  6 17:58 /tmp/day.txt.101369.2022-03-06@18:00:18~
    

    /tmp/day.txt 这是新生成的文件和内容
    /tmp/day.txt.4888.2022-03-06@18:00:18 这是backup参数做好的文件备份

    相关文章

      网友评论

        本文标题:Linux_311_Ansible模块之copy文件

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