命令行参考
适合谁阅读: 你需要知道某个参数的确切名称或行为。
读完后你可以: 写出任意 InvenTag 命令行,并知道哪些参数现在可用。
本页与这一版文档所描述的发行版的 inventag --help 一致。运行 inventag --help 可查看你已安装的发行版的参数。状态列说明针对模拟 AWS 的离线测试显示的结果。
“未测试”表示还没有测试覆盖该行所述的内容。“仅限模拟的 Google”表示测试使用的是模拟的 Google 客户端。详情见功能状态。
命令
inventag [flags] run a scan and write reports
inventag --version print the version and exit
inventag templates list [--kind K] [--all]
inventag templates show NAME|PATH [--resolved]
inventag templates init NAME|PATH --output FILE|FOLDER [--name NAME] [--extend] [--force]
inventag templates validate NAME|PATH [NAME|PATH ...]
inventag selftest check this copy of InvenTag, offline
inventag completion bash|zsh|fish print a shell completion script
inventag 命令在 Linux、macOS 和 Windows 上的用法相同。
未针对 Windows 或 macOS 验证:inventag 命令的行为与在 Linux 上相同。
templates、selftest 和 completion 必须是程序名之后的第一个词。
模板
| 参数 | 默认值 | 状态 | 功能 |
|---|---|---|---|
--template NAME|PATH | 无 | 可用 | 使用模板。可重复指定,一份报告模板、一份标签策略模板和一份配置模板各一个。见模板。 |
templates 子命令
| 命令 | 功能 |
|---|---|
templates list | 列出模板的类型、来源和说明,以及搜索过的文件夹。--kind report|tag-policy|config 用于筛选;--all 还会显示被同名模板遮住的模板。 |
templates show NAME | 打印该文件。--resolved 打印应用 extends 和默认值之后的结果。 |
templates init NAME --output PATH | 把模板复制到你的项目中。--extend 写出一个继承原模板的简短文件;--name 设置名称;--force 覆盖已有文件。 |
templates validate NAME|PATH ... | 检查文件以及它们引用的模板。全部有效时退出码为 0,否则为 1。 |
selftest 和 completion
| 命令 | 作用 |
|---|---|
selftest | 检查这份 InvenTag 是否能正常工作:软件包、内置模板、AWS 服务数据、只读保护,以及写入临时文件夹后删除的一个 Excel 文件和一个 Word 文件。它不调用 AWS,不打开网络连接,也不需要凭证。退出码 0:所有检查通过。退出码 1:有检查失败;其他检查仍会运行。退出码 2:未知参数。 |
completion bash|zsh|fish | 把 shell 补全脚本输出到标准输出。它只读取命令自身的选项,不调用 AWS,也不打开网络连接。退出码 0。缺少 shell 名或 shell 名未知:显示用法行,退出码 2。 |
安装补全脚本,见 命令行补全。命令如下:
source <(inventag completion bash)
inventag completion zsh > "${fpath[1]}/_inventag"
inventag completion fish > ~/.config/fish/completions/inventag.fish
账户
| 参数 | 默认值 | 状态 | 功能 |
|---|---|---|---|
--accounts-file FILE | 无 | 可用 | 从 JSON 或 YAML 文件读取账户。见配置文件。--accounts-file 和 --accounts-prompt 只能给一个(否则退出码 2)。 |
--accounts-prompt | 关 | 可用 | 以交互方式询问账户和凭证。 |
--cross-account-role ARN|NAME | 无 | 可用 | 完整的角色 ARN 会被直接代入;与 --accounts-file 或 --accounts-prompt 一起使用时以退出码 2 停止。角色名称会与 --accounts-file 中的每个账户 ID 组合。只给角色名称而没有 --accounts-file 时,会以退出码 2 停止。 |
输出
| 参数 | 默认值 | 状态 | 功能 |
|---|---|---|---|
--create-excel | 没有其他输出参数时为开 | 可用 | 写出 multi_account_bom_<timestamp>.xlsx。 |
--create-word | 关 | 可用 | 写出 multi_account_bom_<timestamp>.docx。 |
--create-google-docs | 关 | 仅限模拟的 Google | 把 Word 报告创建为 Google 文档。需要 Google 附加组件(pip install "inventag-aws[google]")。见 Google Sheets 和 Docs。 |
--create-google-sheets | 关 | 仅限模拟的 Google | 把 Excel 报告创建为 Google Sheets 电子表格。 |
--google-credentials FILE | 无 | 仅限模拟的 Google | 服务账号密钥文件。不给出时使用应用默认凭证(Application Default Credentials)。 |
--google-oauth-client FILE | 无 | 仅限模拟的 Google | OAuth 客户端文件;你在浏览器中登录。 |
--google-folder-id ID | 无 | 仅限模拟的 Google | 存放文件的 Drive 文件夹。 |
--google-share-with EMAIL | 无 | 仅限模拟的 Google | 与该地址共享。可重复指定以添加更多地址。 |
--google-share-role ROLE | reader | 仅限模拟的 Google | reader、commenter 或 writer。 |
--google-share-anyone | 关 | 仅限模拟的 Google | 任何拿到链接的人都可以读取。不加此参数时文件是私有的。 |
--google-title TITLE | 带日期的名称 | 仅限模拟的 Google | Google 文件的标题。 |
--output-directory DIR | bom_output | 可用 | 存放所有输出文件的文件夹。 |
--per-account-reports | 关 | 可用 | 每个账户写出一个 <account-id>_bom_<timestamp> 文件,而不是一份合并的报告。 |
未针对真实的 Google 验证:此表中的 Google 参数会创建、共享和输出什么。
扫描什么
| 参数 | 默认值 | 状态 | 功能 |
|---|---|---|---|
--account-regions LIST | 每个已启用的区域 | 可用 | 以逗号分隔的区域代码,例如 us-east-1,eu-west-1。IAM、S3 存储桶列表、CloudFront 和 Route 53 始终只从 us-east-1 读取一次,因此无需列出它。 |
--account-services LIST | 全部 | 可用 | 以逗号分隔的服务名称,与 Service 列匹配,例如 EC2,S3。只扫描这些服务,报告中也只保留这些服务。 |
--account-tags JSON | 无 | 可用 | 标签筛选,例如 '{"Environment":"prod"}'。只保留带有这些标签的资源。 |
--fallback-display auto|always|never | auto | 可用 | 是否显示只通过 Resource Groups Tagging API 找到的资源。auto 只对自身发现代码没有返回结果的服务显示它们。 |
--hide-fallback-resources | 关 | 可用 | 等同于 --fallback-display never。已弃用。 |
分析
| 参数 | 默认值 | 状态 | 功能 |
|---|---|---|---|
--finops-checks | 关闭 | 可用 | 对扫描找到的资源运行六项 FinOps 检查(FIN-001 到 FIN-006),并在 Excel 报告末尾添加一个 FinOps 工作表。不再发出任何 AWS 调用,不需要额外的权限,其检查结果也不改变退出码。使用 --template 时不添加任何内容。见 FinOps 检查。 |
--enable-network-analysis | 关 | 可用 | 找到 VPC 时,在 Excel 报告中添加 Network Analysis 工作表。与 --template 一起使用时不添加任何内容。见网络与安全分析。 |
--enable-security-analysis | 关 | 可用 | 找到安全组时,在 Excel 报告中添加 Security Analysis 工作表。与 --template 一起使用时不添加任何内容。 |
--enable-cost-analysis | 关 | 可用 | 在默认工作簿中添加 Cost Analysis 工作表(以及四个 forgotten_analysis.* 列)。会调用 Cost Explorer、Pricing 和 CloudWatch;--no-billing-validation 与它相互独立,不会停止这些调用。 一次运行中的 Cost Explorer 请求:启用账单验证时(默认)每个账户最多一次,使用 --enable-cost-analysis 时整个运行最多再多一次。单账户运行同时使用两者时最多发出 2 次;使用 --no-billing-validation 时最多发出 1 次。被限流的请求会重试,总共最多 10 次尝试,每次重试都是一次新的请求。AWS 对每次请求收费。需要 pricing:GetProducts 和 cloudwatch:GetMetricStatistics。费用是根据 AWS Pricing API 得出的估算值,不是你的账单。 |
--billing-validation, --no-billing-validation | 开 | 可用 | 在发现阶段使用 Cost Explorer 找出有支出的服务。AWS 对每次 Cost Explorer 请求收费。详见Cost Explorer 定价。--no-billing-validation 会停止这些调用;--enable-cost-analysis 与它相互独立,仍会调用 Cost Explorer。 |
未针对真实的 AWS 验证:--enable-cost-analysis 只需要这两项额外权限,并且估算值与 AWS 价格一致。
未针对真实的 AWS 验证:AWS 计费的请求次数,以及 AWS 是否对重试计费。
配置文件
| 参数 | 默认值 | 状态 | 功能 |
|---|---|---|---|
--tag-mappings FILE | 无 | 可用 | 每条映射添加一个命名列。见配置文件。 |
--service-descriptions FILE | 无 | 可用 | 添加 service_description 列。你的条目会替换内置描述。见配置文件。 |
--bom-config FILE | 无 | 可用 | 从 YAML 或 JSON 文件设置 BOM 处理配置的字段。其他键会被忽略,并给出指明这些键的警告。类型错误的值会以退出码 1 停止,并指明该键。 |
--validate-config | 关 | 可用 | 加载并检查配置,然后退出。 |
--validate-config-only | 关 | 可用 | 检查 --accounts-file、--tag-mappings 和 --service-descriptions,然后退出。不会读取 --bom-config。不会进行任何 AWS 调用。 |
报告版式文件
这些参数用一个 YAML 或 JSON 文件改变默认 Excel 和 Word 报告的版式。见单份报告的版式文件。
| 参数 | 默认值 | 状态 | 功能 |
|---|---|---|---|
--word-template PATH | 无 | 可用 | Word 报告的版式文件。需要 --create-word;没有它时运行以退出码 2 停止。 |
--excel-template PATH | 无 | 可用 | Excel 报告的版式文件。只给了 --create-word 时,运行以退出码 2 停止。 |
--print-default-template word|excel | 无 | 可用 | 打印该格式的默认版式文件,然后以退出码 0 退出。 |
--validate-templates | 关 | 可用 | 检查 --word-template 和 --excel-template 给出的文件,然后退出。不调用 AWS。两个参数都没有时:退出码 2。 |
并行处理
| 参数 | 默认值 | 状态 | 功能 |
|---|---|---|---|
--max-concurrent-accounts N | 4 | 可用 | 同时扫描的账户数(至少 1)。 |
--concurrency N | 64 | 可用 | 整个运行中同时进行的 AWS 请求的最大数量(1 到 256)。1 表示一次只发送一个请求。超出 1 到 256 的值,或不是整数的值:退出码 2。在配置模板中:spec.concurrency。见并发。 |
--account-processing-timeout SECONDS | 1800 | 可用 | 每个账户允许的时间,从该账户开始扫描时算起(至少 60)。超时的账户算作失败;它找到的资源保留在报告中。 |
--disable-parallel-processing | 关 | 可用 | 一次只扫描一个账户。它不改变账户内部的线程。 |
--resume | 关 | 可用 | 复用同一条命令之前未完成的运行中已完成账户的保存结果(相同的账户、区域、服务和筛选条件),只扫描其他账户。与 --disable-state-management 一起使用:退出码 2。 |
--resume-max-age-hours HOURS | 24 | 可用 | 与 --resume 一起使用时,只复用不超过这个时长的保存结果(最多 168)。 |
状态与变更跟踪
| 参数 | 默认值 | 状态 | 功能 |
|---|---|---|---|
--enable-state-management | 开 | 可用 | 在 <output>/state/ 中保存快照。 |
--disable-state-management | 关 | 可用 | 不保存快照和变更日志。 |
--enable-delta-detection / --disable-delta-detection | 开 | 可用 | 把新快照与上一份快照比较。--disable-delta-detection 不写出变更报告。 |
--generate-changelog, --no-generate-changelog | 开 | 可用 | 从第二次运行起写出 changelog_<timestamp>.md。--no-generate-changelog 会停止写出。 |
--state-retention-days N | 30 | 可用 | 运行保存新快照时,删除超过 N 天的状态快照。0 或 none 保留任何时长的快照。 |
--state-max-snapshots N | 100 | 可用 | 只保留最新的 N 个状态快照。0 或 none 保留所有快照。 |
可选的退出码
| 参数 | 默认值 | 状态 | 功能 |
|---|---|---|---|
--fail-on-account-error | 关 | 可用 | 有账户无法扫描或超出时限时,以退出码 5 结束。报告仍会写出。不带该参数时,这样的运行以退出码 0 结束,除非所有账户都失败。 |
--fail-if-empty | 关 | 可用 | 没有找到资源或筛选条件把它们全部去掉时,以退出码 6 结束。 |
如果同时满足多个条件,先是 5,然后是 6,然后是 3,最后是 4。退出码 3、4 和 5 会写出报告。
对于退出码 6,带 --template 的运行会写出一份不列出任何资源的报告,不带 --template 的运行则不写文件。
日志
| 参数 | 默认值 | 状态 | 功能 |
|---|---|---|---|
--verbose, -v | 关 | 可用 | 显示每一步。 |
--debug, -d | 关 | 可用 | 显示 AWS 请求。密钥会被遮蔽。 |
--log-file FILE | 无 | 可用 | 同时把详细日志写入该文件。 |
--disable-account-logging | 关 | 可用 | 不在日志行前加账户前缀。 |
凭证
| 参数 | 默认值 | 状态 | 功能 |
|---|---|---|---|
--validate-credentials | 关 | 可用 | 按扫描使用凭证的方式检查每个账户的凭证(对于其他账户中没有凭证的条目,使用跨账户角色),然后退出(全部可用为 0,否则为 1)。 |
--credential-timeout SECONDS | 30 | 可用 | 登录和凭证检查(包括 AssumeRole)的超时时间。 |
--skip-credential-validation | 关 | 可用 | 在运行开始时跳过凭证检查。 |
只读防护与审计
只读防护始终开启。它会阻止每一个不是读取的 AWS 调用,以及会返回密钥或数据的读取调用。
| 参数 | 默认值 | 状态 | 功能 |
|---|---|---|---|
--audit-output FILE | 无 | 可用 | 写出一份 JSON 列表,记录本次运行调用的每个 AWS 操作,包括被允许的和被阻止的。当运行无权使用某个 IAM 操作时,该文件还会有列表 permissions_denied:见仅使用 ReadOnlyAccess 运行。 |
--validate-operations SERVICE:OPERATION ... | 无 | 可用 | 对照防护检查这些操作,然后退出(全部允许为 0,否则为 1)。 |
--enforce-read-only, --security-validation, --enable-production-safety | 开 | 无效果 | 保留这些参数是为了让旧命令仍能运行。防护始终开启。 |
--disable-production-safety | 关 | 以退出码 2 停止 | 防护无法关闭。 |
--compliance-standard ... | 无 | 以退出码 2 停止 | 未实现:无法根据清单检查某个标准。请使用标签策略模板。 |
--risk-threshold LEVEL | 无 | 可用 | 与 --enable-security-analysis 一起使用时:如果有安全组规则达到或超过 LOW、MEDIUM、HIGH 或 CRITICAL,退出码为 4。没有 --enable-security-analysis 时:退出码为 2。 |
ALLOWED ec2:DescribeInstances (read-only prefix)
ALLOWED s3:GetBucketTagging (read-only prefix)
BLOCKED s3:PutObject (operation is not on the read-only allowlist)
BLOCKED secretsmanager:GetSecretValue (operation returns secrets, signed URLs or customer data)
echo $?
1
以退出码 2 停止的参数会打印如下内容(第一个示例来自没有安装 Google 库的机器):
inventag --create-google-docs
inventag - WARNING - - No account specification method provided. Using default AWS credentials.
inventag: error: Google: Google output needs libraries that are not installed (google.auth). Install the Google libraries with: pip install "inventag-aws[google]"
(exit code 2)
inventag --risk-threshold HIGH
inventag: error: --risk-threshold: needs --enable-security-analysis, which produces the risk levels it checks.
(exit code 2)
inventag --compliance-standard soc2
inventag: error: --compliance-standard: Compliance-standard enforcement is not implemented yet; the option has never changed InvenTag's behaviour. InvenTag does not assess compliance. To check your tags against a built-in baseline, run: inventag --template security-baseline --create-excel
(exit code 2)
inventag --disable-production-safety
inventag: error: --disable-production-safety: The read-only guard cannot be disabled from the CLI: InvenTag only needs read access, so non-read AWS calls are always blocked. Remove this option; nothing else is needed. To see which calls are allowed, run: inventag --validate-operations ec2:DescribeInstances
(exit code 2)
S3 上传
| 参数 | 默认值 | 状态 | 功能 |
|---|---|---|---|
--s3-bucket BUCKET | 无 | 可用 | 把本次运行写出的每个文件(Excel、Word 和标签合规 JSON)上传到该存储桶。上传失败时,运行以退出码 1 结束。需要 s3:PutObject。 |
--s3-key-prefix PREFIX | bom-reports/ | 可用 | 存储桶中的文件夹(键前缀)。文件位于 <prefix><UTC timestamp>/ 之下。 |
--s3-encryption AES256|aws:kms | AES256 | 可用 | 上传文件使用的服务器端加密。 |
--s3-kms-key-id KEY | 无 | 可用 | 用于 aws:kms 的 KMS 密钥。不给出时,S3 使用 AWS 托管密钥 aws/s3。需要 --s3-encryption aws:kms,否则退出码 2。 |
未针对真实的 AWS 验证:上传只需要 s3:PutObject(使用 aws:kms 时还需要 kms:GenerateDataKey)。
未针对 AWS 验证:aws:kms 没有密钥 ID 时,S3 是否使用 AWS 托管密钥 aws/s3。
退出码
| 代码 | 含义 |
|---|---|
| 0 | 成功。部分账户失败但其他账户正常,以及没有找到任何资源时,也是 0,除非你传入 --fail-on-account-error 或 --fail-if-empty。见功能状态。 |
| 1 | 出错:参数、配置或模板无效,数值超出范围(例如 --max-concurrent-accounts 0),凭证失败,所有账户都失败(无法扫描或超出时限),S3 上传失败,或某种报告格式无法写出 |
| 2 | 命令行无效:未知、格式错误或相互冲突的选项,超出 1 到 256 的 --concurrency 值,尚未实现的参数,Google 选项有误,版式文件参数缺少它所需的报告,或给了角色名称但没有 --accounts-file |
| 3 | 报告已写出,但带有 fail_on_tag_violations: true 的配置模板发现了标签策略违规 |
| 4 | 报告已写出,但 --risk-threshold 发现有安全组规则达到或超过该级别 |
| 5 | 报告已写出,但有账户无法扫描或超出时限,并且你传入了 --fail-on-account-error |
| 6 | 没有可报告的资源,并且你传入了 --fail-if-empty |