学海无涯
go,go,go

ansible-常用模块:copy、Fetch、file、cron(二)

说明:
(1)如果指定的目标目录不存在,系统会自动创建,否则源目录会放到目标目录下面去。
(2)如果 copy的是文件,dest指定的名字和源如果不同,并且它不是已经存在的目录,相当于copy过去后再重命名。
(3)如果dest是目标机器上已经存在的目录,则会直接把文件copy到该目录下面。
(4)设定的用户和组oldboy在所有客户端必须存在。

#把管理端的文件/tmp/l.sh拷贝到被管理端的目录/tmp/,src=/tmp/l.sh表示管理端的原文件,dest=/tmp/拷贝到被管理端的目录路径后面如果接个文件名称可以进行改名
[root@ansible ~]# ansible all -m copy -a 'src=/tmp/l.sh dest=/tmp/'
192.168.2.196 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "checksum": "2980ea44582cc9f750e0db09a35d17cf279fb258", 
    "dest": "/tmp/l.sh", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "6755b776e19a3b11acf70a8f77ae2e2a", 
    "mode": "0644", 
    "owner": "root", 
    "size": 32, 
    "src": "/root/.ansible/tmp/ansible-tmp-1579402884.75-187395985101304/source", 
    "state": "file", 
    "uid": 0
}
192.168.2.197 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "checksum": "2980ea44582cc9f750e0db09a35d17cf279fb258", 
    "dest": "/tmp/l.sh", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "6755b776e19a3b11acf70a8f77ae2e2a", 
    "mode": "0644", 
    "owner": "root", 
    "size": 32, 
    "src": "/root/.ansible/tmp/ansible-tmp-1579402884.79-61004522822204/source", 
    "state": "file", 
    "uid": 0
}
192.168.2.189 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "checksum": "2980ea44582cc9f750e0db09a35d17cf279fb258", 
    "dest": "/tmp/l.sh", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "6755b776e19a3b11acf70a8f77ae2e2a", 
    "mode": "0644", 
    "owner": "root", 
    "size": 32, 
    "src": "/root/.ansible/tmp/ansible-tmp-1579402884.69-145530211619445/source", 
    "state": "file", 
    "uid": 0
}
#检查
[root@ansible ~]# ansible all -m shell -a 'ls -l /tmp/l.sh'
192.168.2.196 | CHANGED | rc=0 >>
-rw-r--r-- 1 root root 32 Jan 19 11:01 /tmp/l.sh

192.168.2.197 | CHANGED | rc=0 >>
-rw-r--r-- 1 root root 32 Jan 19 11:01 /tmp/l.sh

192.168.2.189 | CHANGED | rc=0 >>
-rw-r--r-- 1 root root 32 Jan 19 11:01 /tmp/l.sh

[root@ansible ~]# ansible all -m shell -a 'cat /tmp/l.sh'
192.168.2.196 | CHANGED | rc=0 >>
#!/bin/bash
hostname
date
df -h

192.168.2.197 | CHANGED | rc=0 >>
#!/bin/bash
hostname
date
df -h

192.168.2.189 | CHANGED | rc=0 >>
#!/bin/bash
hostname
date
df -h

