登录
/
注册
首页
论坛
其它
首页
科技
业界
安全
程序
广播
Follow
关于
导读
排行榜
资讯
发帖说明
登录
/
注册
账号
自动登录
找回密码
密码
登录
立即注册
搜索
搜索
关闭
CSDN热搜
程序园
精品问答
技术交流
资源下载
本版
帖子
用户
软件
问答
教程
代码
写记录
写博客
小组
VIP申请
VIP网盘
网盘
联系我们
发帖说明
道具
勋章
任务
淘帖
动态
分享
留言板
导读
设置
我的收藏
退出
腾讯QQ
微信登录
返回列表
首页
›
业界区
›
安全
›
使用Ansible为集群初始化并配置免密
使用Ansible为集群初始化并配置免密
[ 复制链接 ]
求几少
2025-6-9 13:06:23
猛犸象科技工作室:
网站开发,备案域名,渗透,服务器出租,DDOS/CC攻击,TG加粉引流
使用Ansible为集群初始化并配置免密
前情概要
集群的36台服务器安装好了centos7.9设置了统一的root密码,并配置好了主机名和ip。现在需要实现:
每台关闭防火墙和selinux
删除安装操作系统时创建的默认用户user及其家目录
将集群的36台主机和ip信息添加到/etc/hosts文件
删除默认yum源配置文件,添加指定的repo文件
为集群36台主机配置ssh相互免密
Ansible实现
感觉Ansible比使用脚本来得更方便,所以使用Ansible。
playbook的yaml文件:
---
- name: Initialize servers
hosts: all_servers
gather_facts: no
become: no
tasks:
- name: Disable firewall
service:
name: firewalld
state: stopped
enabled: no
- name: Disable SELinux
selinux:
state: disabled
policy: targeted
- name: Disable SELinux immediately
command: setenforce 0
ignore_errors: yes
- name: Ensure user is absent and home directory removed
user:
name: user
state: absent
remove: yes
- name: Remove default yum repos
file:
path: "{{ item }}"
state: absent
with_fileglob:
- /etc/yum.repos.d/*.repo
- name: Copy http.repo to all servers
copy:
src: /root/http.repo
dest: /etc/yum.repos.d/http.repo
owner: root
group: root
mode: '0644'
- name: Add hostname into /etc/hosts
lineinfile:
path: /etc/hosts
line: "{{ hostvars[item]['ansible_host'] }} {{ item }}"
state: present
create: yes
regexp: "^{{ hostvars[item]['ansible_host'] }}\\s+{{ item }}$"
with_items: "{{ groups['all_servers'] }}"
- name: Check /root/.ssh exists
file:
path: /root/.ssh
state: directory
mode: '0700'
- name: Check id_rsa exists
stat:
path: /root/.ssh/id_rsa
register: ssh_key
- name: Generate SSH keypair if not already present
openssh_keypair:
path: /root/.ssh/id_rsa
type: rsa
size: 2048
state: present
mode: '0600'
when: not ssh_key.stat.exists
- name: Gather SSH public keys from all servers
slurp:
src: /root/.ssh/id_rsa.pub
register: public_key
- name: Set up authorized_keys for all servers
authorized_key:
user: root
key: "{{ hostvars[item]['public_key']['content'] | b64decode }}"
state: present
with_items: "{{ groups['all_servers'] }}"
复制代码
inventory文件
[all_servers]
hpc_mgr_1 ansible_user=root ansible_host=10.2.1.9 ansible_connection=local
hpc_mgr_2 ansible_user=root ansible_host=10.2.1.11
hpc_node_1 ansible_user=root ansible_host=10.2.1.13
hpc_node_2 ansible_user=root ansible_host=10.2.1.15
hpc_node_3 ansible_user=root ansible_host=10.2.1.17
hpc_node_4 ansible_user=root ansible_host=10.2.1.19
hpc_node_5 ansible_user=root ansible_host=10.2.1.21
hpc_node_6 ansible_user=root ansible_host=10.2.1.23
hpc_node_7 ansible_user=root ansible_host=10.2.1.25
hpc_node_8 ansible_user=root ansible_host=10.2.1.27
hpc_node_9 ansible_user=root ansible_host=10.2.1.29
hpc_node_10 ansible_user=root ansible_host=10.2.1.31
hpc_node_11 ansible_user=root ansible_host=10.2.1.33
hpc_node_12 ansible_user=root ansible_host=10.2.1.35
hpc_node_13 ansible_user=root ansible_host=10.2.1.37
hpc_node_14 ansible_user=root ansible_host=10.2.1.39
hpc_node_15 ansible_user=root ansible_host=10.2.1.41
hpc_node_16 ansible_user=root ansible_host=10.2.1.43
hpc_node_17 ansible_user=root ansible_host=10.2.1.45
hpc_node_18 ansible_user=root ansible_host=10.2.1.47
hpc_node_19 ansible_user=root ansible_host=10.2.1.49
hpc_node_20 ansible_user=root ansible_host=10.2.1.51
hpc_node_21 ansible_user=root ansible_host=10.2.1.53
hpc_node_22 ansible_user=root ansible_host=10.2.1.55
hpc_node_23 ansible_user=root ansible_host=10.2.1.57
hpc_node_24 ansible_user=root ansible_host=10.2.1.59
hpc_node_25 ansible_user=root ansible_host=10.2.1.61
hpc_node_26 ansible_user=root ansible_host=10.2.1.63
hpc_node_27 ansible_user=root ansible_host=10.2.1.65
hpc_node_28 ansible_user=root ansible_host=10.2.1.67
hpc_node_29 ansible_user=root ansible_host=10.2.1.69
hpc_node_30 ansible_user=root ansible_host=10.2.1.71
hpc_node_31 ansible_user=root ansible_host=10.2.1.73
hpc_node_32 ansible_user=root ansible_host=10.2.1.75
hpc_fnode_1 ansible_user=root ansible_host=10.2.1.77
hpc_fnode_2 ansible_user=root ansible_host=10.2.1.79
复制代码
执行playbook:
ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook -i inventory.ini a.yaml --ask-pass
复制代码
总结
临时使用,体验很不错。
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
使用
Ansible
集群
初始化
配置
相关帖子
如何使用 vxe-gantt table 甘特图来实现多个维度视图展示
记录 Windows系统开启hyper-v ,部分端口被保留,导致端口不能使用而报错的问题
Claude Code 使用 Skills
ACP:让 AI 编程工具配置从此告别碎片化 —— 一款开源的 AI 配置管理平台
如何使用DashVector的多向量检索
追踪链路--使用iptables/ipvs来记录后端pod真实ip
使用Python免费合并PDF文件
使用DNGuard加密并打包C# .NET Core程序为单一EXE文件
最小二乘问题详解9:使用Ceres求解非线性最小二乘
Kali2025.4+Cherry Studio一键配置HexStrike AI全自动渗透测试助手全教程
回复
使用道具
举报
提升卡
置顶卡
沉默卡
喧嚣卡
变色卡
千斤顶
照妖镜
相关推荐
代码
如何使用 vxe-gantt table 甘特图来实现多个维度视图展示
2
801
晚能
2025-12-13
安全
记录 Windows系统开启hyper-v ,部分端口被保留,导致端口不能使用而报错的问题
0
210
溜椎干
2025-12-15
安全
Claude Code 使用 Skills
1
230
王妍芳
2025-12-16
业界
ACP:让 AI 编程工具配置从此告别碎片化 —— 一款开源的 AI 配置管理平台
0
780
哈妙思
2025-12-16
业界
如何使用DashVector的多向量检索
0
270
别萧玉
2025-12-16
业界
追踪链路--使用iptables/ipvs来记录后端pod真实ip
0
800
硫辨姥
2025-12-17
安全
使用Python免费合并PDF文件
0
599
洪势
2025-12-18
安全
使用DNGuard加密并打包C# .NET Core程序为单一EXE文件
2
742
嫁吱裨
2025-12-19
业界
最小二乘问题详解9:使用Ceres求解非线性最小二乘
0
496
挚魉
2025-12-19
安全
Kali2025.4+Cherry Studio一键配置HexStrike AI全自动渗透测试助手全教程
0
1012
遇玷
2025-12-20
回复
(2)
拙因
2025-11-7 23:19:04
回复
使用道具
举报
照妖镜
猛犸象科技工作室:
网站开发,备案域名,渗透,服务器出租,DDOS/CC攻击,TG加粉引流
很好很强大 我过来先占个楼 待编辑
王平莹
2025-11-29 14:36:57
回复
使用道具
举报
照妖镜
程序园永久vip申请,500美金$,无限下载程序园所有程序/软件/数据/等
新版吗?好像是停更了吧。
高级模式
B
Color
Image
Link
Quote
Code
Smilies
您需要登录后才可以回帖
登录
|
立即注册
回复
本版积分规则
回帖并转播
回帖后跳转到最后一页
浏览过的版块
业界
签约作者
程序园优秀签约作者
发帖
求几少
2025-11-29 14:36:57
关注
0
粉丝关注
13
主题发布
板块介绍填写区域,请于后台编辑
财富榜{圆}
3934307807
991124
anyue1937
9994892
kk14977
6845357
4
xiangqian
638210
5
韶又彤
9997
6
宋子
9981
7
闰咄阅
9993
8
刎唇
9993
9
俞瑛瑶
9998
10
蓬森莉
9950
查看更多
今日好文热榜
688
当遇见 CatchAdmin V5-模块化设计重新定义
490
【有手就行】SWIFT:花20分钟把大模型的名
559
论文速读记录 | 2025.12(2)
373
浮点数的本质:为什么计算机无法精确表示0.
726
Flink源码阅读:如何生成JobGraph
928
Python 潮流周刊#132:30 年 Python 自由职
481
大模型榜单周报(2025/12/20)
157
【节点】[LinearToGammaSpaceExact节点]原
789
Aspire 与 Azure Functions 深度集成:架构
1003
阿里Z-Image图像生成模型容器部署
310
痞子衡嵌入式:16MB以上NOR Flash地址模式
668
最新AI换脸软件,全面升级可直播,Mirage下
451
热点 Key 与大 Key 治理——识别、拆分、预
646
Media Extended
330
vlookup的终结者splookup,9个案例讲透查询
872
ROS2之Launch介绍
895
精选 8 个 .NET 开发实用的类库,效率提升
110
精选 8 个 .NET 开发实用的类库,效率提升
600
精选 8 个 .NET 开发实用的类库,效率提升
173
精选 8 个 .NET 开发实用的类库,效率提升