使用 Bolt 在 100 台主机上并行安装 nginx 的命令?
解读
面试官想确认三件事:
- 你是否知道 Bolt 是无 Master 的即时任务引擎,而非传统 Puppet Master-Agent 架构;
- 能否用一条命令完成“并发、并发度可控、错误可重试”的批量安装;
- 是否具备国内运维场景下的“源加速、免密登录、合规审计”意识。
回答时先给出最简可行命令,再主动补充并发度、限速、错误重试、国内源替换、执行用户、sudo 提权、输出格式等工程化细节,体现“能干活、懂规范、可落地”。
知识点
- Bolt 核心概念:task、plan、inventory、concurrency、run-as、sudo-password、format。
- 国内镜像:CentOS 常用阿里云、华为云、清华源;Ubuntu 用清华、中科大、华为源。
- 并发控制:--concurrency 参数默认 100,可降至 20~30 避免镜像源限流。
- 错误处理:--no-strict-host-key-checking、--connect-timeout、--modulepath、--rerun 文件。
- 合规审计:--out json | jq 解析、--log-level debug 留存、/var/log/bolt-run.log。
- 免密前置:提前 bolt command run 'echo "Defaults !requiretty" >> /etc/sudoers.d/99_bolt' 避免 sudo 交互。
答案
最简命令(已配置好 inventory.yaml,用户为 bolt,sudo 免密码):
bolt command run 'yum -y install nginx' --targets web100 --concurrency 50 --run-as root
工程化完整命令(CentOS 场景,带国内源加速、错误重试、JSON 输出):
bolt command run 'sed -e "s|^mirrorlist=|#mirrorlist=|" -e "s|^#baseurl=|baseurl=|" -e "s|mirror.centos.org|mirrors.aliyun.com|g" -i.bak /etc/yum.repos.d/CentOS-*.repo && yum makecache fast && yum -y install nginx' --targets web100 --concurrency 30 --connect-timeout 15 --run-as root --sudo-password "(date +%F).log
Ubuntu 场景对应:
bolt command run 'sed -i "s|http://.*.ubuntu.com|https://mirrors.tuna.tsinghua.edu.cn|g" /etc/apt/sources.list && apt-get update -qq && DEBIAN_FRONTEND=noninteractive apt-get -y install nginx' --targets web100 --concurrency 30 --run-as root --sudo-password "$(cat ~/.ssh/sudo_pass)" --out json
拓展思考
- 幂等性:命令式安装不具备幂等,可封装为 Bolt task 或 Puppet 模块,用 plan 调用,实现“已安装则跳过”。
- 灰度发布:把 100 台拆成 10 组,用 --targets @group1 逐组执行,结合 --rerun 失败列表二次执行。
- 合规基线:安装后立刻用 bolt task run nginx::config --targets web100 下发 /etc/nginx/nginx.conf 模板,并触发 systemctl reload nginx,实现“装完即合规”。
- 审计闭环:把 JSON 结果通过 ELK 或 Loki 收集,字段包括 target、exit_code、stdout、stderr、duration,方便后续合规报表。
- 国产化替代:若主机为麒麟 V10、UOS,需把源替换成对应国产厂商 repo,并用 bolt file upload 提前推送 repo 文件,避免 sed 失效。