#复制到远端主机改名并且修改权限mode=600设置权限为600,owner设置属主位wang
[root@ansible ~]# ansible all -m copy -a 'src=/tmp/l.sh dest=/tmp/test1 mode=600 owner=wang'
192.168.2.197 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "checksum": "2980ea44582cc9f750e0db09a35d17cf279fb258", 
    "dest": "/tmp/test1", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "6755b776e19a3b11acf70a8f77ae2e2a", 
    "mode": "0600", 
    "owner": "wang", 
    "size": 32, 
    "src": "/root/.ansible/tmp/ansible-tmp-1579405064.84-112352220018873/source", 
    "state": "file", 
    "uid": 1000
}
192.168.2.196 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "checksum": "2980ea44582cc9f750e0db09a35d17cf279fb258", 
    "dest": "/tmp/test1", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "6755b776e19a3b11acf70a8f77ae2e2a", 
    "mode": "0600", 
    "owner": "wang", 
    "size": 32, 
    "src": "/root/.ansible/tmp/ansible-tmp-1579405064.73-266071756678537/source", 
    "state": "file", 
    "uid": 1000
}
192.168.2.189 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "checksum": "2980ea44582cc9f750e0db09a35d17cf279fb258", 
    "dest": "/tmp/test1", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "6755b776e19a3b11acf70a8f77ae2e2a", 
    "mode": "0600", 
    "owner": "wang", 
    "size": 32, 
    "src": "/root/.ansible/tmp/ansible-tmp-1579405064.66-257835854228248/source", 
    "state": "file", 
    "uid": 1000
}
#检查
[root@ansible ~]# ansible all -m shell -a 'ls -l /tmp/test1'
192.168.2.197 | CHANGED | rc=0 >>
-rw------- 1 wang root 32 Jan 19 11:37 /tmp/test1

192.168.2.189 | CHANGED | rc=0 >>
-rw------- 1 wang root 32 Jan 19 11:37 /tmp/test1

192.168.2.196 | CHANGED | rc=0 >>
-rw------- 1 wang root 32 Jan 19 11:37 /tmp/test1

[root@ansible ~]# ansible all -m shell -a 'cat /tmp/test1'
192.168.2.189 | CHANGED | rc=0 >>
#!/bin/bash
hostname
date
df -h

192.168.2.197 | CHANGED | rc=0 >>
#!/bin/bash
hostname
date
df -h

192.168.2.196 | CHANGED | rc=0 >>
#!/bin/bash
hostname
date
df -h


#拷贝/tmp目录到被管理端并且改名为dirtest
[root@ansible tmp]# ansible all -m copy -a 'src=/tmp dest=/tmp/dirtest'
192.168.2.189 | CHANGED => {
    "changed": true, 
    "dest": "/tmp/dirtest/", 
    "src": "/tmp"
}
192.168.2.197 | CHANGED => {
    "changed": true, 
    "dest": "/tmp/dirtest/", 
    "src": "/tmp"
}
192.168.2.196 | CHANGED => {
    "changed": true, 
    "dest": "/tmp/dirtest/", 
    "src": "/tmp"
}
#检查
[root@ansible tmp]# ansible all -m shell -a 'ls -ld /tmp/dirtest'
192.168.2.189 | CHANGED | rc=0 >>
drwxr-xr-x 3 root root 17 Jan 19 11:42 /tmp/dirtest

192.168.2.196 | CHANGED | rc=0 >>
drwxr-xr-x 3 root root 17 Jan 19 11:42 /tmp/dirtest

192.168.2.197 | CHANGED | rc=0 >>
drwxr-xr-x 3 root root 17 Jan 19 11:42 /tmp/dirtest


 'content="df -h\nhostname\nls\n" dest=/tmp/f1.sh' content='你要传输过去变成文件内容'  dest=/tmp/f1.sh远程端被管理主机生成的文件
把内容传输过去直接变成f1.sh文件的内容
[root@ansible tmp]# ansible all -m copy -a 'content="df -h\nhostname\nls\n" dest=/tmp/f1.sh'
192.168.2.189 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "checksum": "7e36e7d98b3cf09d54c2280d20af39d29c799189", 
    "dest": "/tmp/f1.sh", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "9448519c2551487ac4f6205ced16ec29", 
    "mode": "0644", 
    "owner": "root", 
    "size": 18, 
    "src": "/root/.ansible/tmp/ansible-tmp-1579413313.62-119847073475602/source", 
    "state": "file", 
    "uid": 0
}
192.168.2.196 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "checksum": "7e36e7d98b3cf09d54c2280d20af39d29c799189", 
    "dest": "/tmp/f1.sh", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "9448519c2551487ac4f6205ced16ec29", 
    "mode": "0644", 
    "owner": "root", 
    "size": 18, 
    "src": "/root/.ansible/tmp/ansible-tmp-1579413313.74-237463421648206/source", 
    "state": "file", 
    "uid": 0
}
192.168.2.197 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "checksum": "7e36e7d98b3cf09d54c2280d20af39d29c799189", 
    "dest": "/tmp/f1.sh", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "9448519c2551487ac4f6205ced16ec29", 
    "mode": "0644", 
    "owner": "root", 
    "size": 18, 
    "src": "/root/.ansible/tmp/ansible-tmp-1579413313.82-179982320516715/source", 
    "state": "file", 
    "uid": 0
}

