Ansible与容器前的艺术

在容器化让为虚拟化准备映像变得如此简单之前,准备自定义 ISO 映像从 CD 启动是一门艺术。后来,这些映像被用来启动虚拟机。换句话说,ISO 映像是容器映像的前身。

正是因为如此,我与 Windows Docker 客户端发生了几次不幸的冲突。即使在不运行任何容器的情况下,Windows 内存管理器也会将尽可能多的内存交给它,以减缓我正在忙碌的工作。因此,我禁止在我的机器上使用 Windows Docker 客户端。

请不要误会我的意思。我并不讨厌 Docker,只是讨厌它的 Windows 客户端。

这一步迫使我回到过去。我开始直接在 Windows 虚拟机管理程序 Hyper-V 上运行虚拟机。

Shoemaker, Why Do You Go Barefoot?

在按照相同的鼠标点击操作在 Hyper-V Manager 上创建虚拟机长达数小时之后,我意识到自己就像一个赤脚的鞋匠:我以小时计费构建 DevOps 管道,却把时间浪费在鼠标点击上?

接受挑战。我通过 duckduckgo 阅读到可以使用 PowerShell 创建虚拟机。不用一周的时间,我就编写了一个创建新虚拟机的脚本,如此处所示。

An Old Art Rediscovered

这很棒,但我意识到在安装 Ubuntu 时我仍在点击鼠标。要实现自动化,看起来是一个更难攻克的难题。我们必须解压缩 ISO 映像,以某种方式对其进行处理,然后再次打包,并注意保持指示计算机如何启动的内容完好无损。

幸运的是,我找到了一份非常出色的指南,介绍了如何做到这一点。该过程包括三个步骤:

  1. 解压 Ubuntu ISO 启动映像。
  2. 操作内容:
    1. 移动主引导记录(MBR)。
    2. 指定用户通常在图形用户界面(GUI)上进行哪些操作,并在安装过程中定制安装和运行的内容。这通过使用Ubuntu的Cloud-init语言的一个子集来实现。具体指导见此地
    3. 指导启动加载器(此处为Grub)寻找自定义启动指令,并且不要等待用户输入。这是我所确定的Grub配置
  3. 使用一个名为Xorriso的应用程序进行打包。

对于这项古老工艺的巫师来说,Xorriso充当着他们的魔杖。它的文档页面看起来像一本咒语书。我必须亲自动手才能完全理解,但我目前(很可能是有误的)理解是,它创建了引导分区,加载了复制的MBR,并使用类似于Cloud-init的指令来创建一个修正的ISO镜像。

为完成 touches 使用Ansible

虽然我能够不输入任何内容就从PowerShell成功引导Ubuntu22,但下次Ubuntu发布新版本时又会怎样呢?真正的DevOps要求我们不仅用ASCII文档记录过程,而且要用某种脚本在需要时运行。Ansible在其多功能性方面显示出优势,我仅用一个下午就完成了这件事。秘诀是指导Ansible执行一个本地操作。也就是说,不要使用SSH来为目标机器发送指令,而是Ansible控制器也是学生:

YAML

 

- hosts: localhost
  connection: local

完整的剧本接下来给出,并提供了上面解释内容的另一种视角:

YAML

 

# stamp_images.yml

- hosts: localhost
  connection: local
  become: true
  vars_prompt:
    - name: "base_iso_location"
      prompt: "Enter the path to the base image"
      private: no
      default: /tmp/ubuntu-22.04.4-live-server-amd64.iso

  tasks:
    - name: Install 7Zip
      ansible.builtin.apt:
        name: p7zip-full
        state: present

    - name: Install Xorriso
      ansible.builtin.apt:
        name: xorriso
        state: present

    - name: Unpack ISO
      ansible.builtin.command: 
        cmd: "7z -y x {{ base_iso_location }} -o/tmp/source-files"

    - name: Copy boot partitions
      ansible.builtin.copy:
        src: /tmp/source-files/[BOOT]/
        dest: /tmp/BOOT
  
    - name: Delete working boot partitions
      ansible.builtin.file:
        path:  /tmp/source-files/[BOOT]
        state: absent
  
    - name: Copy files for Ubuntu Bare
      ansible.builtin.copy:
        src: bare/source-files/bare_ubuntu
        dest: /tmp/source-files/

    - name: Copy boot config for Ubuntu bare
      ansible.builtin.copy:
        src: bare/source-files/boot/grub/grub.cfg
        dest: /tmp/source-files/boot/grub/grub.cfg

    - name: Stamp bare image
      ansible.builtin.command: 
        cmd: xorriso -as mkisofs -r   -V 'Ubuntu 22.04 LTS AUTO (EFIBIOS)'   -o ../ubuntu-22.04-wormhole-autoinstall-bare_V5_1.iso   --grub2-mbr ../BOOT/1-Boot-NoEmul.img -partition_offset 16   --mbr-force-bootable   -append_partition 2 28732ac11ff8d211ba4b00a0c93ec93b ../BOOT/2-Boot-NoEmul.img   -appended_part_as_gpt   -iso_mbr_part_type a2a0d0ebe5b9334487c068b6b72699c7   -c '/boot.catalog'   -b '/boot/grub/i386-pc/eltorito.img'     -no-emul-boot -boot-load-size 4 -boot-info-table --grub2-boot-info   -eltorito-alt-boot   -e '--interval:appended_partition_2:::'   -no-emul-boot .
        chdir: /tmp/source-files

    - name: Copy files for Ubuntu Atomika
      ansible.builtin.copy:
        src: atomika/source-files/atomika_ubuntu
        dest: /tmp/source-files/

    - name: Copy boot config for Ubuntu Atomika
      ansible.builtin.copy:
        src: atomika/source-files/boot/grub/grub.cfg
        dest: /tmp/source-files/boot/grub/grub.cfg

    - name: Stamp Atomika image
      ansible.builtin.command:
        cmd: xorriso -as mkisofs -r   -V 'Ubuntu 22.04 LTS AUTO (EFIBIOS)'   -o ../ubuntu-22.04-wormhole-autoinstall-atomika_V5_1.iso   --grub2-mbr ../BOOT/1-Boot-NoEmul.img -partition_offset 16   --mbr-force-bootable -append_partition 2 28732ac11ff8d211ba4b00a0c93ec93b ../BOOT/2-Boot-NoEmul.img   -appended_part_as_gpt   -iso_mbr_part_type a2a0d0ebe5b9334487c068b6b72699c7   -c '/boot.catalog'   -b '/boot/grub/i386-pc/eltorito.img'     -no-emul-boot -boot-load-size 4 -boot-info-table --grub2-boot-info   -eltorito-alt-boot   -e '--interval:appended_partition_2:::'   -no-emul-boot .
        chdir: /tmp/source-files        

注意这里使用的Xorriso命令的魔力,用于准备两个图像:一个支持Kubernetes,一个不支持Kubernetes。

唯一的要求是安装有Ansible的机器来运行这个剧本。上述剧本的输出可以从这里下载并预安装一个非常新的Ansible版本。

结论

这篇文章已经过时了,但重温事情的起点,以了解事情为何会变成这样,还是很重要的。此外,Windows 和容器之间的关系并不融洽,因此我们欢迎对如何让开发人员的日子过得更好进行任何调查。

我提到了部分代码,但完整项目可在 GitHub 上查看。

Source:
https://dzone.com/articles/ansible-and-the-pre-container-arts