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"
}
相关推荐
HideF3 天前
Linux ansible
ansible
垚垚学技术_聚焦云原生16 天前
Ansible Vault 安全实践:敏感密码、密钥加密存储方案
安全·elasticsearch·ansible
广目软件17 天前
HA分布式架构采集节点变更操作手册20261029
运维·服务器·ansible·zabbix·prometheus
Moshow郑锴18 天前
Ansible 批量部署 Node.js 服务:ZIP 直解与共享目录分发两种方案对比
node.js·ansible
垚垚学技术_聚焦云原生18 天前
Ansible Playbook 进阶:when 条件、loop 循环、handlers 触发器落地
linux·ansible·自动化运维·devops
Moshow郑锴19 天前
GCP MIG 重建后 Ansible 初始化实战:PowerShell + NSSM 挂载 Windows 服务
windows·ansible
承渊政道21 天前
新增一台服务器时还要重新装Nginx?Ansible只改hosts目标列表,同一份Playbook继续执行
运维·nginx·ansible·内网穿透·cpolar
Helix25022 天前
Ansible混合环境自动化:Windows与Linux统一配置管理实战
windows·自动化·ansible·playbook·配置管理·winrm·混合环境
垚垚学技术_聚焦云原生24 天前
Ansible 变量体系详解:主机变量、组变量、全局变量优先级实战
ansible·自动化运维·devops·变量优先级
I'mChloe1 个月前
不再逐台装Docker:我用Ansible Playbook批量统一服务器环境
服务器·docker·ansible