ansible使用技巧

一、常用模块

ansible-doc -l   # 列出 Ansible 支持的模块

ansible-doc ping   # 查看该模块帮助信息

 
1、command 作为 Ansible 的默认模块,可以运行远程权限范围所有的 shell 命令,不支持管道符
ansible Client -m command -a "free -m"               # 查看 Client 分组主机内存使用情况

2、script 的功能是在远程主机执行主控端存储的 shell 脚本文件,相当于 scp + shell 组合
ansible Client -m script -a "/home/test.sh 12 34"    # 远程执行本地脚本

3、shell模块基本和command相同,但是shell支持管道符
ansible Client -m shell -a "/home/test.sh"           # 执行远程脚本

4、copy模块实现主控端向目标主机拷贝文件,类似于 scp 功能
ansible Client -m copy -a "src=/home/test.sh dest=/tmp/ owner=root group=root mode=0755"   # 向 Client 组中主机拷贝 test.sh 到 /tmp 下,属主、组为 root ,权限为 0755

5、stat模块
获取远程文件状态信息,atime/ctime/mtime/md5/uid/gid 等信息
ansible Client -m stat -a "path=/etc/syctl.conf"

6、yum模块软件包管理
ansible Client -m yum -a "name=curl state=latest"

7、corn模块,远程主机 crontab 配置
ansible Client -m cron -a "name='check dirs' hour='5,2' job='ls -alh > /dev/null'"
效果:* 5,2 * * * ls -alh > /dev/null

8、mount模块,远程主机分区挂载
ansible Client -m mount -a "name=/mnt/data src=/dev/sd0 fstype=ext4 opts=ro state=present"

9、service模块,远程主机系统服务管理
ansible Client -m service -a "name=nginx state=stoped"
ansible Client -m service -a "name=nginx state=restarted"
ansible Client -m service -a "name=nginx state=reloaded"

10、user模块
ansible Client -m user -a "name=wang comment='user wang'"
ansible Client -m user -a "name=wang state=absent remove=yes"    # 添加删除用户

 

更多详细使用方法:https://juejin.im/post/5b3a074d51882548bc3aa35e

 

 

posted @ 2020-02-10 14:23  jayce9102  阅读(173)  评论(0编辑  收藏  举报