跳到主要内容
版本:v1.4.1(当前)

单份报告的版式文件

本页介绍 --word-template 和 --excel-template 参数。每个参数接收一个版式文件, 对应一种报告格式。关于用 --template 选择的命名模板,请参阅模板。

报告模板是一个 YAML 或 JSON 文件。模板控制 Word 报告或 Excel 报告的结构。 用 --word-template 或 --excel-template 把文件交给 CLI。

如果不提供模板,InvenTag 使用内置的默认模板。此时报告的结构与没有模板功能的版本相同。

模板可以修改什么​

项目Word 报告Excel 报告
标题和副标题文档的标题部分工作簿属性,以及摘要表上方的可选行
公司名称{{ company_name }} 的值{{ company_name }} 的值,以及工作簿的创建者属性
徽标标题上方的 PNG 或 JPEG 文件不可用
章节或工作表显示哪些章节及其顺序显示哪些工作表及其顺序
列显示哪些列,以及它们的顺序、标题和宽度显示哪些列,以及它们的顺序、标题和宽度
服务资源表中出现哪些服务及其顺序哪些服务有工作表及其顺序
页眉和页脚页眉、页脚、页码每个工作表的打印页眉和打印页脚
表头行颜色不可用表头行的填充颜色
自由文本带变量的标题和段落不可用

制作模板​

  1. 把默认模板打印到文件。

    Shell
    inventag --print-default-template word > my_word_template.yaml
    inventag --print-default-template excel > my_excel_template.json
  2. 编辑文件。参见键参考。

  3. 验证文件。此命令不会发出任何 AWS 调用。

    Shell
    inventag --validate-templates \
    --word-template my_word_template.yaml \
    --excel-template my_excel_template.json

    如果文件有效,CLI 会为每个文件打印一行 OK,并以退出码 0 结束。

  4. 在运行中使用该文件。

    Shell
    inventag --create-word --create-excel \
    --word-template my_word_template.yaml \
    --excel-template my_excel_template.json

CLI 接受 .yaml、.yml 和 .json 这几种文件扩展名,两种格式均适用。默认模板文件也在仓库中: templates/default_word_template.yaml 和 templates/default_excel_template.json。

模板参数​

参数功能
--word-template PATHWord 报告的模板。此参数需要 --create-word。
--excel-template PATHExcel 报告的模板。此标志需要 --create-excel。没有它,CLI 会以退出码 2 停止。
--print-default-template word 或 --print-default-template excel打印默认模板并以退出码 0 结束。
--validate-templates验证 --word-template 和 --excel-template 指定的文件,然后结束。不发出 AWS 调用。

最小示例​

这个 Word 模板包含一个标题、每个服务一张资源表,以及一张摘要表。

my_word_template.yaml
template_version: "2.0"
format_type: word
document:
title: "{{ company_name }} Cloud Inventory"
subtitle: "Accounts: {{ account_ids }}"
branding:
company_name: Example Corp
header_footer:
header_text: "{{ company_name }} - Internal"
footer_text: "Generated {{ generated_date }}"
include_page_numbers: true
sections:
- id: title
content_type: title
- id: inventory
content_type: resources
title: Resources
service_title: "{{ service }} ({{ service_resource_count }})"
columns:
- field: name
heading: Resource name
width: 2.5
- field: region
- field: tags.Owner
heading: Owner
- id: summary
content_type: service_summary
title: "Summary of {{ total_resources }} resources"

这个 Excel 模板包含一个列出所有资源的工作表,然后是摘要工作表。

my_excel_template.json
{
"template_version": "2.0",
"format_type": "excel",
"document": {"title": "{{ company_name }} inventory"},
"branding": {"company_name": "Example Corp", "header_color": "1F4E78"},
"sections": [
{
"id": "all_resources",
"content_type": "resources",
"group_by_service": false,
"sheet_name": "All resources",
"columns": [
{"field": "service", "heading": "Service", "width": 14},
{"field": "name", "heading": "Name"},
{"field": "region", "heading": "Region"},
{"field": "tags.Owner", "heading": "Owner"}
]
},
{
"id": "summary",
"content_type": "service_summary",
"sheet_name": "Summary",
"show_title": true
}
]
}

变量​

在文本值中把变量写成 {{ name }}。InvenTag 在写入报告时用变量的值替换它。

