20 分钟建立一个 Ansible 实验室

系统 Linux
本文将解释如何在 20 分钟内以完全自动化的方式在本地机器上部署自己的实验室环境。

[[361417]]

建立一个支持学习和实验新软件的环境。

能够构建和拆解公有云环境是非常有用的,但我们大多数人都不能轻松访问公有云。退而求其次的最好办法就是在本地机器上建立一个实验室,但即使在本地机器上运行也会带来性能、灵活性和其他挑战。大多数时候,本地机器上额外的工作负载会干扰我们日常的工作,它们当然也会影响你提供一个现成的环境来玩耍和实验新软件。

几年前,当我和我的团队开始学习 Ansible 时,我们就遇到了这个挑战。我们找不到一个可以单独使用的环境,我们对这种情况的失望导致我们中的一些人停止了实验。我们知道需要找到一个解决方案。

我们花了很多时间研究各种方案,得出了一套工具,使我们的好奇心能够在我们完全控制的环境中学习。我们可以在本地机器上轮换和拆解实验室环境,而不需要访问内部实验室或公共云。

本文将解释如何在 20 分钟内以完全自动化的方式在本地机器上部署自己的实验室环境。

你可以在我的 GitHub 仓库中找到这个练习的所有代码。

工具和软件

本方案使用以下工具和软件:

  • Ansible 是我们选择的自动化工具,因为它易于使用,而且足够灵活,可以满足实验室的要求。
  • Vagrant 易于使用,用于构建和维护虚拟机。
  • VirtualBox 是一个托管管理程序,可以在 Windows 和 Linux 环境中使用。
  • Fedora v30+ 是我本地机器上的操作系统。

