安装ansible
yum install ansble
ansible用ssh管理主机,配置成密钥模式来管理节点主机
[root@centos01 ~]# ssh-keygen -t rsa <!--生成密钥对--> Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa):<!--密钥对存放路径--> Created directory '/root/.ssh'. Enter passphrase (empty for no passphrase): <!--输入私钥保护密码,直接按Enter键表示无密码--> Enter same passphrase again: <!--再次输入--> Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: SHA256:cJz6NRTrvMDxX+Jpce6LRnWI3vVEl/zvARL7D10q9WY root@centos01 The key's randomart image is: +---[RSA 2048]----+ | . . .| | . . + oo| | . = o o. oo| | = * o..+ *| | . S *.=+=*+| | . o =+XooE| | . ..=.++.| | ..o ..| | .. o. | +----[SHA256]-----+
上传公钥到节点主机
ssh-copy-id -i .ssh/id_rsa.pub [email protected]
管理节点的命令
ansible -i /etc/ansible/hosts web -m ping #/etc/ansible/hosts节点管理路径, web组
[root@centos01 ~]# vim /etc/ansible/hosts ............ <!--此处省略部分内容--> [web] 192.168.100.20 192.168.100.30 [test] www.benet.com:222 <!--通过222端口管理设备--> [mail] yj1.kgc.cn yj[2:5].kgc.cn <!--[2:5]表示2~5之间的所有数字,即表示yj2.kgc.cn、yj3.kgc.cn……的所有主机-->
ansible web -m command -a "systemctl status httpd" --limit "192.168.100.20" #发送命令给web组192.168.100.20节点主机
ansible web -m command -a "systemctl status httpd" #发送命令给web组的所有主机