本节内容以Centos7为系统环境进行讲解:
1.安装epel源,方便直接yum安装:
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
2.yum安装ansible:
yum -y install ansible
3.对ansible的hosts文件进行配置:
[root@sdrtest ~]# vi /etc/ansible/hosts
[root@sdrtest ~]# cat /etc/ansible/hosts
[webservers]
139.224.239.128
4.不上上面没有进行的ssh免密登录的操作,配置免密登录后操作会更简便一点:
[root@sdrtest ~]# cd .ssh
[root@sdrtest .ssh]# ssh-keygen -t rsa
Enter file in which to save the key (/root/.ssh/id_rsa): //回车
Enter passphrase (empty for no passphrase): //回车
Enter same passphrase again: //回车
[root@sdrtest .ssh]# ssh-copy-id 139.224.239.128
root@139.224.239.128's password: //输入需要远程免密的机器的密码
5.在机器上面本地进行ansible操作以验证ssh免密登录是否已经操作成功:
[root@sdrtest .ssh]# ansible all -m shell -a 'ifconfig'
139.224.239.128 | CHANGED | rc=0 >>
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.5.33.10 netmask 255.255.0.0 broadcast 10.5.255.255
ether 00:16:3e:08:21:2c txqueuelen 1000 (Ethernet)
RX packets 885498 bytes 185093829 (176.5 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 766049 bytes 570904522 (544.4 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1 (Local Loopback)
RX packets 254554 bytes 15785354 (15.0 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 254554 bytes 15785354 (15.0 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
6.ansible各参数对应的意义注解:
ansible all[指定主机组] -m[指定模块] shell[shell模块] -a[指定模块使用的参数] 'ifconfig|grep 56'[模块使用的参数]
others...