如何设计 Ansible 的入门工作坊
本月在公司内部做了一次 Ansible 的入门工作坊。本文即对这次工作坊的设计过程进行一次总结。其他技术类的工作坊也可以参考。 设计过程大概过程如下文所述。 首先,我们需要确定参加本次工作坊的受众。他们是否具有最基本的前提。本次工作坊的受众有开发、测试、运维,还有毕业生。但是他们都会使用 shell。这已经满足最基本的前提。同时,了解受众后了,也就可以因材施教。 第二,分析工作坊的内容。Ansible 是一款上手非常容易的自动化运维工具。它的特点就是实操性非常强,不需要理解 Ansible 背后的概念就可以使用的工具。 笔者根据受众和教学内容的特点,得出本次工作坊的目标(教学目标): 知道 Ansible 是什么,并知道它的作用。 了解如何查文档。 能部署一个 Spring Boot 应用。 是不是很简单?其实不然。整个工作坊没有一个人能完成所有的任务。同时发现有运维和开发基础的同学会做得更快。 那接下来怎么实现这个目标呢?笔者使用的是任务驱动的方法。也就是受众通过做一个个任务,在任务中完成学习。同时,教师可以任务过程穿插讲相关的知识点。 以下为任务列表: 执行 ansible-playbook -i hosts playbook.yml 成功 创建用户 apps 及用户组 apps: user 模块: https://docs.ansible.com/ansible/latest/modules/user_module.html group 模块: https://docs.ansible.com/ansible/latest/modules/group_module.html 创建以下文件夹,并设置文件夹的用户和组为 apps: /apps,/apps/hello,/apps/hello/bin,/apps/hello/logs file 模块: https://docs.ansible.com/ansible/latest/modules/file_module.html 将 helloworld-0.0.2.jar copy 到 /apps/hello/bin 目录下,设置该 jar 文件的用户和用户组为 apps copy 模块: https://docs.ansible.com/ansible/latest/modules/copy_module.html 使用 template 模块将 app.service copy 到目标服务器的 /etc/systemd/system 中,并重命名 hello.service : template 模块: https://docs.ansible.com/ansible/latest/modules/template_module.html 启动 hello 服务 service 模块: https://docs.ansible.com/ansible/latest/modules/service_module.html 监听 hello 服务是否启动成功 wait_for 模块: https://docs.ansible.com/ansible/latest/modules/wait_for_module.html 为目标机器安装 JDK 1.8: 在本地仓库中创建 roles 目录 clone 代码:https://github.com/geerlingguy/ansible-role-java 到 roles 目录中 在 playbook.yml 文件中加入 ansible-role-java 的role 创建自定义 role: hello role 进入 roles 目录:cd roles 使用命令生成 role 模板:ansible-galaxy init hello 将 hello 的部署逻辑(在 playbook.yml 中)写入到 hello role 中 将 hello 部署到多台机器 需要修改 hosts 文件 多环境部署 任务的设计并不是随意的,而是有意的。比如: ...