登录
/
注册
首页
论坛
其它
首页
科技
业界
安全
程序
广播
Follow
关于
每日签到
每天签到奖励2圆-6圆
发帖说明
VIP申请
登录
/
注册
账号
自动登录
找回密码
密码
登录
立即注册
搜索
搜索
关闭
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
集群
初始化
配置
相关帖子
使用Yolo12算法进行区域内实时目标计数
C++ 开发环境配置
SpringBoot使用AOP优雅的实现系统操作日志的持久化!
磁盘性能测试工具FIO-使用教程
Vim在Windows的安装与基本使用
使用unsloth实现LoRA微调
Redis数据持久化方案与集群部署
$\LaTeX{}$之图片使用
$\LaTeX{}$之表格使用
记录 使用PsExec启动System权限的WPF 程序
vip免费申请,1年只需15美金$
回复
使用道具
举报
提升卡
置顶卡
沉默卡
喧嚣卡
变色卡
千斤顶
照妖镜
相关推荐
业界
使用Yolo12算法进行区域内实时目标计数
0
114
孟清妍
2025-09-05
安全
C++ 开发环境配置
0
562
觐有
2025-09-06
业界
SpringBoot使用AOP优雅的实现系统操作日志的持久化!
0
121
马璞玉
2025-09-07
业界
磁盘性能测试工具FIO-使用教程
0
365
吕清莹
2025-09-07
安全
Vim在Windows的安装与基本使用
0
389
福清婉
2025-09-07
安全
使用unsloth实现LoRA微调
0
781
心麾浪
2025-09-07
安全
Redis数据持久化方案与集群部署
0
44
钱艷芳
2025-09-09
科技
$\LaTeX{}$之图片使用
0
76
即息极
2025-09-10
科技
$\LaTeX{}$之表格使用
0
641
袁可佳
2025-09-10
安全
记录 使用PsExec启动System权限的WPF 程序
0
842
姬宜欣
2025-09-10
高级模式
B
Color
Image
Link
Quote
Code
Smilies
您需要登录后才可以回帖
登录
|
立即注册
回复
本版积分规则
回帖并转播
回帖后跳转到最后一页
浏览过的版块
科技
业界
代码
签约作者
程序园优秀签约作者
发帖
求几少
2025-6-9 13:06:23
关注
0
粉丝关注
12
主题发布
板块介绍填写区域,请于后台编辑
财富榜{圆}
敖可
9984
杭环
9988
凶契帽
9988
4
氛疵
9988
5
黎瑞芝
9988
6
猷咎
9986
7
里豳朝
9986
8
肿圬后
9986
9
蝓俟佐
9984
10
虽裘侪
9984
查看更多