Ansibel常用模块
1、cron 模块
创建一个同步时间的计划任务,每 5 分钟同步一下服务器的时间:
ansible webservers -m cron -a "minute='*/5' job='ntpdate time.windows.com &>/dev/null' name='sync time'" ? -a "" 指定时间 ? minute= 分 ? hour= 时 ? day= 日 ? month= 月 ? weekday= 周 ? job= 执行命令 ? name= 定时任务名称阿良教育:http://www.aliangedu.cn ? user= 执行用户 ? state={present|absent} 删除 2、文件传输(copy、file 和 synchronize) ? copy 拷贝文件 ? file 修改文件权限,创建和删除目录 ? synchronize 增量同步 使用 copy 模块将文件传输到多台服务器: ansible webservers -m copy –a “src=/etc/hosts dest=/tmp/hosts” 修改文件权限: ansible webservers -m file –a “dest=/srv/foo/a.txt mode=600” ansible webservers -m file –a “dest=/srv/foo/b.txt mode=600 owner=mdehaan group=mdehaan” 在远程主机创建目录并设置权限: ansible webservers -m file –a “dest=/path/to/c mode=755 owner=mdehaan group=mdehaan state=directory” 删除目录(递归)或删除文件: ansible webservers -m file –a “dest=/path/to/c state=absent” 增量同步网站程序: ansible webservers -m synchronize -a "src=/data/html dest=/var/www/html" 排除目录: ansible webservers -m synchronize -a "src=/data/html dest=/var/www/html rsync_opts=--exclude=.git" 注:synchronize 依赖系统 rsync 命令