ansible安装

\[10.Shell\]

环境:RHEL/CentOS Stream 10 / Rocky Linux,使用dnf包管理器,搭建 ansible 控制节点,需要三台虚拟机 管理 node1、node2 两台被管机器

配置本地域名解析,把 IP 和主机名做映射,后续直接写node1/node2代替 IP,不用每次输入长 IP。

shell 复制代码
vim /etc/hosts
192.168.
132.130 node1
192.168.142.131 node2

免密登录设置

shell 复制代码
ssh-keygen -t rsa
ls .ssh
shell 复制代码
ssh-copy-id node1
ssh-copy-id node2
  1. ssh-keygen -t rsa:生成 RSA 格式的 ssh 公私密钥对,一路回车即可,密钥默认存放在~/.ssh/目录。
    • 私钥:id_rsa(控制节点保留,不要泄露)
    • 公钥:id_rsa.pub(分发到被管理机器)
  2. ls .ssh:查看生成的密钥文件是否存在。
  3. ssh-copy-id node1:把本机公钥推送至 node1,写入 node1 的~/.ssh/authorized_keys,实现 ssh 免密码登录 node1。
  4. ssh-copy-id node2:同理,给 node2 配置免密。

安装源epel sshpass ansible

shell 复制代码
dnf install https://mirrors.aliyun.com/epel/epel-release-latest-10.noarch.rpm
dnf repolist
dnf install ansible-core -y
yum install-y sshpass

初始化ansible配置创建目录

shell 复制代码
ansible --version
ansible-config init --disabled > ./anaconda-ks.cfg
makdir ansible
cd ansible 
  1. ansible --version:查看 ansible 版本,验证安装是否成功。
  2. ansible-config init --disabled > ./anaconda-ks.cfg:导出一份全部默认禁用的 ansible 完整配置模板文件。
  3. mkdir ansible:创建 ansible 独立工作目录,隔离项目文件。
  4. cd ansible:进入工作目录,后续所有操作在此目录执行。
shell 复制代码
vim ansible.cfg

[defaults]
inventory=inventory
shell 复制代码
vim inventory

node1
node2
  • ansible.cfg是 ansible 主配置文件。
  • inventory=inventory:指定主机清单文件名字叫inventory,存放在当前目录,inventory 文件定义要管理哪些机器。
    连通性测试
shell 复制代码
ansible all -k -m ping
  • all:代表清单里所有主机(node1+node2)
  • -k:(ask_password) 提示输入 ssh 密码;如果已经配置 ssh 免密,这个参数可以去掉。
  • -m ping:调用ping模块,ansible 内置模块,不是系统 ping 命令,用来检测 ansible 能不能连通目标主机。

✅成功返回:SUCCESS;失败返回报错,排查 ssh、网络、inventory、hosts 解析

例如:

shell 复制代码
node2 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}
node1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}
相关推荐
杜子不疼.1 天前
Ansible 批量部署 Node Exporter:从单机安装流程到远程主机统一执行
ai·ansible
运维全栈笔记2 天前
Ansible 自动化 Nginx 集群部署实战:动态 Inventory、滚动发布与 Role 工程化
nginx·自动化·ansible
User_芊芊君子7 天前
让异构增量同步飞起来-KFS全链路并行同步方案
运维·自动化·ansible
あ-9 天前
ansible-playbook安装seaweedfs三节点集群
ansible
Tassel_YUE11 天前
非 ansible 主机批量执行命令方法:psssh 和 pscp(随手记)
linux·网络·ssh·scp·ansible
.柒宇.21 天前
运维常见面试题_07_配置管理与监控(Ansible · Prometheus · Alertmanager)· 容器编排(Kubernetes)
运维·k8s·ansible·prometheus
相顾若初见21 天前
Ansible部署k8s
容器·kubernetes·ansible
wzq11_66622 天前
ansible部署项目——基于tidb数据库安装zabbix_server
ansible·zabbix·tidb
吃不吃早饭22 天前
RHEL9 搭建 Harbor 私有仓库与 Ansible 自动化部署 Kubernetes 运行环境完整实践
kubernetes·自动化·ansible