你必须进行以下设置才能建立环境:

  • 一个互联网连接
  • 在 BIOS 中启用虚拟化技术支持(以下是在我的联想笔记本上的过程
  • Vagrant v2.2.9
  • 最新版本的 Ansible
  • 最新版本的 VirtualBox
  • Fedora v30+ 宿主机操作系统

这个实验室环境有什么?

这个项目旨在部署一个带有 Ansible 引擎和多个 Linux 节点的 Ansible 主机,以及一些预加载和预配置的应用程序(httpd 和 MySQL)。它还启用了 Cockpit,这样你就可以在测试过程中监控虚拟机(VM)的状态。使用预部署的应用程序的原因是为了提高效率(所以你不必花时间安装这些组件)。这样你就可以专注于创建角色和剧本,并针对上述工具部署的环境进行测试。

我们确定,对于我们的用例来说,最好的方案是多机 Vagrant 环境。Vagrant 文件创建了三个 CentOS 虚拟机,以模拟两个目标主机和一个 Ansible 控制机。

  • Host1: 没有图形用户界面(GUI),安装 httpd 和 MySQL
  • Host2: 没有 GUI,安装了 httpd 和 MySQL
  • Ansible-host:没有 GUI,安装了 Ansible 引擎

启用多个管理程序

如果使用了多个管理程序,一些管理程序可能不允许你拉起虚拟机。要解决这个问题,请遵循以下步骤(基于 Vagrant 的安装说明)。

首先,找出管理程序的名称:

  1. $ lsmod | grep kvm
  2. kvm_intel             204800  6
  3. kvm                   593920  1 kvm_intel
  4. irqbypass              16384  1 kvm

我感兴趣的是 kvm_intel,但你可能需要另一个(比如 kvm_amd)。

以 root 身份运行以下内容,将该管理程序列入黑名单:

  1. $ echo 'blacklist kvm-intel' >> /etc/modprobe.d/blacklist.conf

重新启动你的机器并尝试再次运行 Vagrant。

Vagrant 文件

  1. cat Vagrantfile
  1. # -*- mode: ruby -*-
  2. # vi: set ft=ruby :
  3.  
  4. Vagrant.configure("2") do |config|
  5. # Define VMs with static private IP addresses, vcpu, memory and vagrant-box.
  6. boxes = [
  7. {
  8. :name => "web1.demo.com", Host1 this is one of the target nodes
  9. :box => "centos/8", OS version
  10. :ram => 1024, Allocated memory
  11. :vcpu => 1, Allocated CPU
  12. :ip => "192.168.29.2" Allocated IP address of the node
  13. },
  14. {
  15. :name => "web2.demo.com", Host2 this is one of the target nodes
  16. :box => "centos/8",
  17. :ram => 1024,
  18. :vcpu => 1,
  19. :ip => "192.168.29.3"
  20. },
  21. {
  22. :name => "ansible-host", Ansible Host with Ansible Engine
  23. :box => "centos/8",
  24. :ram => 8048,
  25. :vcpu => 1,
  26. :ip => "192.168.29.4"
  27. }
  28. ]
  29.  
  30. # Provision each of the VMs.
  31. boxes.each do |opts|
  32. config.vm.define opts[:name] do |config|
  33. # Only Enable this if you are connecting to Proxy server
  34. # config.proxy.http = "http://usernam:password@x.y:80" Needed if you have a proxy
  35. # config.proxy.https = "http://usernam:password@x.y:80"
  36. # config.proxy.no_proxy = "localhost,127.0.0.1"
  37. config.vm.synced_folder ".", "/vagrant", id: "vagrant-root", disabled: true
  38. config.ssh.insert_key = false
  39. config.vm.box = opts[:box]
  40. config.vm.hostname = opts[:name]
  41. config.vm.provider :virtualbox do |v| Defines the vagrant provider
  42. v.memory = opts[:ram]
  43. v.cpus = opts[:vcpu]
  44. end
  45. config.vm.network :private_network, ip: opts[:ip]
  46. config.vm.provision :file do |file|
  47. file.source = './keys/vagrant' vagrant keys to allow access to the nodes
  48. file.destination = '/tmp/vagrant' the location to copy the vagrant key
  49. end
  50. config.vm.provision :shell, path: "bootstrap-node.sh" script that copy hosts entry
  51. config.vm.provision :ansible do |ansible| declaration to run ansible playbook
  52. ansible.verbose = "v"
  53. ansible.playbook = "playbook.yml" the playbook used to configure the hosts
  54. end
  55. end
  56. end
  57. end

这些是你需要注意的重要文件。

  • inventory-test.yaml:连接到节点的清单文件
  • playbook.yaml:Vagrant 供应者调用的用于配置节点的剧本文件
  • `Vagrantfile':Vagrant 用来部署环境的文件
  • Vagrant 密钥文件:连接实验室环境中各节点的 Vagrant 密钥

你可以根据你的需要调整这些文件。Ansible 的灵活性使你有能力根据你的需要声明性地改变你的环境。

部署你的实验室环境

首先,克隆这个 GitHub 仓库 中的代码:

  1. $ git clone https://github.com/mikecali/ansible-labs-101.git
  2. Cloning into 'ansible-labs-101'...
  3. remote: Enumerating objects: 15, done.
  4. remote: Counting objects: 100% (15/15), done.
  5. remote: Compressing objects: 100% (13/13), done.
  6. remote: Total 15 (delta 2), reused 10 (delta 0), pack-reused 0
  7. Unpacking objects: 100% (15/15), 6.82 KiB | 634.00 KiB/s, done.

接下来,将你的目录改为 vagrant-session-2,并查看其内容:

  1. $ ls
  2. Bootstrap-node.sh   inventory   keys   playbook.yml   README.md Vagrantfile

现在你已经拥有了实验室环境所需的所有工件和配置文件。要部署环境,请运行:

  1. $ vagrant up

只要有一个像样的网络连接,只需要 20 分钟左右就可以得到一个运行环境:

  1. $ vagrant up
  2. Bringing machine 'web1.demo.com' up with 'virtualbox' provider...
  3. Bringing machine 'web2.demo.com' up with 'virtualbox' provider...
  4. Bringing machine 'ansible-host' up with 'virtualbox' provider...
  5. ==> web1.demo.com: Importing base box 'centos/8'...
  6. ==> web1.demo.com: Matching MAC address for NAT networking...
  7. ==> web1.demo.com: Checking if box 'centos/8' version '1905.1' is up to date...
  8. ==> web1.demo.com: Setting the name of the VM: ansible-labs_web1democom_1606434176593_70913
  9. ==> web1.demo.com: Clearing any previously set network interfaces...
  10. ==> web1.demo.com: Preparing network interfaces based on configuration...
  11. web1.demo.com: Adapter 1: nat
  12. web1.demo.com: Adapter 2: hostonly
  13. ==> web1.demo.com: Forwarding ports...
  14. web1.demo.com: 22 (guest) => 2222 (host) (adapter 1)
  15. ==> web1.demo.com: Running 'pre-boot' VM customizations...
  16. ==> web1.demo.com: Booting VM...
  17. ==> web1.demo.com: Waiting for machine to boot. This may take a few minutes...
  18. web1.demo.com: SSH address: 127.0.0.1:2222
  19. web1.demo.com: SSH username: vagrant
  20. web1.demo.com: SSH auth method: private key
  21. [...]

一旦该剧本执行完成,你会看到这样的输出:

  1. PLAY RECAP *********************************
  2. Ansible-host     : ok=20 changed=11 unreachable=0 failed=0 skipped=0 rescued=0 ignored=3
  3.  
  4. Real 18m14.288s
  5. User 2m26.978s
  6. Sys 0m26.849s

确认所有虚拟机都在运行:

  1. $ vagrant status
  2. Current machine states:
  3.  
  4. Web1.demo.com    running (virtualbox)
  5. Web2.demo.com    running (virtualbox)
  6. ansible-host     running (virtualbox)
  7. [...]

你可以通过登录其中一个虚拟机进一步调查。访问 ansible-host

  1. > vagrant ssh ansible-host
  2. Activate the web console with: systemctl enable --now cockpit.socket
  3.  
  4. Last login: Thu Nov 26 12:21:23 2020 from 10.0.2.2
  5. [vagrant@ansible-host ~] uptime
  6. 16:46:42 up 1:24, 1 user, load average: 0.00, 0.01, 0.04

最后,你可以使用 Ansible 模块来 ping 你创建的其他节点:

  1. [vagrant@ansible-host]$ ansible -i inventory-test.yaml \
  2. webservers -m ping -u vagrant
  3. 192.168.29.2 | SUCCESS => {
  4.   "Ansible-facts": {
  5.       "Discovered_interpreter_python": "/usr/libexec/platform-python"
  6.     },
  7.     "Changed": false;
  8.     "Ping": "pong"
  9. }
  10. [...]

清理

运行如下命令来清理环境:

  1. $ vagrant destroy [vagrant machine name]

你的输出会像这样:

 

Output from cleaning up environment

有创意的学习

在自己的实验室里利用自己的时间学习 Ansible 这样的软件是一个好习惯,但由于受到无法控制的限制,可能会很困难。

有时候,你需要发挥创意,找到另一种方法。在开源社区中,你可以选择很多方案;我们选择这些工具的主要原因之一是,它们是许多人常用和熟悉的。

 

另外,请注意,这些剧本并没有按照我的要求进行优化。

 

责任编辑:庞桂玉 来源: Linux中国
相关推荐

2021-04-03 21:40:51

硬件防火墙家庭实验室

2021-12-30 09:40:33

CentOS家庭实验室Linux

2009-06-25 19:03:37

2023-11-29 14:18:32

2021-08-04 09:48:05

数字化

2015-10-10 11:23:49

2015-02-06 09:23:52

赛可达实验室网络安全

2011-05-25 17:51:40

2015-07-16 16:28:13

Testin云测

2020-07-14 18:00:33

树莓派网络文件系统Linux

2009-09-21 11:45:36

CCIE实验室考试

2014-11-27 10:52:17

vlan路由

2014-06-16 14:45:26

曙光EB级云存储实验室

2010-08-23 14:01:25

互联网

2013-04-07 09:38:00

HPC硬件HPC高性能计算

2023-08-09 15:11:26

FedoraLinux实验室

2014-04-08 20:40:01

华为OpenDayligh

2009-09-01 16:41:00

思科认证CCIEEdify

2009-11-07 20:55:30

2010-08-24 11:28:26

点赞
收藏

51CTO技术栈公众号