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"
}
相关推荐
Helix25018 小时前
Ansible混合环境自动化:Windows与Linux统一配置管理实战
windows·自动化·ansible·playbook·配置管理·winrm·混合环境
垚垚学技术_聚焦云原生2 天前
Ansible 变量体系详解:主机变量、组变量、全局变量优先级实战
ansible·自动化运维·devops·变量优先级
I'mChloe11 天前
不再逐台装Docker:我用Ansible Playbook批量统一服务器环境
服务器·docker·ansible
筑梦之路11 天前
Alibaba Cloud Linux 4 制作 Ansible RPM包实践(离线可复用)——筑梦之路
linux·运维·ansible
张洛闻Eren12 天前
Ansible【第一课】:Ansible介绍
运维·数据库·云原生·kubernetes·github·ansible
张洛闻Eren12 天前
Ansible【第二课】:实施 Ansible Playbook
linux·运维·服务器·云原生·kubernetes·k8s·ansible
励志师code12 天前
Ansible 批量运维实战:从连通性巡检到灰度变更的完整闭环
运维·ansible
哈__17 天前
飞牛OS还在手动维护?我用Ansible把重复操作写成了Playbook
开发语言·ansible·php
垚垚学技术_聚焦云原生1 个月前
Ansible Role 生产环境标准目录结构与规范化实践
java·elasticsearch·ansible
垚垚学技术_聚焦云原生1 个月前
Ansible Playbook 实战:批量配置 Linux 主机
ansible