(3)常用模块

1)查看模块帮助:ansible-doc 模块名 例如:ansible-doc shell 搜索:/EX 查看案例

2)ping模块:测试连接到目标主机的ssh端口是否能正常通信

ansible webservers -m ping -o

3)command模块:用于远程主机命令,默认模块,建议使用shell模块

ansible webservers -m command -a 'uptime'

4)shell模块:执行远程命令,一些简单的shell命令

    ansible webservers -m shell -a 'uptime'

5)copy模块:复制文件到远程主机

ansible webservers -m copy -a 'src=/etc/hosts dest=/tmp/hosts '
ansible webservers -m copy -a 'src=/etc/hosts dest=/tmp/hosts backup=yes'

6)user模块:创建和删除用户

创建用户:ansible webservers -m user -a 'name=jack shell=/sbin/nologin'
删除用户:ansible webservers -m user -a 'name=jack state=absent'
	echo 123 | openssl passwd -1 -stdin
	ansible webservers -m user -a 'name=jack password="$1$Ec9OCYTb$OJtUgRRglXNuEMmSCLfPl/"'

7)yum模块:安装和卸载软件包

安装:ansible webservers -m yum -a 'name=httpd state=latest'
卸载:ansible webservers -m yum -a 'name=httpd state=absent'

8)service模块:控制服务运行状态

ansible webservers -m service -a 'name=httpd state=started enabled=yes'   \state是服务的启动,重启,关闭;enabled是服务器的开机启动
ansible webservers -m service -a 'name=httpd  state=stopped enabled=no'	

9)file模块:创建文件或目录

创建文件:ansible webservers -m file -a 'path=/tmp/1.log mode=777 state=touch' 
创建目录:ansible webservers -m file -a 'path=/tmp/dir1 mode=777 state=directory'

10)cron模块:创建和移除定义任务

ansible webservers -m cron -a 'name="test" minute="*/10" job="/bin/echo hello"'
ansible webservers -m cron -a 'name="test2" minute="00" hour="03" job="/bin/echo hello"'
移除:ansible webservers -m cron -a 'name="test2" minute="00" hour="03" job="/bin/echo hello" state=absent'
原文地址:https://www.cnblogs.com/lovelinux199075/p/9004307.html