#检查
[root@ansible tmp]# ansible all -m shell -a 'ls -l /tmp/f1.sh'
192.168.2.197 | CHANGED | rc=0 >>
-rw-r--r-- 1 root root 18 Jan 19 13:55 /tmp/f1.sh

192.168.2.196 | CHANGED | rc=0 >>
-rw-r--r-- 1 root root 18 Jan 19 13:55 /tmp/f1.sh

192.168.2.189 | CHANGED | rc=0 >>
-rw-r--r-- 1 root root 18 Jan 19 13:55 /tmp/f1.sh

[root@ansible tmp]# ansible all -m shell -a 'cat /tmp/f1.sh'
192.168.2.197 | CHANGED | rc=0 >>
df -h
hostname
ls

192.168.2.189 | CHANGED | rc=0 >>
df -h
hostname
ls

192.168.2.196 | CHANGED | rc=0 >>
df -h
hostname
ls
Fetch模块实现从客户端抓文件到服务端,不能抓取目录,要把客户端的目录拷贝到服务端可以打个包在拉取到服务端
#创建测试目录
[root@ansible ~]# mkdir -p /app


#把远端被管理的机器的/etc/passwd文件抓取到管理端的/app目录下
src=/etc/passwd表示远程的passwd文件,dest=/app/表示抓取到管理端的/app目录
[root@ansible ~]# ansible all -m fetch -a 'src=/etc/passwd dest=/app/'
192.168.2.196 | CHANGED => {
    "changed": true, 
    "checksum": "7abc9f317e5290dda5c3726d03652c4f9a154f55", 
    "dest": "/app/192.168.2.196/etc/passwd", 
    "md5sum": "80740a3c75d742936473a5280cbad486", 
    "remote_checksum": "7abc9f317e5290dda5c3726d03652c4f9a154f55", 
    "remote_md5sum": null
}
192.168.2.189 | CHANGED => {
    "changed": true, 
    "checksum": "7abc9f317e5290dda5c3726d03652c4f9a154f55", 
    "dest": "/app/192.168.2.189/etc/passwd", 
    "md5sum": "80740a3c75d742936473a5280cbad486", 
    "remote_checksum": "7abc9f317e5290dda5c3726d03652c4f9a154f55", 
    "remote_md5sum": null
}
192.168.2.197 | CHANGED => {
    "changed": true, 
    "checksum": "e5624e38d5db2598488a945c66fdea08b4aa653b", 
    "dest": "/app/192.168.2.197/etc/passwd", 
    "md5sum": "691c419e184c211ee1f3d3a2aa142ec3", 
    "remote_checksum": "e5624e38d5db2598488a945c66fdea08b4aa653b", 
    "remote_md5sum": null
}

#检查
[root@ansible ~]# cd /app
[root@ansible app]# ls
192.168.2.189  192.168.2.196  192.168.2.197
[root@ansible app]# tree 
.
├── 192.168.2.189
│   └── etc
│       └── passwd
├── 192.168.2.196
│   └── etc
│       └── passwd
└── 192.168.2.197
    └── etc
        └── passwd

6 directories, 3 files