变量值
{{ company_name }}branding.company_name 的值。
{{ document_title }}替换之后的 document.title 的值。
{{ document_subtitle }}替换之后的 document.subtitle 的值。
{{ generated_at }}UTC 生成时间,格式为 YYYY-MM-DD HH:MM:SS。
{{ generated_date }}UTC 生成日期,格式为 YYYY-MM-DD。
{{ account_ids }}报告中的账户 ID,已排序,以逗号加空格分隔。
{{ account_count }}报告中不同账户 ID 的数量。
{{ total_resources }}报告中的资源数量。
{{ service_count }}报告中不同服务的数量。
{{ region_count }}报告中不同区域的数量。

下面这些变量只在 Word resources 章节的 service_title 和 truncation_note 中有效:

变量值
{{ service }}当前表格的服务名称。
{{ service_resource_count }}当前服务的资源数量。
{{ shown_resource_count }}当前表格中的行数。

你可以在 variables 下定义自己的变量:

YAML
variables:
classification: Internal
header_footer:
footer_text: "Classification: {{ classification }}"

变量规则:

  • 变量名只能包含字母、数字和下划线。
  • 自定义变量的名称以小写字母开头,只能包含小写字母、数字和下划线。
  • 自定义变量不能使用内置变量的名称。
  • 变量的值是纯文本。值中不能包含另一个变量。
  • document.title 和 document.subtitle 不能使用 {{ document_title }} 或 {{ document_subtitle }}。
  • 以下键接受变量:document.title、document.subtitle、 header_footer.header_text、header_footer.footer_text,以及章节键 title、paragraphs、service_title 和 truncation_note。
  • 列标题、工作表名称和 branding.company_name 不接受变量。
  • InvenTag 不支持表达式、过滤器、条件、循环或文件包含。例如,{{ company_name | upper }} 是错误。
  • InvenTag 不会替换资源数据中的变量。名称中含有文本 {{ company_name }} 的资源, 在报告中保持原样。

键参考​

tests/unit/test_template_system.py 中的一个测试会确保本参考包含 inventag/reporting/report_template.py 中模式的每一个键。

顶层键​

键类型格式默认值说明
template_version字符串Word 和 Excel必填模式版本。必须是 "2.0"。
format_type字符串Word 和 Excel必填word 或 excel。
name字符串Word 和 Excel空模板的名称。不会显示在报告中。
description字符串Word 和 Excel空说明。不会显示在报告中。
document映射Word 和 Excel见下文标题和副标题。
branding映射Word 和 Excel见下文公司名称、徽标和颜色。
variables映射Word 和 Excel空你自己的变量:名称对应文本。
extra_fields列表Word 和 Excel空columns 可以使用的更多资源字段名。例如,来自你的标签映射的列。
header_footer映射Word 和 Excel见下文页眉和页脚文本。
sections列表Word 和 Excel必填章节(Word)或工作表(Excel),按报告顺序排列。

document​

键类型格式默认值说明
title字符串Word 和 Excel空Word:title 章节的文本。Excel:工作簿的标题属性。
subtitle字符串Word 和 Excel空Word:标题下方的一行。Excel:工作簿的主题属性。

branding​

键类型格式默认值说明
company_name字符串Word 和 Excel空{{ company_name }} 的值。Excel:同时也是工作簿的创建者属性。
logo_path字符串Word无显示在标题上方的 PNG 或 JPEG 文件。路径相对于模板文件,且必须位于其目录之内。
logo_width_inches数字Word1.5徽标宽度,0.25 到 7。
header_color字符串Excel366092表头行的填充颜色,六位十六进制数字。
键类型格式默认值说明
header_text字符串Word 和 Excel空Word:页眉。Excel:每个工作表的打印页眉。最多 255 个字符。
footer_text字符串Word 和 Excel空Word:页脚。Excel:每个工作表的打印页脚。最多 255 个字符。
include_page_numbers布尔值Wordfalse在页脚中添加 Page <number>。

Excel 的打印页眉和打印页脚只会在页面布局视图和纸张上显示。

未针对 Microsoft Excel 验证:打印页眉和页脚只会在页面布局视图和纸张上显示。

sections​

sections 的每个条目都是一个映射。content_type 键决定该章节显示什么。

content_type格式功能
titleWord文档标题、副标题和徽标。
textWord一个可选的标题和一组段落。
service_summaryWord 和 Excel每个服务一行:名称、资源数量、百分比。在 Word 中是一张表,在 Excel 中是一个工作表。
resourcesWord 和 Excel资源表。Word:每个服务一张表。Excel:每个服务一个工作表,或所有资源共用一个工作表。
network_analysisExcel网络分析工作表。只有存在网络分析数据时,InvenTag 才会写入它。
security_analysisExcel安全分析工作表。只有存在安全分析数据时,InvenTag 才会写入它。
cost_analysisExcel成本分析工作表。只有存在成本分析数据时,InvenTag 才会写入它(--enable-cost-analysis)。

