阿里云Ansible自动化运维平台部署
以下是在阿里云平台上基于Ansible实现自动化运维的完整实践指南,整合所有核心操作流程和命令,适配指定的服务器规划:
一、环境规划
主机名 |
IP地址 |
角色 |
操作系统 |
manage01 |
192.168.98.200/24 |
Ansible控制节点 |
CentOS 7.9 |
node1 |
192.168.98.201/24 |
业务节点 |
CentOS 7.9 |
node2 |
192.168.98.202/24 |
业务节点 |
CentOS 7.9 |
node3 |
192.168.98.203/24 |
业务节点 |
CentOS 7.9 |
二、部署前准备
1. 阿里云安全组配置
- 所有ECS实例安全组放行规则:
- 入方向:TCP 22(SSH)、ICMP
- 出方向:All Traffic
2. 所有节点基础配置
# 1. 关闭防火墙与SELinux(所有节点执行)
systemctl stop firewalld && systemctl disable firewalld
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
setenforce 0
# 2. 配置阿里云内网时间同步
yum install -y chrony
cat > /etc/chrony.conf << EOF
server ntp.aliyun.com iburst
server ntp1.aliyun.com iburst
EOF
systemctl restart chronyd && systemctl enable chronyd
三、Ansible控制节点部署(manage01)
1. 安装Ansible
# 安装EPEL仓库和Ansible
yum install -y epel-release
yum install -y ansible git
# 验证安装
ansible --version # 应显示ansible 2.9+版本
2. 配置SSH免密登录
# 1. 生成密钥对(默认路径)
ssh-keygen -t rsa -b 4096 -N "" -f ~/.ssh/id_rsa
# 2. 分发公钥到所有节点
for node in node1 node2 node3 manage01; do
ssh-copy-id -i ~/.ssh/id_rsa.pub root@$node
done
# 3. 测试连通性
ansible all -m ping -i inventory.ini
四、Ansible核心配置
1. 项目目录结构
mkdir -p ~/ansible-project/{inventory,group_vars,roles,playbooks}
cd ~/ansible-project
2. 主机清单文件
# ~/ansible-project/inventory/production.ini
[management]
manage01 ansible_host=192.168.98.200
[nodes]
node1 ansible_host=192.168.98.201
node2 ansible_host=192.168.98.202
node3 ansible_host=192.168.98.203
[all:vars]
ansible_user=root
ansible_ssh_private_key_file=~/.ssh/id_rsa
ansible_python_interpreter=/usr/bin/python
3. Ansible配置文件
# ~/ansible-project/ansible.cfg
[defaults]
inventory = ./inventory/production.ini
host_key_checking = False
log_path = ./ansible.log
roles_path = ./roles
forks = 20
[privilege_escalation]
become = True
become_method = sudo
become_user = root
become_ask_pass = False
五、基础环境自动化配置
1. 静态IP配置(所有节点)
# ~/ansible-project/playbooks/network_config.yml
---
- name: Configure Static IP
hosts: all
become: yes
vars:
interface: eth0
network_config:
manage01:
ip: 192.168.98.200
gateway: 192.168.98.1
node1:
ip: 192.168.98.201
gateway: 192.168.98.1
node2:
ip: 192.168.98.202
gateway: 192.168.98.1
node3:
ip: 192.168.98.203
gateway: 192.168.98.1
tasks:
- name: Configure network interface
template:
src: templates/ifcfg-eth0.j2
dest: /etc/sysconfig/network-scripts/ifcfg-{{ interface }}
notify: Restart network
handlers:
- name: Restart network
service:
name: network
state: restarted
模板文件 templates/ifcfg-eth0.j2:
DEVICE={{ interface }}
BOOTPROTO=static
ONBOOT=yes
IPADDR={{ network_config[inventory_hostname].ip }}
NETMASK=255.255.255.0
GATEWAY={{ network_config[inventory_hostname].gateway }}
DNS1=100.100.2.136 # 阿里云内网DNS
DNS2=100.100.2.138
2. 主机名配置
# ~/ansible-project/playbooks/hostname_config.yml
---
- name: Set Hostname
hosts: all
become: yes
tasks:
- name: Set system hostname
hostname:
name: "{{ inventory_hostname }}"
- name: Update /etc/hosts
lineinfile:
path: /etc/hosts
regexp: "^{{ ansible_default_ipv4.address }}"
line: "{{ ansible_default_ipv4.address }} {{ inventory_hostname }}"
state: present
执行命令:
ansible-playbook playbooks/network_config.yml
ansible-playbook playbooks/hostname_config.yml
六、核心运维场景实践
场景1:批量安装基础工具
# ~/ansible-project/playbooks/install_essentials.yml
---
- name: Install Base Packages
hosts: nodes
become: yes
tasks:
- name: Install common tools
yum:
name: [vim, wget, telnet, net-tools, lsof]
state: latest
场景2:部署Nginx集群
# ~/ansible-project/roles/nginx/tasks/main.yml
---
- name: Install Nginx
yum:
name: nginx
state: latest
- name: Copy customized config
template:
src: nginx.conf.j2
dest: /etc/nginx/nginx.conf
backup: yes
notify: Restart Nginx
- name: Ensure service running
service:
name: nginx
state: started
enabled: yes
handlers:
- name: Restart Nginx
service:
name: nginx
state: restarted
执行命令:
ansible-playbook playbooks/install_essentials.yml
ansible-playbook -i inventory.ini playbooks/deploy_nginx.yml
七、生产级增强配置
1. 敏感信息加密
# 创建加密文件
ansible-vault create group_vars/all_secrets.yml
# Playbook中调用
- name: Load secrets
include_vars: group_vars/all_secrets.yml
no_log: true
2. 阿里云动态Inventory集成
# 安装阿里云Python SDK
pip install aliyun-python-sdk-ecs
# 动态Inventory脚本示例
# ~/ansible-project/inventory/aliyun_ecs.py
#!/usr/bin/env python
from aliyunsdkcore.client import AcsClient
from aliyunsdkecs.request.v20140526 import DescribeInstancesRequest
client = AcsClient('<ACCESS_KEY>', '<SECRET_KEY>', 'cn-hangzhou')
def main():
request = DescribeInstancesRequest.DescribeInstancesRequest()
response = client.do_action_with_exception(request)
print(format_output(response))
if __name__ == "__main__":
main()
八、验证与监控
1. 服务状态验证
ansible nodes -m shell -a "systemctl status nginx"
ansible nodes -m uri -a "url=http://localhost/health"
2. 阿里云监控集成
# ~/ansible-project/roles/monitoring/tasks/main.yml
- name: Install CloudMonitor Agent
yum:
name: aliyun-cloudmonitor
state: present
- name: Start CloudMonitor
service:
name: cloudmonitor
state: started
enabled: yes
九、运维操作速查表
操作场景 |
命令示例 |
检查节点连通性 |
ansible all -m ping |
批量执行Shell命令 |
ansible nodes -m shell -a "df -h" |
文件分发 |
ansible web -m copy -a "src=app.conf dest=/etc/app/ owner=root" |
服务管理 |
ansible db -m service -a "name=mysql state=restarted" |
安全更新 |
ansible all -m yum -a "name=* state=latest update_cache=yes" |
剧本测试 |
ansible-playbook deploy.yml --check --diff |
加密剧本运行 |
ansible-playbook secure.yml --ask-vault-pass |
通过本指南,您已完成以下核心建设:
- 标准化基础环境:网络、主机名、安全策略统一配置
- 自动化运维体系:Ansible控制节点+被管节点架构
- 生产级最佳实践:动态Inventory、加密管理、监控集成
- 可扩展场景支持:通过Roles机制快速扩展新服务部署
后续建议:
- 使用Git进行配置版本管理
- 定期执行ansible-playbook --check验证配置漂移
- 通过阿里云OOS实现Ansible任务调度
- 使用Ansible Tower/AWX实现可视化运维
参考资料:
- https://github.com/ansible/ansible
- https://www.redhat.com/en/technologies/management/ansible
- https://ansible-tran.readthedocs.io/en/latest/docs/intro.html
学习视频:
阿里云Ansible自动化运维平台部署的更多相关文章
- OMS自动化运维平台部署
OMS自动化运维平台部署 一.基础环境安装 yum -y install mariadb mariadb-devel mariadb-server wget epel-release python-d ...
- 【I·M·U_Ops】------Ⅰ------ IMU自动化运维平台设想
说明本脚本仅作为学习使用,请勿用于任何商业用途.本文为原创,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接和本声明. #A 搞这个平台的初心 由于之前呆的单位所有IT相关硬件资源都要我们 ...
- 三石之道之Ansible自动化运维工具部署
centos6默认python版本为2.6 centos7默认python版本为2.7 ansible需要最低python2.7的支持 总结:centos6要部署ansible工具,需要先升级pyth ...
- 用友iuap云运维平台支持基于K8s的微服务架构
什么是微服务架构? 微服务(MicroServices)架构是当前互联网业界的一个技术热点,业内各公司也都纷纷开展微服务化体系建设.微服务架构的本质,是用一些功能比较明确.业务比较精练的服务去解决更大 ...
- 实战:阿里巴巴 DevOps 转型后的运维平台建设
导读:阿里巴巴DevOps转型之后,运维平台是如何建设的?阿里巴巴高级技术专家陈喻结合运维自身的理解,业务场景的分析和业界方法论的一些思考,得出来一些最佳实践分享给大家. 前言 “我是这个应用 ...
- 阿里巴巴 DevOps 转型后的运维平台建设
原文:http://www.sohu.com/a/156724220_262549 本文转载自公众号「DevOps 时代」,高效运维社区致力于陪伴您的职业生涯,与您一起愉快的成长. 作者简介: 陈喻( ...
- (1)Linux常用的运维平台和工具
运维工程师使用的运维平台和工具包括: Web服务器:apache.tomcat.nginx.lighttpd 监控:nagios.ganglia.cacti.zabbix 自动部署:ansible.s ...
- Ansible 自动化运维工具
Ansible 自动化运维工具 Ansible是什么? Ansible是一个"配置管理工具"也是一个"自动化运维工具" Ansible 作用: Ansible是 ...
- 简单聊一聊Ansible自动化运维
一.Ansible概述 Ansible是今年来越来越火的一款开源运维自动化工具,通过Ansible可以实现运维自动化,提高运维工程师的工作效率,减少人为失误.Ansible通过本身集成的非常丰富的模块 ...
- 技术沙龙|京东云DevOps自动化运维技术实践
自动化测试体系不完善.缺少自助式的持续交付平台.系统间耦合度高服务拆分难度大.成熟的DevOps工程师稀缺,缺少敏捷文化--这些都是DevOps 在落地过程中,或多或少会碰到的问题,DevOps发展任 ...
随机推荐
- 还堵在高速路上吗?带你进入Scratch世界带你飞
国庆假期高速路的风景 国庆假期正式启动人从众模式,无论是高速公路还是景区,不管是去程还是回程,每一次都堪称经典. 一些网友在经历漫长的拥堵后 哭笑不得地表示 "假期都在堵车中度过了" ...
- SnowFlake雪花算法
简介 自然界不存在两片完全一样的雪花,每一片都是独一无二的,雪花算法的命名由此而来,所有雪花算法表示生成的ID唯一,且生成的ID是按照一定的结构组成. 组成结构 上图可以看到雪花算法的结构由四部分组成 ...
- 丢掉WebView,使用JS+Rust开发跨端桌面应用-Deft
简介 随着Web技术的发展,越来越多的跨端应用选择了WebView作为基础解决方案.诚然WebView让跨端应用开发变得简单了很多,极大的提高了开发效率,但是,WebView也存在着一些广为诟病的缺点 ...
- keycloak~refresh_token的标准化
内容大纲 refresh_token作用 使用方法 refresh_token规范 keycloak开启refresh_token的限制 refresh_token时的错误汇总 keycloak中re ...
- 【BUUCTF】AreUSerialz
[BUUCTF]AreUSerialz (反序列化) 题目来源 收录于:BUUCTF 网鼎杯 2020 青龙组 题目描述 根据PHP代码进行反序列化 <?php include("fl ...
- Visual Studio 好用的主题+字体推荐!!!
Vs2022主题+字体 Visual Studio(VS)是一款功能强大的集成开发环境(IDE),可以用于开发各种类型的应用程序,包括桌面应用.Web应用.移动应用等.它提供了许多主题设置和字体选项, ...
- AI与.NET系列文章之三:在.NET中使用大语言模型(LLMs)
引言 在技术迅猛发展的今天,大语言模型(Large Language Models, LLMs)已成为人工智能领域的核心驱动力之一.从智能对话系统到自动化内容生成,LLMs的应用正在深刻改变我们的工作 ...
- 鸿蒙WebSocket的使用竟如此简单
使用WebSocket建立服务器与客户端的双向连接,需要先通过createWebSocket()方法创建WebSocket对象,然后通过connect()方法连接到服务器.当连接成功后,客户端会收到o ...
- vuex 踩坑记之unknown local mutation type
使用模块化定义vuex时,出现了这么个错误unknown local mutation type,检查好久发现单词并没有写错,代码如下: // 引入请求数据的方法 import { reqUsers ...
- ubuntu install 下载安装包报错 subprocess installed post-installation script returned error exit status 10
前言 在 ubuntu 环境下使用 sudo apt-get install 安装软件包时,会报错 XXX 为安装软件包 dpkg:error processing package XXX (--co ...