登录
/
注册
首页
论坛
其它
首页
科技
业界
安全
程序
广播
Follow
关于
签到
每天签到奖励2-10圆
导读
排行榜
TG频道
发帖说明
登录
/
注册
账号
自动登录
找回密码
密码
登录
立即注册
搜索
搜索
关闭
CSDN热搜
程序园
精品问答
技术交流
资源下载
本版
帖子
用户
软件
问答
教程
代码
写记录
VIP申请
VIP网盘
网盘
联系我们
发帖说明
每日签到
道具
勋章
任务
淘帖
动态
分享
留言板
导读
设置
我的收藏
退出
腾讯QQ
微信登录
返回列表
首页
›
业界区
›
安全
›
使用Ansible为集群初始化并配置免密
使用Ansible为集群初始化并配置免密
[ 复制链接 ]
求几少
2025-6-9 13:06:23
使用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
集群
初始化
配置
相关帖子
使用EB tresos对配置can、icu
C# 使用 using 关键字间接实现只读局部变量的方法
使用Scalar.AspNetCore来管理你的OpenApi
WPF/C#:使用Microsoft Agent Framework框架创建一个带有审批功能的终端Agent
还在发愁怎么配置VSCode?一篇文章教会你!
关于2233看板娘的配置方法
FFmpeg开发笔记(八十四)使用国产的librestreaming实现RTMP直播
使用PySide6/PyQt6实现自定义窗口布局,实现类似FluentWindow效果
使用 Github Pages 和 Hexo 搭建博客
vip免费申请,1年只需15美金$
回复
使用道具
举报
提升卡
置顶卡
沉默卡
喧嚣卡
变色卡
千斤顶
照妖镜
相关推荐
安全
使用EB tresos对配置can、icu
0
648
胥望雅
2025-10-16
安全
C# 使用 using 关键字间接实现只读局部变量的方法
0
284
东门芳洲
2025-10-17
业界
使用Scalar.AspNetCore来管理你的OpenApi
0
367
揿纰潦
2025-10-17
业界
WPF/C#:使用Microsoft Agent Framework框架创建一个带有审批功能的终端Agent
0
993
谅潭好
2025-10-18
安全
还在发愁怎么配置VSCode?一篇文章教会你!
0
237
蝙俚
2025-10-18
业界
关于2233看板娘的配置方法
0
497
吕梓美
2025-10-18
业界
FFmpeg开发笔记(八十四)使用国产的librestreaming实现RTMP直播
0
582
讹过畔
2025-10-19
业界
使用PySide6/PyQt6实现自定义窗口布局,实现类似FluentWindow效果
0
407
吉芷雁
2025-10-19
业界
使用 Github Pages 和 Hexo 搭建博客
0
80
荡俊屯
2025-10-20
高级模式
B
Color
Image
Link
Quote
Code
Smilies
您需要登录后才可以回帖
登录
|
立即注册
回复
本版积分规则
回帖并转播
回帖后跳转到最后一页
签约作者
程序园优秀签约作者
发帖
求几少
2025-6-9 13:06:23
关注
0
粉丝关注
12
主题发布
板块介绍填写区域,请于后台编辑
财富榜{圆}
anyue1937
9994893
dage888
999994
3934307807
992122
4
富账慕
9983
5
邹语彤
9982
6
二艰糖
10000
7
刎唇
9993
8
匝抽
9986
9
聚怪闩
9960
10
孙淼淼
9977
查看更多