5、find模块
[centos@self1-centos7-2 ~]$ ansible-doc -s find
- name: Return a list of files based on specific criteria
find:
age: # 根据时间范围查找。age=-3d seconds, minutes, hours, days, or weeks (e.g., "1w").
age_stamp: # 时间类型指定。atime ctime mtime
contains: # 根据文件内容进行查找,可以是个正则表达式
depth: # Set the maximum number of levels to decend into. Setting recurse to `no' will override this value, which is effectively
depth 1. Default is unlimited depth.
excludes: # One or more (shell or regex) patterns, which type is controlled by `use_regex' option. Items whose basenames match an
`excludes' pattern are culled from `patterns' matches. Multiple patterns can be specified
using a list.
file_type: # 查找指定的类型,any,directory,file,link
follow: # Set this to `yes' to follow symlinks in path for systems with python 2.6+.
get_checksum: # 当有文件被找到,还会返回sha1码
hidden: # yes时不忽略隐藏文件
paths: # (required) 列出搜索位置,多个位置用,隔开。
patterns: # shell的glob表达式匹配。如果想使用python的正则,需要`use_regex' 选项打开.
recurse: # 目录包含目录,yes会递归查找
size: # 根据文件大小来查找
use_regex: # If `no', the patterns are file globs (shell). If `yes', they are python regexes.
示例:
从以下可以看出contains=".testfind." file_type=any时或的关系!(rc.local文件中并没有testfind字符串)
[root@self1-centos7-2 16:20:07 ~]#ansible self1-1 -m find -a 'paths=/test contains=".*testfind.*" recurse=yes hidden=yes'
self1-1 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"examined": 2,
"files": [
{
"atime": 1560356013.9905548,
"ctime": 1560356007.2717743,
"dev": 2049,
"gid": 0,
"gr_name": "root",
"inode": 8726906,
"isblk": false,
"ischr": false,
"isdir": false,
"isfifo": false,
"isgid": false,
"islnk": false,
"isreg": true,
"issock": false,
"isuid": false,
"mode": "0644",
"mtime": 1560356007.2717743,
"nlink": 1,
"path": "/test/filetestfind.txt",
"pw_name": "root",
"rgrp": true,
"roth": true,
"rusr": true,
"size": 13,
"uid": 0,
"wgrp": false,
"woth": false,
"wusr": true,
"xgrp": false,
"xoth": false,
"xusr": false
}
],
"matched": 1,
"msg": ""
}
[root@self1-centos7-2 16:21:28 ~]#ansible self1-1 -m find -a 'paths=/test contains=".*testfind.*" recurse=yes hidden=yes file_type=any'
self1-1 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"examined": 2,
"files": [
{
"atime": 1560351247.0321498,
"ctime": 1560351160.9830947,
"dev": 2049,
"gid": 0,
"gr_name": "root",
"inode": 4537510,
"isblk": false,
"ischr": false,
"isdir": false,
"isfifo": false,
"isgid": false,
"islnk": false,
"isreg": true,
"issock": false,
"isuid": false,
"mode": "0644",
"mtime": 1560351160.9798353,
"nlink": 1,
"path": "/test/rc.local",
"pw_name": "root",
"rgrp": true,
"roth": true,
"rusr": true,
"size": 701,
"uid": 0,
"wgrp": false,
"woth": false,
"wusr": true,
"xgrp": false,
"xoth": false,
"xusr": false
},
{
"atime": 1560356013.9905548,
"ctime": 1560356007.2717743,
"dev": 2049,
"gid": 0,
"gr_name": "root",
"inode": 8726906,
"isblk": false,
"ischr": false,
"isdir": false,
"isfifo": false,
"isgid": false,
"islnk": false,
"isreg": true,
"issock": false,
"isuid": false,
"mode": "0644",
"mtime": 1560356007.2717743,
"nlink": 1,
"path": "/test/filetestfind.txt",
"pw_name": "root",
"rgrp": true,
"roth": true,
"rusr": true,
"size": 13,
"uid": 0,
"wgrp": false,
"woth": false,
"wusr": true,
"xgrp": false,
"xoth": false,
"xusr": false
}
],
"matched": 2,
"msg": ""
}
查找以txt结尾的文件,两句的结果一样。第二句如果没有use_regex=yes,是找不到结果的。
[root@self1-centos7-2 16:28:53 ~]#ansible self1-1 -m find -a 'paths=/test patterns="*.txt" recurse=yes'
[root@self1-centos7-2 16:29:41 ~]#ansible self1-1 -m find -a 'paths=/test patterns=".*\.txt" use_regex=yes recurse=yes'
self1-1 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"examined": 2,
"files": [
{
"atime": 1560356013.9905548,
"ctime": 1560356007.2717743,
"dev": 2049,
"gid": 0,
"gr_name": "root",
"inode": 8726906,
"isblk": false,
"ischr": false,
"isdir": false,
"isfifo": false,
"isgid": false,
"islnk": false,
"isreg": true,
"issock": false,
"isuid": false,
"mode": "0644",
"mtime": 1560356007.2717743,
"nlink": 1,
"path": "/test/filetestfind.txt",
"pw_name": "root",
"rgrp": true,
"roth": true,
"rusr": true,
"size": 13,
"uid": 0,
"wgrp": false,
"woth": false,
"wusr": true,
"xgrp": false,
"xoth": false,
"xusr": false
}
],
"matched": 1,
"msg": ""
}
只查找1h以内创建的文件,只有filetestfind.txt, 文件rc.local就不会搜到
[root@self1-centos7-2 16:39:41 ~]#ansible self1-1 -m find -a 'paths=/test age=-1h age_stamp=ctime recurse=yes'
self1-1 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"examined": 2,
"files": [
{
"atime": 1560356013.9905548,
"ctime": 1560356007.2717743,
"dev": 2049,
"gid": 0,
"gr_name": "root",
"inode": 8726906,
"isblk": false,
"ischr": false,
"isdir": false,
"isfifo": false,
"isgid": false,
"islnk": false,
"isreg": true,
"issock": false,
"isuid": false,
"mode": "0644",
"mtime": 1560356007.2717743,
"nlink": 1,
"path": "/test/filetestfind.txt",
"pw_name": "root",
"rgrp": true,
"roth": true,
"rusr": true,
"size": 13,
"uid": 0,
"wgrp": false,
"woth": false,
"wusr": true,
"xgrp": false,
"xoth": false,
"xusr": false
}
],
"matched": 1,
"msg": ""
}
6、replace模块
[root@self1-centos7-2 16:40:17 ~]#ansible-doc -s replace
- name: Replace all instances of a particular string in a file using a back-referenced regular expression
replace:
backup: # 修改之前对文件进行备份
path: # (required) 要操作的文件
regexp: # (required) 指定一个python正则表达式,匹配的字符串将被替换
replace: # 指定要替换成的字符串
ansible database -m replace -a 'path=/test regexp="file" replace=FILE backup=yes'