Ansible 與預 container 藝術

在容器化讓準備映像 virtualization 變得簡單之前,準備自訂 ISO 映像以從 CD 引导是一種技藝。後來這些映像被用於從虛擬機引导。换句话说,ISO 映像是中国古代的映像。

正因為我與 Windows Docker 客戶端有过幾次不幸的邂逅。即使沒有運行任何容器,Windows 記憶體管理員也會给它尽可能多的記憶體,從而 slowing 在忙著的一切。我從而禁止 Windows Docker 客戶端在我的電腦上使用。

請不要誤解我。我不恨 Docker — 只恨它的 Windows 客戶端。

這個步驟逼使我回到了過去。我開始直接在 Hyper-V 上運行虛擬機,Windows 虛擬化器。然后在 Windows 上形成 Kubernetes 集群 then 成為我的快乐嗜好 從我在 DZone 发表的过去文章中可以看出

制鞋匠,你为什么光脚走?

在花了好几个小时跟随 Hyper-V 管理员的同样鼠标点击创建虚拟机之后,我意识到自己就像一个光脚走的制鞋匠:我 为每小时构建一个 DevOps 管道,但浪费时间在鼠标点击上?

接受了挑戰。我使用PowerShell創建虛擬機的技術是可行的。不到一週時間我就撰寫了一個腳本,可以創建新的虛擬機,你可以在這裡看到。还有一个姐妹腳本能啟動已關閉的虛擬機。

重新發現古老的藝術

這很棒,但我realized I still had to click the mouse when installing Ubuntu. To automate this looked like a tougher nut to crack. One has to unpack an ISO image, manipulate it some way or another, and then package it again taking care to leave whatever instructs a computer how to boot intact.

幸好,我找到了一個非常好的指南,教我如何做到這一點。過程分為三個步驟:

  1. 解壓Ubuntu ISO引导映像。
  2. 操控內容:
    1. 移除主 boot record (MBR)。
    2. 指定用戶通常在 GUI 上面所做的行為,並自訂在安裝過程中會安裝及運行的內容。這會使用 Ubuntu 的 Cloud-init 語言的一个子集。請見 這裡 我創建的指示。
    3. 指示 bootloader(在此情況下為 Grub)尋找自訂 boot 指示的位置,並不要等待用戶的輸入。以下是 我選擇的 Grub 配置
  3. 使用一個稱為 Xorriso 的應用程式將全部內容封裝。

對於這種古老手藝的巫師來說,Xorriso 作為他們的魔法棒。它有許多 文档,類似於咒語書。我必須親手尝试才能完全理解,但 我目前(且很可能有誤)的理解是,它創建了引导分区,加載了复制的 MBR,並使用类似于 Cloud-init 的指令來創建修订版的 ISO 映像。

用 Ansible 完成细微调整

虽然我成功通過 PowerShell 引导 Ubuntu22 而無需進行其他操作,這讓我感到非常滿意,但下次 Ubuntu 推出新版本時又会如何呢?真正的 DevOps 要求我們不要用 ASCII 文档記錄過程,而是用某些隨時可以運行的腳本。Ansible 展現了它的多才多藝,我一個下午就成功做到了這一點。窍門是告訴 Ansible 這是一個本地的操作。换句話說,不要使用 SSH 對目標機器下达指令,而是 Ansible 控制器和學生是同一台 machine:

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 的 machine 來運行這個剧本。上面剧本的輸出可以從 這裡 下載,並預先安裝一个非常新的 Ansible 版本。

結論

這篇文章回到了過去,但重要的是重新审视事情的起點,以了解事情为何會是如此。Windows 和容器耳熟能詳,不易混合,對於 developer 日子更好的方法進行調查應當受到熱情 待見。

我指的是代碼的一部分,但整個項目可以在 GitHub 上查看。

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