在 20 分钟内搭建 Ansible 实验室

构建一个环境以支持学习和实验新软件。
99 位读者喜欢这个。
Science lab with beakers

能够构建和拆除公共云环境非常有用,但我们大多数人无法轻松访问公共云。次好的选择是在本地机器上拥有一个实验室,但即使在本地机器上运行也会带来性能、灵活性和其他挑战。大多数时候,本地机器上的额外工作负载会干扰我们的日常工作,并且它们肯定会阻止我们拥有一个随时可用的环境来玩耍和实验新软件。

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

我们花费了大量时间研究各种选项,并提出了一套工具,使我们能够在完全受控的环境中满足我们的学习好奇心。我们可以在本地机器上启动和拆除实验室环境,而无需访问本地实验室或公共云。

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

您可以在我的 GitHub 存储库中找到此练习的所有代码。

工具和软件

此解决方案使用以下工具和软件

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

您必须设置以下内容才能构建环境

  • 互联网连接
  • BIOS 中启用的虚拟化技术支持(这是我的 Lenovo 笔记本电脑的步骤
  • Vagrant v2.2.9
  • 最新版本的 Ansible
  • 最新版本的 VirtualBox
  • Fedora v30+ 主机操作系统

实验室环境中有哪些内容?

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

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

  • 主机 1:无图形用户界面 (GUI),已安装 httpd 和 MySQL
  • 主机 2:无 GUI,已安装 httpd 和 MySQL
  • Ansible-host:无 GUI,已安装 Ansible 引擎

启用多个虚拟机监控程序

如果使用多个虚拟机监控程序,某些虚拟机监控程序可能不允许您启动 VM。要解决此问题,请按照以下步骤操作(基于 Vagrant 的 安装 说明)。

首先,找出虚拟机监控程序的名称

$ lsmod | grep kvm
kvm_intel             204800  6
kvm                   593920  1 kvm_intel
irqbypass              16384  1 kvm

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

以 root 身份运行以下命令以列入黑名单虚拟机监控程序

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

重启您的机器并再次尝试运行 Vagrant。

Vagrant 文件

cat Vagrantfile 
# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
# Define VMs with static private IP addresses, vcpu, memory and vagrant-box.
  boxes = [
    {
      :name => "web1.demo.com", ⇒ Host1 this is one of the target nodes
      :box => "centos/8",	      ⇒ OS version 
      :ram => 1024,		      ⇒ Allocated memory
      :vcpu => 1,               ⇒  Allocated CPU
      :ip => "192.168.29.2"     ⇒ Allocated IP address of the node
    },
    {
      :name => "web2.demo.com", ⇒ Host2 this is one of the target nodes
      :box => "centos/8",
      :ram => 1024,
      :vcpu => 1,
      :ip => "192.168.29.3"
    },
    {
      :name => "ansible-host", ⇒ Ansible Host with Ansible Engine
      :box => "centos/8",
      :ram => 8048,
      :vcpu => 1,
      :ip => "192.168.29.4"
    }
  ]

  # Provision each of the VMs.
  boxes.each do |opts|
    config.vm.define opts[:name] do |config|
#   Only Enable this if you are connecting to Proxy server
#      config.proxy.http    = "http://usernam:password@x.y:80"⇒ Needed if you have a proxy
#      config.proxy.https   = "http://usernam:password@x.y:80"
#      config.proxy.no_proxy = "localhost,127.0.0.1"
      config.vm.synced_folder ".", "/vagrant", id: "vagrant-root", disabled: true
      config.ssh.insert_key = false
      config.vm.box = opts[:box]
      config.vm.hostname = opts[:name]
      config.vm.provider :virtualbox do |v| ⇒  Defines the vagrant provider
        v.memory = opts[:ram]
        v.cpus = opts[:vcpu]
      end
      config.vm.network :private_network, ip: opts[:ip]
      config.vm.provision :file do |file|
         file.source     = './keys/vagrant' ⇒ vagrant keys to allow access to the nodes
         file.destination    = '/tmp/vagrant' ⇒ the location to copy the vagrant key
      end
      config.vm.provision :shell, path: "bootstrap-node.sh" ⇒ script that copy hosts entry
      config.vm.provision :ansible do |ansible| ⇒ declaration to run ansible playbook
        ansible.verbose = "v"
        ansible.playbook = "playbook.yml" ⇒ the playbook used to configure the hosts
      end
        end
  end
end

这些是您需要注意的重要文件

  • inventory-test.yaml:用于连接到节点的清单文件
  • playbook.yaml:Vagrant provisioner 调用以配置节点的剧本文件
  • Vagrantfile:Vagrant 用于部署环境的文件
  • vagrant keys:用于连接到实验室环境中节点的 Vagrant 密钥

您可以根据需要调整这些文件。Ansible 的灵活性使您能够根据需要声明性地更改您的环境。

部署您的实验室环境

首先,从 GitHub 存储库克隆代码

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

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

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

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

$ vagrant up

在良好的互联网连接下,只需大约 20 分钟即可获得一个运行环境

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

剧本执行完成后,您将看到如下输出

PLAY RECAP *********************************
Ansible-host     : ok=20 changed=11 unreachable=0 failed=0 skipped=0 rescued=0 ignored=3

Real 18m14.288s
User 2m26.978s
Sys 0m26.849s

验证所有 VM 是否正在运行

$ vagrant status
Current machine states:

Web1.demo.com    running (virtualbox)
Web2.demo.com    running (virtualbox)
ansible-host     running (virtualbox)
[...]

您可以通过登录其中一个 VM 来进一步调查。访问 ansible-host

> vagrant ssh ansible-host
Activate the web console with: systemctl enable --now cockpit.socket

Last login: Thu Nov 26 12:21:23 2020 from 10.0.2.2
[vagrant@ansible-host ~] uptime
16:46:42 up 1:24, 1 user, load average: 0.00, 0.01, 0.04

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

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

清理

通过运行以下命令清理您的环境

$ vagrant destroy [vagrant machine name]

您的输出将如下所示

发挥创意来学习

在您自己的实验室中利用自己的时间学习 Ansible 等软件是一个好习惯,但由于超出您控制范围的限制,这可能会很困难。

有时,您需要发挥创意并找到另一种方法。开源社区中有许多选项可供您选择;我们选择这些工具的主要原因之一是它们被广泛使用并且许多人熟悉。

另外,请注意,这些剧本没有我想象的那么优化。请随时改进它们并在评论中分享您的工作。

接下来阅读什么
标签
ID
Mike Calizo 是 Elastic.co 的首席客户成功经理,专注于政府客户,常驻澳大利亚堪培拉。Mike 认为“数据就是力量”,利用这种力量可以改进组织,使其能够利用自己的见解通过创新实现差异化,并通过成本优化策略提高效率。

评论已关闭。

Creative Commons License本作品根据 Creative Commons Attribution-Share Alike 4.0 International License 获得许可。
© . All rights reserved.