抓取客户主机端的/var/log/*所有日志到管理端的app目录
使用shell模块打包日志
[root@ansible app]# ansible all -m shell -a 'tar Jcf /tmp/log.tar.xz /var/log/*.log'
[WARNING]: Consider using the unarchive module rather than running 'tar'.  If you need to use command because unarchive is insufficient you can add 'warn: false' to this
command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.

192.168.2.189 | CHANGED | rc=0 >>
tar: Removing leading `/' from member names

192.168.2.196 | CHANGED | rc=0 >>
tar: Removing leading `/' from member names

192.168.2.197 | CHANGED | rc=0 >>
tar: Removing leading `/' from member names
#检查
[root@ansible app]# ansible all -m shell -a 'ls -l /tmp/log.tar.xz'
192.168.2.189 | CHANGED | rc=0 >>
-rw-r--r-- 1 root root 1124 Jan 19 14:06 /tmp/log.tar.xz

192.168.2.197 | CHANGED | rc=0 >>
-rw-r--r-- 1 root root 1124 Jan 19 14:06 /tmp/log.tar.xz

192.168.2.196 | CHANGED | rc=0 >>
-rw-r--r-- 1 root root 2024 Jan 19 14:06 /tmp/log.tar.xz

#抓取压缩的日志到管理端
[root@ansible app]# ansible all -m fetch -a 'src=/tmp/log.tar.xz dest=/app/'
192.168.2.189 | CHANGED => {
    "changed": true, 
    "checksum": "fde2c4ad6e1627524e6f75d2db653ba79fa03207", 
    "dest": "/app/192.168.2.189/tmp/log.tar.xz", 
    "md5sum": "02b8aaa54fb48cce4bb9b517491c3344", 
    "remote_checksum": "fde2c4ad6e1627524e6f75d2db653ba79fa03207", 
    "remote_md5sum": null
}
192.168.2.196 | CHANGED => {
    "changed": true, 
    "checksum": "f64178b293f1474c73db5e8d266c74c574cb9265", 
    "dest": "/app/192.168.2.196/tmp/log.tar.xz", 
    "md5sum": "c3da6559226769a7276bd73821c9418e", 
    "remote_checksum": "f64178b293f1474c73db5e8d266c74c574cb9265", 
    "remote_md5sum": null
}
192.168.2.197 | CHANGED => {
    "changed": true, 
    "checksum": "41e7c46980cc33361c0ba116a7edbce3604d6ca3", 
    "dest": "/app/192.168.2.197/tmp/log.tar.xz", 
    "md5sum": "3d8e23a74427a4f4b5a8bf74bf836839", 
    "remote_checksum": "41e7c46980cc33361c0ba116a7edbce3604d6ca3", 
    "remote_md5sum": null
}

#检查
[root@ansible app]# tree /app/192.168.2.*
/app/192.168.2.189
├── etc
│   └── passwd
└── tmp
    └── log.tar.xz
/app/192.168.2.196
├── etc
│   └── passwd
└── tmp
    └── log.tar.xz
/app/192.168.2.197
├── etc
│   └── passwd
└── tmp
    └── log.tar.xz

6 directories, 6 files
[root@ansible app]# cd 192.168.2.196
[root@ansible 192.168.2.196]# ls
etc  tmp
[root@ansible 192.168.2.196]# cd tmp/
[root@ansible tmp]# ls
log.tar.xz
file模块
一、概述
file 模块可以帮助我们完成一些对文件的基本操作。比如,创建文件或目录、删除文件或目录、修改文件权限等。

二、常用参数
模块参数详解:  
    owner:修改属主
    group:修改属组
    mode:修改权限
    path=:要修改文件的路径
    recurse:递归的设置文件的属性,只对目录有效
        yes:表示使用递归设置

    state:
        touch:创建一个新的空文件
        directory:创建一个新的目录,当目录存在时不会进行修改
        link:创建软连接,结果src一起使用此选项才生效
        hard:创建硬连接

        absent:删除文件,目录,软连接
     src:当state=link时,要被连接文件的源路径

在/app目录下生成文件testfile
state=touch表示创建空文件,path=/opt/file-test指定生产文件的路径和名称,mode=600指定生产文件的权限为600,owner=wang指定文件属主为wang
[root@ansible tmp]# ansible all -m file -a 'path=/opt/file-test state=touch mode=600 owner=wang'
192.168.2.189 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "dest": "/opt/file-test", 
    "gid": 0, 
    "group": "root", 
    "mode": "0600", 
    "owner": "wang", 
    "size": 0, 
    "state": "file", 
    "uid": 1000
}
192.168.2.197 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "dest": "/opt/file-test", 
    "gid": 0, 
    "group": "root", 
    "mode": "0600", 
    "owner": "wang", 
    "size": 0, 
    "state": "file", 
    "uid": 1000
}
192.168.2.196 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "dest": "/opt/file-test", 
    "gid": 0, 
    "group": "root", 
    "mode": "0600", 
    "owner": "wang", 
    "size": 0, 
    "state": "file", 
    "uid": 1000
}

#检查
[root@ansible tmp]# ansible all -m shell -a 'ls -l /opt/file-test'
192.168.2.196 | CHANGED | rc=0 >>
-rw------- 1 wang root 0 Jan 19 14:18 /opt/file-test

192.168.2.197 | CHANGED | rc=0 >>
-rw------- 1 wang root 0 Jan 19 14:18 /opt/file-test

192.168.2.189 | CHANGED | rc=0 >>
-rw------- 1 wang root 0 Jan 19 14:18 /opt/file-test

为file-test文件创建软连接
state=link表示创建软连接,src=/opt/file-test表示源文件,path=/tmp/file-test-likn指定生产软连接的路径和名称
[root@ansible tmp]# ansible all -m file -a 'src=/opt/file-test path=/tmp/file-test-link state=link'
192.168.2.196 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "dest": "/tmp/file-test-link", 
    "gid": 0, 
    "group": "root", 
    "mode": "0777", 
    "owner": "root", 
    "size": 14, 
    "src": "/opt/file-test", 
    "state": "link", 
    "uid": 0
}
192.168.2.197 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "dest": "/tmp/file-test-link", 
    "gid": 0, 
    "group": "root", 
    "mode": "0777", 
    "owner": "root", 
    "size": 14, 
    "src": "/opt/file-test", 
    "state": "link", 
    "uid": 0
}
192.168.2.189 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "dest": "/tmp/file-test-link", 
    "gid": 0, 
    "group": "root", 
    "mode": "0777", 
    "owner": "root", 
    "size": 14, 
    "src": "/opt/file-test", 
    "state": "link", 
    "uid": 0
}

检查
[root@ansible tmp]# ansible all -m shell -a 'ls -l /tmp/file-test-link'
192.168.2.197 | CHANGED | rc=0 >>
lrwxrwxrwx 1 root root 14 Jan 19 14:21 /tmp/file-test-link -> /opt/file-test

192.168.2.196 | CHANGED | rc=0 >>
lrwxrwxrwx 1 root root 14 Jan 19 14:21 /tmp/file-test-link -> /opt/file-test

192.168.2.189 | CHANGED | rc=0 >>
lrwxrwxrwx 1 root root 14 Jan 19 14:21 /tmp/file-test-link -> /opt/file-test


file创建目录
[root@ansible tmp]# ansible all -m file -a 'path=/tmp/dir1 state=directory'
192.168.2.197 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "gid": 0, 
    "group": "root", 
    "mode": "0755", 
    "owner": "root", 
    "path": "/tmp/dir1", 
    "size": 6, 
    "state": "directory", 
    "uid": 0
}
192.168.2.196 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "gid": 0, 
    "group": "root", 
    "mode": "0755", 
    "owner": "root", 
    "path": "/tmp/dir1", 
    "size": 6, 
    "state": "directory", 
    "uid": 0
}
192.168.2.189 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "gid": 0, 
    "group": "root", 
    "mode": "0755", 
    "owner": "root", 
    "path": "/tmp/dir1", 
    "size": 6, 
    "state": "directory", 
    "uid": 0
}

#检查
[root@ansible tmp]# ansible all -a 'ls -ld /tmp/dir1'
192.168.2.196 | CHANGED | rc=0 >>
drwxr-xr-x 2 root root 6 Jan 19 14:26 /tmp/dir1

192.168.2.197 | CHANGED | rc=0 >>
drwxr-xr-x 2 root root 6 Jan 19 14:26 /tmp/dir1

192.168.2.189 | CHANGED | rc=0 >>
drwxr-xr-x 2 root root 6 Jan 19 14:26 /tmp/dir1

#删除刚刚创建的dir1目录
state=absent代表删除,path可以指定文件或者目录这样就直接删除了
[root@ansible tmp]# ansible all -m file -a 'path=/tmp/dir1 state=absent'
192.168.2.189 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "path": "/tmp/dir1", 
    "state": "absent"
}
192.168.2.196 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "path": "/tmp/dir1", 
    "state": "absent"
}
192.168.2.197 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "path": "/tmp/dir1", 
    "state": "absent"
}
#检查成功删除
[root@ansible tmp]# ansible all -a 'ls -ld /tmp/dir1'
192.168.2.196 | FAILED | rc=2 >>
ls: cannot access /tmp/dir1: No such file or directorynon-zero return code

192.168.2.197 | FAILED | rc=2 >>
ls: cannot access /tmp/dir1: No such file or directorynon-zero return code

192.168.2.189 | FAILED | rc=2 >>
ls: cannot access /tmp/dir1: No such file or directorynon-zero return code

#删除文件
[root@ansible tmp]# ansible all -m file -a 'path=/tmp/file-test-link state=absent'
192.168.2.197 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "path": "/tmp/file-test-link", 
    "state": "absent"
}
192.168.2.196 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "path": "/tmp/file-test-link", 
    "state": "absent"
}
192.168.2.189 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "path": "/tmp/file-test-link", 
    "state": "absent"
}

#检查成功删除
[root@ansible tmp]# ansible all -a 'ls -ld /tmp/file-test-link'
192.168.2.189 | FAILED | rc=2 >>
ls: cannot access /tmp/file-test-link: No such file or directorynon-zero return code

192.168.2.196 | FAILED | rc=2 >>
ls: cannot access /tmp/file-test-link: No such file or directorynon-zero return code

192.168.2.197 | FAILED | rc=2 >>
ls: cannot access /tmp/file-test-link: No such file or directorynon-zero return code

#创建3个文件
ansible all -m file -a 'path=/tmp/file3 state=touch mode=600 owner=wang'

#检查
[root@ansible tmp]# ansible all -m shell -a 'ls -l /tmp/file*'
192.168.2.197 | CHANGED | rc=0 >>
-rw------- 1 wang root 0 Jan 19 14:54 /tmp/file1
-rw------- 1 wang root 0 Jan 19 14:54 /tmp/file2
-rw------- 1 wang root 0 Jan 19 14:54 /tmp/file3

192.168.2.196 | CHANGED | rc=0 >>
-rw------- 1 wang root 0 Jan 19 14:54 /tmp/file1
-rw------- 1 wang root 0 Jan 19 14:54 /tmp/file2
-rw------- 1 wang root 0 Jan 19 14:54 /tmp/file3

192.168.2.189 | CHANGED | rc=0 >>
-rw------- 1 wang root 0 Jan 19 14:54 /tmp/file1
-rw------- 1 wang root 0 Jan 19 14:54 /tmp/file2
-rw------- 1 wang root 0 Jan 19 14:54 /tmp/file3

删除创建的file3个文件
[root@ansible tmp]# ansible all -m file -a 'path=/tmp state=absent'
192.168.2.196 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "path": "/tmp", 
    "state": "absent"
}
192.168.2.197 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "path": "/tmp", 
    "state": "absent"
}
192.168.2.189 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "path": "/tmp", 
    "state": "absent"
}
#检查删除成功
[root@ansible tmp]# ansible all -m shell -a 'ls -l /tmp/file*'
192.168.2.197 | FAILED | rc=2 >>
ls: cannot access /tmp/file*: No such file or directorynon-zero return code

192.168.2.196 | FAILED | rc=2 >>
ls: cannot access /tmp/file*: No such file or directorynon-zero return code

192.168.2.189 | FAILED | rc=2 >>
ls: cannot access /tmp/file*: No such file or directorynon-zero return code
ansible模块---cron
me设置定时任务的名称,默认是以#Ansible: test-cron开头后面加自己设置的名称name=test-cron
minute设置定时任务的分钟数这里设置每2分钟执行一次minute=*/2
hour设置小时,这里设置为每天早上6点hour=06
day设置日期这里设置每个月的3号day=03 
month设置月数,这里设置每年的2月份month=02
weekday设置星期几这里设置为星期2weekday=01
job设置要执行的定时任务这里的定时任务为时间同步 job='/usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1'
[root@lamp ~]# crontab -l
#Ansible: test-cron
*/2 06 03 02 01 /usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1