章节键:

键类型格式章节类型默认值说明
id字符串Word 和 Excel全部必填章节的唯一名称:小写字母、数字和下划线。
content_type字符串Word 和 Excel全部必填章节显示什么。
enabled布尔值Word 和 Excel全部true设为 false 可省略该章节。
title字符串Wordtext、service_summary、resources空章节的标题文本。
level整数Wordtext、service_summary、resources1标题级别,1 到 3。服务标题使用下一级。
page_break_before布尔值Word全部false让章节从新的一页开始。
paragraphs列表Wordtext空列表text 章节的段落。
columnsauto 或列表Word 和 Excelservice_summary、resourcesautoauto,或列的列表。参见列。
table_style字符串Wordservice_summary、resourcesservice_summary 为 Light Grid Accent 1,resources 为 Light List Accent 1Word 表格样式。
services列表Word 和 Excelresources所有服务只显示这些服务,并按此顺序。比较时忽略大小写、空格以及前缀 Amazon 和 AWS。
service_title字符串Wordresources{{ service }} Resources ({{ service_resource_count }})每个服务表的标题。
max_rows整数Wordresources50每个服务表的最大行数,1 到 100000。
max_columns整数Wordresources6当 columns 为 auto 时的最大列数,1 到 100。
truncation_note字符串WordresourcesNote: Showing first {{ shown_resource_count }} of {{ service_resource_count }} {{ service }} resources.未显示全部资源的表格下方的段落。InvenTag 会再加一个段落,说明有多少资源未列出,以及 Excel 报告列出了全部资源。
page_break_after_service布尔值Wordresourcestrue在每个服务表之后另起一页。
sheet_name字符串Excelservice_summary、resourcesservice_summary 为 Summary工作表名称,1 到 31 个字符。对于 group_by_service: false 的 resources 章节,此项必填。
group_by_service布尔值Excelresourcestruetrue:每个服务一个工作表。false:所有资源共用一个工作表。
show_title布尔值Excelservice_summaryfalse在表格上方写入文档标题和副标题。

注意:

  • Word 文档按列表的顺序显示章节。
  • 工作簿按列表的顺序显示工作表。
  • 使用 group_by_service: true 时,每个工作表以其服务的名称命名。如果 Excel 不接受某个名称,InvenTag 会修改它:Excel 不允许的字符变为 -,超过 31 个字符的名称会被截断,History 变为 History (2)。日志会列出每个被修改的工作表。
  • Word 的 resources 章节按名称对服务排序,除非你给出 services。
  • table_style 的有效值是默认 Word 文档中的表格样式。例如 Table Grid、Light List Accent 1、 Light Grid Accent 1 和 Medium Shading 1 Accent 1。

列​

columns 键是 auto 或一个列表。列表的每个条目是一个字段名,或者是带有以下键的映射:

键类型格式默认值说明
field字符串Word 和 Excel必填要显示的字段。
heading字符串Word 和 Excel见下文列标题。
width数字Word 和 Excel自动列宽。Word:英寸,0.2 到 10。Excel:字符数,1 到 255。

默认标题:

  • Word:把字段名转换为带空格和大写字母的形式。例如, source_account_id 变为 Source Account Id。
  • Excel:字段名。

auto 的功能:

  • Word resources 章节:每个服务最多 max_columns 个字段。如果服务有这些字段, 最先出现的是 name、id、type、region、 compliance_status 和 tags。
  • Excel resources 章节:该工作表上资源的所有字段。
  • service_summary 章节:service、resource_count 和 percentage。

service_summary 章节的字段:service、resource_count、 percentage。

resources 章节的字段:

分组字段
标识service, type, id, name, arn
账户和位置account_id, source_account_id, source_account_name, account_name, region, availability_zone
资源详情status, state, creation_date, created_time, launch_time, size, instance_type, storage_type, engine, engine_version, allocated_storage, storage_encrypted, multi_az, publicly_accessible, vpc_id, vpc_name, subnet_id, subnet_name, security_groups, security_group_ids
网络vpc_cidr_block, vpc_total_ips, vpc_utilization_percentage, subnet_cidr_block, subnet_total_ips, subnet_utilization_percentage, subnet_is_public, subnet_availability_zone
安全security_risk_level, security_group_count, max_security_risk, inbound_rules_count, outbound_rules_count, associated_resources_count, has_permissive_rules, permissive_rules
成本estimated_monthly_cost, cost_breakdown, pricing_model, confidence_level, cost_optimization_opportunities, monthly_spend, service_monthly_spend, billing_validated
发现与合规compliance_status, compliance_score, discovery_timestamp, consolidation_timestamp, total_accounts_in_consolidation, discovered_at, discovered_via, priority, tagged, tags, service_description, resource_id, resource_name, resource_type
单个标签tags.<key>,例如 tags.Owner
单个原始 API 属性raw_data.<key>,例如 raw_data.InstanceType
你自己的字段extra_fields 中的每个名称。来源字段 source、sources 和 detail_level 不在上面的列表中;把它们加到 extra_fields 才能用作列。

