Ansible基础(复习1)

Ansible 是一款开源的自动化工具,广泛应用于配置管理、应用部署、任务自动化以及多节点管理等领域。

Ansible官方文档:https://docs.ansible.com/ansible/latest/

Ansible官方安装方法:https://docs.ansible.com/projects/ansible/latest/installation_guide/intro_installation.html#control-node-requirements

|----------------------|------------|---------------------------------------|
| 设备类型 | 默认连接方式 | 说明 |
| Linux系统 | SSH | 默认最常见方式,免密或密钥登录 |
| Windows系统 | WinRM | 需配置WinRM服务,支持HTTP/HTTPS |
| 网络设备 | SSH或API | 使用特点模块/Collection,部分设备需NETCONF |
| 云平台 | API(HTTPS) | 利用SDK/REST API,需提供Access Key、Token等 |
| Kubernetes/OpenShift | API Server | 连接kube-apiserver,使用kubeconfig或Token |
| Docker/Podman | 本地或远程API | 通过Unix socket或远程TCP端口 |
| 存储/虚拟化设备 | API或SSH | 如VMware使用vCenter API,Dell EMC用RESTAPI |
| HTTP服务 | URI模块 | ansible.builtin.uri 可调用REST接口 |
| 自定义服务 | 命令/脚本/API | 可用command,shell,script模块调用 |

本文以Linux系统为主。

一、环境准备

1、rhel 9.3

2、准备三台虚拟机,并配置网络

|---------|---------------|------------|-----------|
| 主机名 | IP | 系统 | 说明 |
| ansible | 192.168.72.63 | Redhat 9.3 | 安装ansible |
| node1 | 192.168.72.64 | Redhat 9.3 | |
| node2 | 192.168.72.65 | Redhat 9.3 | |

3、下载安装ansible和sshpass

sshpass简单来说就是设置免密登录

4、再ansible主机上映射被控节点并测试是否连通

排除网络故障,为了后续的ansible指令正常执行

复制代码
[root@ansible ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.72.64 node1
192.168.72.65 node2
[root@ansible ~]# ping -c 2 node1
PING node1 (192.168.72.64) 56(84) bytes of data.
64 bytes from node1 (192.168.72.64): icmp_seq=1 ttl=64 time=1.83 ms
64 bytes from node1 (192.168.72.64): icmp_seq=2 ttl=64 time=0.711 ms

--- node1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 0.711/1.272/1.833/0.561 ms
[root@ansible ~]# ping -c 2 node2
PING node2 (192.168.72.65) 56(84) bytes of data.
64 bytes from node2 (192.168.72.65): icmp_seq=1 ttl=64 time=1.57 ms
64 bytes from node2 (192.168.72.65): icmp_seq=2 ttl=64 time=0.587 ms

--- node2 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 0.587/1.076/1.565/0.489 ms

5、关闭防火墙

复制代码
[root@ansible ~]# systemctl disable firewalld
[root@ansible ~]# systemctl status firewalld
○ firewalld.service - firewalld - dynamic firewall daemon
     Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; preset: enabled)
     Active: inactive (dead)
       Docs: man:firewalld(1)

6、关闭SELinux(防止出现未知问题)

复制代码
[root@ansible ~]# sed -i "s/SELINUX=enforcing/SELINUX=permissive/g" /etc/selinux/config
[root@ansible ~]# grep permissive /etc/selinux/config
#     permissive - SELinux prints warnings instead of enforcing.
SELINUX=permissive

7、配置免密登录

生成.ssh文件,默认位置在~/.ssh/,文件夹中id_rsa为私钥,id_rsa.pub为公钥,其中-t rsa为设置加密的方式为rsa

复制代码
[root@ansible ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa
Your public key has been saved in /root/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:sk4vLhHUXg6r10w4rs83ZFBqEeyk5K7REnCxppYPhrU root@ansible
The key's randomart image is:
+---[RSA 3072]----+
|  .. o..         |
|. ..o * o        |
| o++ = X         |
|.+o.+ X o        |
|o=E+ =.*S        |
|o = = oo=        |
|   = +oo         |
|  . o+..o        |
|     +=o..       |
+----[SHA256]-----+

将pub公钥交给node1和node2以实现免密登录

复制代码
[root@ansible ~]# ssh-copy-id node1
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host 'node1 (192.168.72.64)' can't be established.
ED25519 key fingerprint is SHA256:iWgbD/D/01ERqLbv0C9V+APJJ8t2fJ/Ysa/FKBSzcCY.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@node1's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'node1'"
and check to make sure that only the key(s) you wanted were added.

[root@ansible ~]# ssh-copy-id node2
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host 'node2 (192.168.72.65)' can't be established.
ED25519 key fingerprint is SHA256:iWgbD/D/01ERqLbv0C9V+APJJ8t2fJ/Ysa/FKBSzcCY.
This host key is known by the following other names/addresses:
    ~/.ssh/known_hosts:1: node1
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@node2's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'node2'"
and check to make sure that only the key(s) you wanted were added.

测试是否设置成功

复制代码
[root@ansible ~]# ssh node1
Register this system with Red Hat Insights: insights-client --register
Create an account or view all your systems at https://red.ht/insights-dashboard
Last login: Mon Apr  6 21:22:37 2026 from 192.168.72.63
[root@node1 ~]# exit
logout
Connection to node1 closed.
[root@ansible ~]# ssh node2
Register this system with Red Hat Insights: insights-client --register
Create an account or view all your systems at https://red.ht/insights-dashboard
Last login: Mon Apr  6 20:39:28 2026 from 192.168.72.1
[root@node2 ~]# exit
logout
Connection to node2 closed.

二、简单使用

复制代码
[root@ansible ~]# mkdir demo1
[root@ansible ~]# cd demo1/
[root@ansible demo1]# ls
[root@ansible demo1]# vim ansible.cfg
[root@ansible demo1]# vim inventory
[root@ansible demo1]# ansible all -m ping
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"
}
[root@ansible demo1]# 

如果此处不是SUCCESS需检查网络配置是否正确

本文只做了简单的操作,为后续操作打基础,下篇将通过具体实验来运用ansible。

相关推荐
东坡大表哥7 分钟前
【Ubuntu】Ubuntu 20.04 LTS → 22.04 LTS 升级实录
android·linux·ubuntu
程序员agions27 分钟前
Docker Compose 生产环境配置记录
运维·docker·容器
郝学胜-神的一滴30 分钟前
Effective Python 条款45:巧用属性注解平滑升级类属性
运维·开发语言·数据库·python·网络协议·numpy
Mikko737 分钟前
Redis 主从切换期间可用吗?6.2 实测哨兵切换:应用不可写 33 秒,主从连不上、数据不一致,以及设了 ACL 后哨兵不切换
java·运维·redis·后端
进击的荆棘2 小时前
Linux网络——应用层协议HTTP
linux·运维·服务器·网络·http
迪康软件zz3 小时前
敏感文件判定引擎:从单词命中到加权打分
运维·安全·自动化运维
jipinxiaofeihua4 小时前
(文章7) 04章-C++异常处理(下)
linux·运维·网络
cpolar技术支持4 小时前
rsync 只看「大小 + 修改时间」:源文件改成 `CCCC-v3`、目标还是 `DDDD-v4`,三条同步命令 rc 全是 0
linux·rsync·cpolar·nas·数据同步
yyzc1111115 小时前
当中小微实体店面临经营困境时如何借助顺成汇创旗下荟生意品牌实现数字化智慧营销
大数据·运维·人工智能
Rsingstarzengjx18 小时前
第三十九章 Linux PWM 驱动实验
linux·运维·服务器