安装ansible集群管理和配置密钥互信

ansible特性

(1)、no agents:不需要在被管控主机上安装任何客户端;

(2)、no server:无服务器端,使用时直接运行命令即可;

(3)、modules in any languages:基于模块工作,可使用任意语言开发模块;

(4)、yaml,not code:使用yaml语言定制剧本playbook;

(5)、ssh by default:基于SSH工作;

(6)、strong multi-tier solution:可实现多级指挥。

ansible特点

(1)、轻量级,无需在客户端安装agent,更新时,只需在操作机上进行一次更新即可;
(2)、批量任务执行可以写成脚本,而且不用分发到远程就可以执行;
(3)、使用python编写,维护更简单,ruby语法过于复杂;
(4)、支持sudo。

环境准备

[root@node1 ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core) 
[root@node1 ~]# uname -r 
3.10.0-229.el7.x86_64
主机名          IP
node1     10.0.0.21
node2     10.0.0.22
node3     10.0.0.23
node4     10.0.0.24

安装ansible(10.0.0.21)

配置所有节密钥互信, 在node01可以免密码登录各节点,只在node01上执行

配置epel源

[root@node1 opt]# yum install -y wget
[root@node1 opt]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

安装

[root@node1 opt]# yum install -y ansible

查看版本

[root@node1 opt]# ansible --version

查看ansible 安装生成的文件

[root@10.0.0.21 opt]# rpm -ql ansible |more
/etc/ansible
/etc/ansible/ansible.cfg #配置文件
/etc/ansible/hosts #主要文件

hosts文件详解

[root@10.0.0.21 opt]# cat /etc/ansible/hosts
# This is the default ansible 'hosts' file.
#
# It should live in /etc/ansible/hosts
#
#   - Comments begin with the '#' character #注释为#
#   - Blank lines are ignored #空白行被忽略
#   - Groups of hosts are delimited by [header] elements #主机组名被写在[]里面
#   - You can enter hostnames or ip addresses #你可以写ip地址也可以写hostnames
#   - A hostname/ip can be a member of multiple groups #一个主机可以被多个主机组包含

配置hosts文件

[root@node1 opt]# vim /etc/ansible/hosts 

加入以下内容

[test]   #模块的名称,可以顺便写
10.0.0.21 10.0.0.22 10.0.0.23 10.0.0.24

生成秘钥

[root@node1 opt]# ssh-keygen 

秘钥分发

[root@node1 opt]# ssh-copy-id -i .ssh/id_rsa.pub 10.0.0.21
[root@node1 opt]# ssh-copy-id -i .ssh/id_rsa.pub 10.0.0.22
[root@node1 opt]# ssh-copy-id -i .ssh/id_rsa.pub 10.0.0.23
[root@node1 opt]# ssh-copy-id -i .ssh/id_rsa.pub 10.0.0.24
或
[root@node1 opt]# ssh-copy-id root@10.0.0.21
[root@node1 opt]# ssh-copy-id root@10.0.0.22
[root@node1 opt]# ssh-copy-id root@10.0.0.23
[root@node1 opt]# ssh-copy-id root@10.0.0.24

测试是否连通

[root@node1 opt]# ansible -m ping 10.0.0.21

登录验证(Ctrl+D 退出连接)

[root@node1 opt]# ssh 10.0.0.21
Last login: Sun Aug 20 13:24:41 2017 from 10.0.0.20
welcome to my service!
[root@node2 opt]# 
原文地址:https://www.cnblogs.com/wanglan/p/7495579.html