安装使用root权限
yum -y install ansible
创建ansible作为ansible的配置信息存储目录
mkdir ansible
进入目录
cd ansible
创建配置文件
vi hosts
[test]
yhq112 ansible_ssh_user=yhq ansible_ssh_pass=123456
说明
[test] 主机组,主机组下边可以包含多条主机信息
yhq112 主机组下的主机名
ansible_ssh_user 指定用户
ansible_ssh_pass 对应密码
查看主机列表
ansible test -i hosts --list-hosts
测试连通性
ansible test -i hosts -m ping
对指定的主机组远程执行命令
ansible test -i hosts -m command -a 'date'
command模块下的命令
chdir 在执行命令之前,先切换到该目录
creates 一个文件名,当这个文件存在,则该命令不执行,可以用来做判断
removes 一个文件名,这个文件不存在,则该命令不执行
例如
#先切换到/app/ 目录,再执行“ls”命令
ansible test -i hosts -m command -a 'chdir=/app ls'
#如果/home/yhq/aaa.jpg存在,则不执行“ls”命令
ansible test -i hosts -m command -a 'creates=/home/yhq/aaa.jpg ls'
ansible test -i hosts -m command -a 'touch /home/yhq/aaa.jpg'
ansible test -i hosts -m command -a 'creates=/home/yhq/aaa.jpg ls'
#如果/home/yhq/aaa.jpg存在,则执行“cat /home/yhq/st.sh”命令
ansible test -i hosts -m command -a 'removes=/home/yhq/aaa.jpg cat /home/yhq/st.sh'
远程执行脚本
ansible test -i hosts -m shell -a 'chdir=/home/yhq/hadoop-3.2.2/ sbin/stop-all.sh'