写一份 .kitchen.yml 同时在 Ubuntu 22.04 与 openEuler 测试同一 SLS。
解读
面试官想通过一份可落地的 Test Kitchen 配置,验证候选人是否具备以下能力:
- 在国内网络环境下快速拉取 Ubuntu 与 openEuler 镜像;
- 用单一配置文件驱动两套操作系统,保证 SLS 的幂等性与兼容性;
- 把 SaltStack 的 masterless 模式与 Test Kitchen 生命周期打通,实现本地 CI 前置验证;
- 对 kitchen-salt 插件的进阶参数(如
is_file_root、pillars、dependencies)有精准控制,避免“跑通 demo 却上不了生产”的尴尬。
该题背后隐藏的真实诉求是:“让你证明你写的 SLS 在国产操作系统同样能稳定落地,且可重复、可回滚、可审计。”
知识点
- kitchen-salt 插件的
provision_command与salt_bootstrap_options如何兼容 Ubuntu 与 openEuler 的包管理差异(apt vs dnf); - 国内镜像源加速:Ubuntu 使用
mirrors.aliyun.com,openEuler 使用repo.openeuler.org; - inspec 后端验证框架在国产 OS 上的 backend 设置(
os-family: linux与release匹配); - 单一 SLS 在 Jinja2 里用
grains.osfinger做条件分支,确保“一份代码,两处运行”; - kitchen.yml 的
lifecycle: post_create钩子,可在容器启动后先行注入公钥或修正 systemd,避免 openEuler 22.03 LTS 在 Docker 下因cgroupsv2导致 service 启动失败; - matrix 并发理念:通过
KITCHEN_YAML环境变量与kitchen test --concurrency 2实现双平台并行,节省 CI 时间; - 合规审计:在
.kitchen.yml同级目录保留test/integration/default/controls目录,方便后续对接国内等保 2.0/3.0 的基线检查脚本。
答案
# .kitchen.yml
---
driver:
name: docker
use_sudo: false
binary: docker
privileged: true
run_command: /usr/lib/systemd/systemd
volume:
- /sys/fs/cgroup:/sys/fs/cgroup:ro
provisioner:
name: salt_solo
is_file_root: true
local_salt_root: .
salt_install: bootstrap
salt_bootstrap_options: -X -x python3 -M -N -r
pillars:
top.sls:
base:
'*':
- common
state_top:
base:
'*':
- nginx
platforms:
- name: ubuntu-22.04
driver_config:
image: mirrors.aliyun.com/library/ubuntu:22.04
platform: ubuntu
run_command: /bin/systemd
provision_command:
- sed -i 's@http://.*.ubuntu.com@http://mirrors.aliyun.com@g' /etc/apt/sources.list
- apt-get update && apt-get install -y python3 sudo curl systemd
- name: openEuler-22.03-lts
driver_config:
image: repo.openeuler.org/openeuler/openEuler:22.03-lts
platform: centos
run_command: /usr/lib/systemd/systemd
provision_command:
- dnf makecache
- dnf install -y python3 sudo curl systemd
- systemctl enable sshd
verifier:
name: inspec
inspec_tests:
- path: test/integration/default
suites:
- name: default
verifier:
inspec_tests:
- test/integration/default
attributes:
关键说明:
- image 字段显式指向国内镜像仓库,CI 节点无需翻墙即可拉取;
- platform: centos 是 Docker 官方对 openEuler 的兼容标签,可让 kitchen-docker 正确识别 systemd 路径;
- salt_bootstrap_options 中的
-M -N关闭 master 安装,保证纯 minion 端自测; - state_top 指向同一套
nginx.sls,内部用grains.osfinger判断 apt/dnf,实现“一份 SLS,两处运行”; - inspec 后端统一走
linux,在controls/nginx_spec.rb里用os.name区分断言,避免硬编码。
拓展思考
- 国产化替代场景:若客户要求银河麒麟 V10 SP2,可复用同一模板,只需把
driver_config.image替换成内网 Harbor 地址,并在provision_command里注入麒麟的repo文件; - CI 集成:在 GitLab-CI 中国自建节点上,通过
docker:dindservice 与KITCHEN_LOCAL_YAML变量,可在 Merge Request 阶段并行跑 Ubuntu+openEuler,10 分钟内给出双平台报告,实现“准入门禁”; - SLS 兼容性设计:把 OS 差异收敛到
map.jinja文件,测试阶段用kitchen verify --destroy=never保留容器,手动kitchen login进去salt-call --local state.sls nginx test=true,可快速定位 Jinja2 渲染差异; - 合规输出:在
test/integration/default/controls里新增describe service('nginx')与describe security_policy用例,可直接对接国内等保工具链,一份测试报告同时满足运维、安全、审计三方诉求,大幅提升面试时的“落地可信度”。