安装ansible
Ansible仓库默认不在yum仓库中,因此我们需要启用epel仓库
安装epel
yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
安装python-jinja2-2.7.2-4.el7
网址为https://centos.pkgs.org/7/centos-x86_64/python-jinja2-2.7.2-4.el7.noarch.rpm.html
使用rz上传
yum安装
[root@localhost ~]# yum -y install python-jinja2-2.7.2-4.el7.noarch.rpm
[root@localhost ~]# yum install -y ansible
生成公钥/私钥
默认生成在root家目录.ssh目录下
[root@localhost ~]# ls -ad .ssh
.ssh
私钥设置,下次登录就不需要密码了
[root@localhost ~]# ssh-copy-id -i root@192.168.1.120 //输入yes和密码
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed – if you are prompted now it is to install the new keys
root@192.168.1.210’s password:
Number of key(s) added: 1
Now try logging into the machine, with: “ssh ‘root@192.168.1.120’”
and check to make sure that only the key(s) you wanted were added.
[root@localhost ~]# ssh-copy-id -i root@192.168.1.120 //输入yes和密码
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed – if you are prompted now it is to install the new keys
root@192.168.1.195’s password:
Number of key(s) added: 1
Now try logging into the machine, with: “ssh ‘root@192.168.1.120’”
and check to make sure that only the key(s) you wanted were added.
配置文件
[root@localhost ~]# vim /etc/ansible/hosts
加组名ip,保存退出
ping自己的组名
[root@localhost ~]# ansible -m ping “fyk”
服务就搭建完成了
然后就可以通过以下命令对这两台机器进行操作
[root@localhost ~]# ansible test-servers -u root -a ls