Linux自动化工具之Ansible拷贝目录或者文件
2020-02-21自动化运维90root368°c
A+ A-注意:源目录会放到目标目录下面去,如果目标指定的目录不存在,它会自动创建。如果拷贝的是文件,dest指定的名字和源如果不同,并且它不是已经存在的目录,相当于拷贝过去后又重命名。但相反,如果test是目标机器上已经存在的目录,则会直接把文件拷贝到该目录下面。
ansible testhost -m copy -a “src=/etc/passwd dest=/tmp/123”
src=/etc/passwd来源目录 ,dest=/tmp/123″拷贝到这个目录下,这里的/tmp/123和源机器上的/etc/passwd是一致的,但如果目标机器上已经有/tmp/123目录,则会再/tmp/123目录下面建立passwd文件,如果没有会建立/tmp/123.
1、拷贝文件
[[email protected] ~]# ansible testhosts -m copy -a "src=/etc/passwd dest=/tmp/123" 101.200.148.30 | SUCCESS => { "changed": true, "checksum": "aae7ac86fef49841ff78094b20baa8eb4b804946", "dest": "/tmp/123", "gid": 0, "group": "root", "md5sum": "d30b329d1e90bdc91f475f8b6cd437b2", "mode": "0644", "owner": "root", "size": 1099, "src": "/root/.ansible/tmp/ansible-tmp-1468614379.06-175386594238677/source", "state": "file", "uid": 0 } 127.0.0.1 | SUCCESS => { "changed": true, "checksum": "aae7ac86fef49841ff78094b20baa8eb4b804946", "dest": "/tmp/123", "gid": 0, "group": "root", "md5sum": "d30b329d1e90bdc91f475f8b6cd437b2", "mode": "0644", "owner": "root", "size": 1099, "src": "/root/.ansible/tmp/ansible-tmp-1468614379.23-164923985358498/source", "state": "file", "uid": 0 }
2、查看刚刚拷贝的文件
[[email protected] ~]# cd /tmp/ [[email protected] tmp]# ls 123 Aegis-<Guid(5A2C30A2-A87D-490A-9281-6765EDAD7CBA)> qtsingleapp-aegisG-46d2 qtsingleapp-aegiss-a5d2 [[email protected] tmp]# vim 123 root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin gopher:x:13:30:gopher:/var/gopher:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin abrt:x:173:173::/etc/abrt:/sbin/nologin haldaemon:x:68:68:HAL daemon:/:/sbin/nologin ntp:x:38:38::/etc/ntp:/sbin/nologin saslauth:x:499:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin nscd:x:28:28:NSCD Daemon:/:/sbin/nologin
3、拷贝目录
src=/etc/ansible源文件,dest=/tmp/ansibletest拷贝目录,owner=root属主 group=root属组 mode=0644权限
[[email protected] ~]# ansible testhosts -m copy -a "src=/etc/ansible dest=/tmp/ansibletest owner=root group=root mode=0644" 101.200.148.30 | SUCCESS => { "changed": true, "dest": "/tmp/ansibletestowner=root/", "src": "/etc/ansible" } 127.0.0.1 | SUCCESS => { "changed": true, "dest": "/tmp/ansibletestowner=root/", "src": "/etc/ansible" }
4、如发现以下问题,先在目标机器上安装软件包
[[email protected] ~]# ansible web10.gz.com -m copy -a "src=/etc/passwd dest=/tmp/1.txt" web10.gz.com | FAILED! => { "changed": false, "checksum": "94c290ba39362aba4abe1b85ad4ede97672f610e", "failed": true, "msg": "Aborting, target uses selinux but python bindings (libselinux-python) aren't installed!" } [[email protected] ~]# yum install -y libselinux-python