如果资源没有某个字段,单元格为空。tags 字段会在一个单元格中显示资源的所有标签。

验证错误​

CLI 在第一次 AWS 调用之前验证每个模板。如果模板无效,CLI 会向标准错误输出为每个问题打印一行, 并以退出码 1 结束。每一行的格式如下:

Text
inventag: error: <file>: <key path>: <reason>

示例:

Text
inventag: error: my_word_template.yaml: colour: unknown key. Allowed keys: template_version, format_type, name, description, document, branding, variables, extra_fields, header_footer, sections
inventag: error: my_word_template.yaml: sections[1].columns[2].field: unknown column 'owner_name'. Use a known field (for example service, type, id, name, region, account_id, tags), a tags.<key> field, or add the name to extra_fields
问题原因的开头
模式中没有的键unknown key. Allowed keys: ...
属于另一种报告格式的键this key is not valid in a Word template 或 this key is not valid in an Excel template
值的类型错误must be a string, but the value is integer
缺少必填键missing key
不存在的章节类型unknown section type 'cover_page' for a Word template. Allowed values: ...
不存在的列unknown column 'owner_name'
不存在的变量unknown variable 'owner'. Allowed variables: ...
表达式、过滤器或嵌套的大括号malformed placeholder
两个章节使用相同的 idduplicate id 'summary'
模板的格式错误the file is a template for Excel reports, but this flag needs a template for Word reports
带有 template_version: "1.0" 的旧模板unsupported version '1.0'
超出模板目录的徽标路径the path must stay in the directory of the template file
不是 YAML 或 JSON 的文件YAML syntax error at line ... 或 JSON syntax error at line ...
同一个映射中重复写了一个键duplicate key 'name'
扩展名不同的文件unsupported file type
过大的文件The maximum is 262144 bytes

模板参数的退出码:

退出码条件
0--validate-templates 未发现问题,或 --print-default-template 打印了模板。
1模板文件无效。
2用法错误。例如,使用 --word-template 而没有 --create-word,或使用 --validate-templates 而没有模板参数。

限制​

项目限制
模板文件大小262144 字节(256 KiB)
文本值2000 个字符
页眉文本和页脚文本255 个字符
章节50
一个章节中的列100
一个 text 章节中的段落100
你自己的变量50
extra_fields 中的条目200
徽标文件大小5242880 字节(5 MiB)
徽标文件类型.png, .jpg, .jpeg
Excel 工作表名称31 个字符,不能包含 [ ] : * ? / \

其他限制:

  • 模板不能更改 Word 报告的字体、标题颜色或页面大小。
  • 模板不能更改网络分析工作表和安全分析工作表的列。
  • 模板不能在 Excel 报告中添加目录、图表或徽标。
  • Word 表格样式必须是默认 Word 文档中的样式。
  • scripts/legacy/ 中的旧脚本没有模板参数。
  • 旧配置示例中的配置键 templates、document_templates 和 default_template 没有任何作用。只有 --word-template 和 --excel-template 会选择模板。包含其中任何一个键的版式文件会被拒绝,并报 "unknown key" 错误。
  • 带有 template_version: "1.0" 的模板文件无效。v1.0.0 之前的 alpha 构建都附带了这类文件, 但 CLI 并不会读取它们。

模板文件的安全性​

  • InvenTag 用 yaml.safe_load 读取 YAML。YAML 文件无法运行代码。
  • InvenTag 不会计算表达式。替换只会替换变量表中的名称。
  • InvenTag 不会替换来自 AWS 的资源数据中的变量。
  • 模板不能包含另一个文件。唯一的文件引用是 branding.logo_path, 而且该文件必须位于模板目录中。
  • 在写出报告之前,InvenTag 会把常含机密的字段的值(例如 Lambda 环境变量、EC2 用户数据和 CloudFormation 参数值)替换为 ***REDACTED***。自由文本字段中的机密不会被识别。
  • Excel 构建器把每个值都存为文本。以 = 开头的值不会作为公式运行。