删除ansible设置的定时任务使用state=absent
ansible的cron模块只能管理ansible设置的定时任务,本机自己设定的定时任务ansible将不能管理
ansible test -m cron -a "name=test-cron minute=*/2 hour=06 day=03 month=02 weekday=01 job='/usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1'"
[root@lnmp ~]# ansible test -m cron -a "name=test-cron minute=*/2 hour=06 day=03 month=02 weekday=01 job='/usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1' state=absent"
10.128.30.74 | SUCCESS => {
"changed": true, 
"envs": [], 
"jobs": []
}
[root@lamp ~]# crontab -l
#time sync by oldboy at 2017-12-13
#*/5 * * * * /usr/sbin/ntpdate cn.pool.ntp.org >/dev/null 2>&1
state=absent为删除定时任务使用state参数

没有使用name参数设置的定时任务没有name参数设置的将直接备注为#Ansible: None 
[root@lnmp ~]# ansible test -m cron -a " minute=*/2 hour=06 day=03 month=02 weekday=01 job='/usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1'"
10.128.30.74 | SUCCESS => {
"changed": true, 
"envs": [], 
"jobs": [
"None"
]
}
[root@lamp ~]# crontab -l
#time sync by oldboy at 2017-12-13
#*/5 * * * * /usr/sbin/ntpdate cn.pool.ntp.org >/dev/null 2>&1
#Ansible: None 没有name设置名称的话设置的定时任务默认是使用none做为备注
*/2 06 03 02 01 /usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1


