一、添加ansible主机

# vim /etc/ansible/hosts
	
	[server1]
	
	添加以下内容:

192.168.61.130 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=redhat
192.168.61.131 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=redhat

二、查看ansible模块

# ansible-doc -l

三、ansible常用模块

1.远程命令模块

command

Ansible默认模块,执行远程主机上的shell命令

# ansible server1 -m command -a "df -h"

scripts

在远程主机上执行主控端上的脚本

# ansible server1 -m script -a "/wh_k/test.sh"

shell

在远程主机上执行远程主机的脚本

# ansible server1 -m command -a "ls -l /wh_k/test.sh"
# ansible server1 -m command -a "chmod 751 /wh_k/test.sh"
# ansible server -m shell -a "/wh_k/test.sh"

2.copy模块

向远程主机拷贝文件

# ansible server1 -m copy -a "src=/wh_k/jmxcmd.jar dest=/wh_k/"

3.stat模块

获取远程主机文件的状态ctime、mtime等

# ansible server1 -m stat -a 'path=/wh_k/test.sh'

4.yum模块

远程主机软件包管理操作,yum、apt等

# ansible server1 -m yum -a 'name=httpd state=latest'

5.service模块

远程主机系统服务管理

# ansible server1 -m service -a 'name=httpd state=start'

6.user模块

远程主机用户

# ansible server1 -m user -a "name=wangwang"