知识了解:
Ansible 是给 Linux 系统管理员使用的出色自动化工具。它是一种开源配置工具,能让系统管理员可以从一个中心节点(即 Ansible 服务器)管理数百台服务器。将 Ansible 与 Puppet、Chef 和 Salt 等类似工具进行比较时,它是首选的配置工具,因为它不需要任何代理,并且可以工作在 SSH 和 python 上。
在本教程中,我们将学习如何在 CentOS 8 系统上安装 Ansible。
一、安装ansible
1.Ansible 包不在 CentOS 8 默认的软件包仓库中。因此,我们需要执行以下命令启用 EPEL仓库
dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm -y
2.启用 epel 仓库后,执行以下 dnf命令安装 Ansible
dnf -y install ansible
3.查看ansible版本
ansible --version
安装完成
2.免密登录
1.当我们使用 yum 或 dnf 命令安装 Ansible 时,它的配置文件、清单文件和角色目录会自动在/etc/ansible 文件夹下创建。
添加一个名称为 webserver的组,并在/etc/ansible/hosts 文件中给该组添加上述的centos8系统的 IP 地址(主192.168.137.134、从192.168.137.135)
vim /etc/ansible/hosts
vim /etc/ansible/ansible.cfg
打开注释
host_key_checking = False
log_path = /var/log/ansible.log
module_name = shell
2.将用户的 ssh 公钥放到属于 webservers 组的远程系统。
首先使用 ssh-keygen命令生成本地用户的公钥和私钥,一直回车
ssh-keygen
公钥和私钥都在/root/.ssh/下面
在134主机上复制一份公钥,命名为authorized_keys
cd ~/.ssh
cp id_rsa.pub authorized_keys
复制一份公钥到135从机上
ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
测试验证ssh
ssh 192.168.137.135
退出远程登录
exit
三、ansible验证
Ansible命令格式 ansible <hosts> [options]
控制台输出hello world
ansible all -a “/bin/echo hello world”
ping 用来测试主机的连通性
ansibe all -m ping