修改定时任务
[root@lamp ~]# crontab -l
#Ansible: None
*/2 06 03 02 01 /usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1
[root@lnmp ~]# ansible test -m cron -a " name=None minute=*/2 hour=06 job='/usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1'"
10.128.30.74 | SUCCESS => {
"changed": true, 
"envs": [], 
"jobs": [
"None"
]
}
[root@lamp ~]# crontab -l
#Ansible: None
*/2 06 * * * /usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1
使用name参数直接指定定时任务的名称None,在使用设置方法将直接修改之前设置cron定时任务,注意一定要指定名称,默认不设置的定时任务时间参数为*

注释定时任务
[root@lamp ~]# crontab -l
#Ansible: None
*/2 06 03 02 01 /usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1
[root@lnmp ~]# ansible test -m cron -a " name=None minute=*/2 hour=06 job='/usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1' disabled=yes"
10.128.30.74 | SUCCESS => {
"changed": true, 
"envs": [], 
"jobs": [
"None"
]
}
[root@lamp ~]# crontab -l
#Ansible: None
#*/2 06 * * * /usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1
disabled=yes为注释定时任务,disabled=no把注释的定时任务去掉#号,disabled参数只能管理ansible设置的定时任务,注:不能管理被控主机自己设置的定时任务
赞(0) 打赏
未经允许不得转载:YYQ运维技术博客_运维的工作学习之路 » ansible-常用模块:copy、Fetch、file、cron(二)
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

运维devops

联系我们关于本博客

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