当前位置:首页 > 系统运维
自动化运维工具 Ansible 的安装和配置
来源:靑龍一笑的博客  作者:靑龍一笑  发布时间:2020-04-24 16:54:37  点击量:702  评论:0

    Ansible 是基于 Python 开发的自动化运维工具,默认通过 SSH 协议管理机器。

一、环境准备

    我这里准备了四台服务器:

管理节点:192.168.154.130(172.17.0.1),操作系统:CentOS7
托管节点1:172.17.0.2,操作系统:CentOS8
托管节点2:172.17.0.3,操作系统:CentOS8
托管节点3:172.17.0.4,操作系统:CentOS8

    由于我准备的三台托管节点是使用 docker 安装的系统,docker 的系统是精简化的,缺少了很多的软件包。因此,需要在托管节点上安装以下几个必须的软件包:

[root@172-17-0-2 ~]# dnf install openssh-server openssh-clients passwd cracklib-dicts python3
[root@172-17-0-2 ~]# ln -s /usr/bin/python3 /usr/bin/python

二、在管理节点上部署 ansible

[root@centos7 ~]# yum install epel-release
[root@centos7 ~]# yum install ansible
[root@centos7 ~]# ansible --version

    Ansible 可以同时操作属于一个组的多台主机,组和主机之间的关系通过 inventory 文件配置,默认的文件路径为 /etc/ansible/hosts。

三、配置 ansible

[root@centos7 ~]# vi /etc/ansible/hosts

    内容如下:

[webservers]
172.17.0.2
172.17.0.3

[dbservers]
172.17.0.4

    方括号[]中是组名,用于对系统进行分类,便于对不同系统进行个别的管理。
    一个系统可以属于不同的组,比如一台服务器可以同时属于 webserver 组和 dbserver 组。这时,属于两个组的变量都可以为这台主机所用。
    如果有主机的 SSH 端口不是标准的 22 端口,可以在主机名之后加上端口号,用冒号分隔。例如:

172.17.0.2:3389

    对于每一个 host,还可以选择连接类型和连接用户名等。例如:

172.17.0.2    ansible_connection=ssh    ansible_ssh_port=3389    ansible_ssh_user=ricen    ansible_ssh_pass="p2020Sys"

    这里先把 /etc/ansible/hosts 的内容修改如下:

[webservers]
172.17.0.2    ansible_connection=ssh    ansible_ssh_port=3389    ansible_ssh_user=ricen    ansible_ssh_pass="p2020Sys"
172.17.0.3    ansible_connection=ssh    ansible_ssh_port=3389    ansible_ssh_user=ricen    ansible_ssh_pass="p2020Sys"

[dbservers]
172.17.0.4    ansible_connection=ssh    ansible_ssh_port=3389    ansible_ssh_user=ricen    ansible_ssh_pass="p2020Sys"

    测试:

[root@centos7 ~]# ansible all -m ping

    这里可能会报如下错误:

Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this.  Please add this host's fingerprint to your known_hosts file to manage this host.

    这是因为使用 ssh 首次连接时,会提示输入 yes/no 信息,解决方法如下:

[root@centos7 ~]# vi /etc/ansible/ansible.cfg

    找到 #host_key_checking = False,将其注释去掉。
    正常的测试结果如下:

[root@centos7 ~]# ansible all -m ping
172.17.0.2 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
172.17.0.4 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
172.17.0.3 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

    应该注意的是,使用 ansible_ssh_pass 的方式不安全,强烈建议使用 SSH 密钥。
    首先,将 /etc/ansible/hosts 的内容修改回来:

[webservers]
172.17.0.2
172.17.0.3

[dbservers]
172.17.0.4

    在管理节点上执行以下操作:

[root@centos7 ~]# mkdir -pv ~/.ssh
[root@centos7 ~]# chmod 700 ~/.ssh/
[root@centos7 ~]# cd ~/.ssh/
[root@centos7 .ssh]# ssh-keygen -t rsa

    在所有托管节点上执行以下操作:

[root@172-17-0-2 ~]# mkdir -pv ~/.ssh
[root@172-17-0-2 ~]# chmod 700 ~/.ssh/
[root@172-17-0-2 ~]# ssh 172.17.0.1 cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
[root@172-17-0-2 ~]# chmod 600 ~/.ssh/authorized_keys

    测试:

[root@centos7 ~]# ansible all -m command -a 'date'
172.17.0.4 | SUCCESS | rc=0 >>
Fri Apr 24 08:50:01 UTC 2020

172.17.0.2 | SUCCESS | rc=0 >>
Fri Apr 24 08:50:01 UTC 2020

172.17.0.3 | SUCCESS | rc=0 >>
Fri Apr 24 08:50:01 UTC 2020

版权所有 © 2005-2023 靑龍一笑的博客  Powered by C.S.Ricen
Copyright © 2005-2023 by www.ricensoftwares.com.cn  All Rights Reserved.

欢迎光临本站,这里是靑龍一笑的博客。

因资金匮乏,本站已迁到国外的免费空间,可能导致本站的访问速度较慢,由此给您带来的不便,敬请谅解。

您可以通过下方的“支持本站建设”链接,给本站提供资金支持。

Free Web Hosting