当前位置: 首页 > news >正文

【网络运维】Ansible roles:角色管理

Ansible roles:角色管理

实验环境准备

准备实验环境:

[furongwang@controller ~]$ mkdir web && cd web[furongwang@controller web]$ cat > ansible.cfg <<'EOF'
[defaults]
remote_user = furongwang
inventory = ./inventory[privilege_escalation]
become = True
become_user = root
become_method = sudo
become_ask_pass = False
EOF[furongwang@controller web]$ cat > inventory <<'EOF'
controller
node1
node2
node3
node4
EOF

Ansible 角色介绍

在生产环境中,Playbook 往往会变得冗长复杂,包含多个文件、任务和处理程序。随着开发的 Playbook 越来越多,代码重复利用变得尤为重要。

Ansible 角色提供了一种标准化方式,将任务、变量、文件、模板等资源打包在统一目录结构中,只需简单调用即可在不同项目中复用自动化代码。

Ansible 角色结构

通过以下命令初始化一个角色:

[furongwang@controller web]$ ansible-galaxy init furongwang
- furongwang was created successfully
[furongwang@controller web]$ tree furongwang
furongwang
├── defaults
│   └── main.yml
├── files
├── handlers
│   └── main.yml
├── meta
│   └── main.yml
├── README.md
├── tasks
│   └── main.yml
├── templates
├── tests
│   ├── inventory
│   └── test.yml
└── vars└── main.yml8 directories, 8 files

角色目录结构说明

  • defaults:包含角色变量的默认值,优先级较低,易于被覆盖
  • files:存放由角色任务引用的静态文件
  • handlers:包含角色的处理程序定义
  • meta:包含角色元信息,如作者、许可证、平台和依赖项
  • tasks:包含角色的主要任务定义
  • templates:存放由角色任务引用的 Jinja2 模板
  • tests:包含测试用的清单和 Playbook
  • vars:定义角色变量值,优先级较高
  • README.md:提供角色描述、使用文档和示例

Ansible 角色目录位置

Ansible 默认在以下位置查找角色:

  • ~/.ansible/roles
  • /usr/share/ansible/roles
  • /etc/ansible/roles

优先级从上到下依次降低。可以通过 ansible.cfg 中的 roles_path 变量自定义角色路径:

[defaults]
roles_path = ./roles

多个路径使用冒号分隔:

roles_path = /etc/ansible/roles:/home/student/web/roles

创建和使用角色

创建角色

[furongwang@controller web]$ mkdir roles
[furongwang@controller web]$ ansible-galaxy init apache --init-path=./roles

角色任务示例

# tasks/main.yml
---
# tasks file for apache
- name: install webyum:name: "{{ web_package }}"state: latest- name: "start {{ web_service }}"service:name: "{{ web_service }}"state: startedenabled: yes- name: prepare motdtemplate:src: motd.j2dest: /etc/motd- name: prepare furongwang sitetemplate:src: furongwang.conf.j2dest: /etc/httpd/conf.d/furongwang.confnotify:- restart_web- name: prepare DocumentRoot file:path: "/var/www/html/{{ ansible_hostname }}" state: directory- name: prepare index.htmltemplate:src: index.html.j2dest: "/var/www/html/{{ ansible_hostname }}/index.html"

调用角色

---
- name: deploy apachehosts: node1roles:- apache

角色依赖管理

角色可以依赖其他角色,这在构建复杂自动化流程时非常有用:

# roles/apache/meta/main.yml
dependencies:
- role: firewallservice: http

任务执行顺序

Ansible 按照特定顺序执行任务:

  1. pre_tasks 任务
  2. roles 任务
  3. tasks 任务
  4. post_tasks 任务

处理程序执行顺序更为复杂,涉及不同阶段通知的处理程序。

示例:

# 准备环境
[furongwang@controller web]$ ansible-galaxy init test_task_exec_order --init-path=roles
[furongwang@controller web]$ vim roles/test_task_exec_order/tasks/main.yml
---
# tasks file for test_task_exec_order
- name: task in roleshell: echo 'task in role'

测试剧本内容如下:

---
- name: test task execute orderhosts: node1gather_facts: falsepre_tasks:- name: task in pre_tasksshell: echo 'task in pre_tasks'roles:- test_task_exec_ordertasks:- name: task in tasksshell: echo 'task in tasks'post_tasks:- name: task in post_tasksshell: echo 'task in post_tasks'

模拟执行结果:(使用了新目录)
在这里插入图片描述

Handlers 执行顺序

play中存在handlers任务,则按以下顺序执行:

  1. pre_tasks任务
  2. pre_tasks通知的handlers任务
  3. roles任务
  4. tasks 任务
  5. roles通知的handlers任务
  6. tasks通知的handlers任务
  7. post_tasks任务
  8. post_tasks通知的handlers任务

示例:

# 准备环境
[furongwang@controller web]$ vim roles/test_task_exec_order/tasks/main.yml
---
# tasks file for test_task_exec_order
- name: task in roleshell: echo 'task in role'notify:- role_handler[furongwang@controller web]$ vim roles/test_task_exec_order/handlers/main.yml
---
# handlers file for test_task_exec_order
- name: role_handlershell: echo handle in role

测试剧本内容如下:

---
- name: test task execute orderhosts: node1gather_facts: falsepre_tasks:- name: task in pre_tasksshell: echo 'task in pre_tasks'notify:- iamhandlerroles:- test_task_exec_ordertasks:- name: task in tasksshell: echo 'task in tasks'notify:- iamhandlerpost_tasks:- name: task in post_tasksshell: echo 'task in post_tasks'notify:- iamhandler handlers:- name: iamhandlershell: echo iamhandler

执行结果如下:(使用了新目录)
在这里插入图片描述

include_role 和 import_role 模块

这两个模块允许在任务级别调用角色:

---
- hosts: node1tasks:    - shell: echo 'first task'- name: use role in tasksimport_role:name: hello

主要区别:

  • import_role 在 Playbook 执行前解析角色
  • include_role 在任务执行时解析角色

Ansible 角色优势

  • 标准化系统配置的基本要素
  • 支持并行开发,提高团队协作效率
  • 提高代码重用率,易于共享
  • 使大型项目更易于管理

开发角色最佳实践

  1. 不要在角色中存储敏感信息
  2. 使用 ansible-galaxy init 初始化角色
  3. 维护完整的 README.md 和 meta/main.yml 文件
  4. 保持角色功能专注单一
  5. 经常重用和重构角色
  6. 使用版本控制系统管理角色

使用系统角色

RHEL 系统提供了预置的系统角色:

[furongwang@controller ~]$ sudo yum install -y rhel-system-roles

时间同步角色示例

---
- name: Time Synchronizationhosts: node1vars:timesync_ntp_servers:- hostname: classroom.example.com   iburst: yestimezone: "Asia/Shanghai"roles:- rhel-system-roles.timesynctasks:- name: Set timezonetimezone:name: "{{ timezone }}"

使用 Ansible Galaxy 部署角色

Ansible Galaxy 是一个由社区管理的角色仓库,包含大量可重用的角色。

常用命令

# 搜索角色
[furongwang@controller ~]$ ansible-galaxy search --platforms=EL haproxy# 查看角色信息
[furongwang@controller ~]$ ansible-galaxy info geerlingguy.docker# 安装角色
[furongwang@controller ~]$ ansible-galaxy install geerlingguy.haproxy# 批量安装角色
[furongwang@controller ~]$ ansible-galaxy install -r requires.yml# 查看本地角色清单
[furongwang@controller web]$ ansible-galaxy list
# /home/student/web/roles
- apache, (unknown version)
- firewall, (unknown version)
- test_task_exec_order, (unknown version)# 角色管理其他命令
# 登入Ansible galaxy
[furongwang@controller ~]$ ansible-galaxy login# 删除Ansible galaxy中角色
[furongwang@controller ~]$ ansible-galaxy delete# 删除本地角色
[furongwang@controller ~]$ ansible-galaxy remove
# 或者rm删除相应目录# 登出Ansible galaxy
[furongwang@controller ~]$ ansible-galaxy logout

总结

​ Ansible 角色是提升自动化代码复用性和维护性的重要工具。通过标准化目录结构、清晰的变量管理和依赖关系,角色使得复杂自动化任务的开发和维护变得更加高效。结合 Ansible Galaxy 社区资源,可以快速获取经过验证的高质量角色,大幅提升自动化项目开发效率。

​ 掌握 Ansible 角色管理,不仅能够提高个人工作效率,还能促进团队协作和知识共享,是每个 Ansible 用户必备的重要技能。

http://www.xdnf.cn/news/1326979.html

相关文章:

  • Android Studio Git提交环境变量问题总结
  • NestJS 依赖注入方式全解
  • 源代码安装部署lamp
  • AI Deep Research 思维链简介
  • 微服务-06.微服务拆分-拆分原则
  • 云手机矩阵:重构企业云办公架构的技术路径与实践落地
  • OpenSSL与OpenSSH的关系
  • SpringBoot-集成POI和EasyExecl
  • PG靶机 - Pebbles
  • 瑞萨e2studio:HardwareDebug配置项详解
  • 主从切换是怎么保证数据一致的?从库为什么会延迟
  • ReLens「Focus DSLR 大光圈虚化相机」v4.1.2 f 解锁付款版 —一款专业大光圈和单反级背景虚化编辑软件
  • 如何将任意文件一键转为PDF?
  • 计算机大数据毕业设计推荐:基于Spark的气候疾病传播可视化分析系统【Hadoop、python、spark】
  • AJAX (一)
  • F003疫情传染病数据可视化vue+flask+mysql
  • JavaSSM框架从入门到精通!第二天(MyBatis(一))!
  • flink+clinkhouse安装部署
  • 当GitHub宕机时,我们如何协作
  • Netty内存池中ChunkList详解
  • 决策树算法学习笔记
  • 设计模式笔记_行为型_解释器模式
  • 技术分享:跨域问题的由来与解决
  • 关于诸多编程语言类型介绍
  • AP6275S AMPAK正基WiFi6模块方案与应用
  • 链表-2.两数相加-力扣(LeetCode)
  • 中科米堆CASAIM自动化三维测量设备测量汽车壳体直径尺寸
  • 丝杆支撑座在自动化生产线中的关键支撑
  • Java -- 用户线程和守护线程--线程同步机制
  • ios使用saveVideoToPhotosAlbum 保存视频失败提示 invalid video