Ansible 模板变量类型评估

发布于 2025-01-09 12:56:04 字数 1565 浏览 0 评论 0原文

只是想问,我如何可以轻松地评估 ansible(jinja 模板)中任何变量的类型。 具体情况是,我的内存中的变量如下所示:(

"DEFAULT_BACKUPPATH": "/backup",
"DEFAULT_BACKUPTYPE": "rsync",
"DEFAULT_ZIP_BACKUP": "0",
"DEFAULT_DD_BACKUP_SAVE_USED_PARTITIONS_ONLY": 0,
"DEFAULT_KEEPBACKUPS": "3",
"DEFAULT_KEEPBACKUPS_DD": 0,
"DEFAULT_KEEPBACKUPS_DDZ": 0,
"DEFAULT_KEEPBACKUPS_TAR": 0,

如您所见,有字符串和数字) 现在我想在模板中使用它们。在模板中,我需要在字符串周围加引号,而在数字周围不加引号,因为这相当常见。

源代码是一个 yaml,看起来像这样(我将其包含在 include_vars 中)

# path to store the backupfile
DEFAULT_BACKUPPATH: "/backup"

# type of backup: dd, tar or rsync
DEFAULT_BACKUPTYPE: "rsync"

# zip tar or dd backup
DEFAULT_ZIP_BACKUP: "0"

# dd backup will save space used by partitions only
DEFAULT_DD_BACKUP_SAVE_USED_PARTITIONS_ONLY: 0

# how many backups to keep
DEFAULT_KEEPBACKUPS: "3"

ansible 的模板模块会剥离所有内容并仅显示值而不考虑类型:

DEFAULT_BACKUPPATH=/backup
DEFAULT_BACKUPTYPE=rsync
DEFAULT_ZIP_BACKUP=0
DEFAULT_DD_BACKUP_SAVE_USED_PARTITIONS_ONLY=0
DEFAULT_KEEPBACKUPS=3
DEFAULT_KEEPBACKUPS_DD=0
DEFAULT_KEEPBACKUPS_DDZ=0

模板相当简单:

DEFAULT_BACKUPPATH={{ DEFAULT_BACKUPPATH }}
DEFAULT_BACKUPTYPE={{ DEFAULT_BACKUPTYPE }}
DEFAULT_ZIP_BACKUP={{ DEFAULT_ZIP_BACKUP }}
DEFAULT_DD_BACKUP_SAVE_USED_PARTITIONS_ONLY={{ DEFAULT_DD_BACKUP_SAVE_USED_PARTITIONS_ONLY }}
DEFAULT_KEEPBACKUPS={{ DEFAULT_KEEPBACKUPS }}
DEFAULT_KEEPBACKUPS_DD={{ DEFAULT_KEEPBACKUPS_DD }}
DEFAULT_KEEPBACKUPS_DDZ={{ DEFAULT_KEEPBACKUPS_DDZ }}

我不想为每个变量指定在模板中应该有哪种类型。心绞痛可以从源头得到这个,但是如何呢?

非常感谢和问候

just wanted to ask, howo I can evaluate the type of any variable in ansible (jinja templates) easily.
The specific case is, that I have vars in memory that look like this:

"DEFAULT_BACKUPPATH": "/backup",
"DEFAULT_BACKUPTYPE": "rsync",
"DEFAULT_ZIP_BACKUP": "0",
"DEFAULT_DD_BACKUP_SAVE_USED_PARTITIONS_ONLY": 0,
"DEFAULT_KEEPBACKUPS": "3",
"DEFAULT_KEEPBACKUPS_DD": 0,
"DEFAULT_KEEPBACKUPS_DDZ": 0,
"DEFAULT_KEEPBACKUPS_TAR": 0,

(as you can see there are STRINGS and NUMBERS)
and now I want to use them in a template. In the template I need to have quotes around strings and nothing around numbers, as it´s fairly common.

the source is a yaml that looks like this (and I include it with include_vars)

# path to store the backupfile
DEFAULT_BACKUPPATH: "/backup"

# type of backup: dd, tar or rsync
DEFAULT_BACKUPTYPE: "rsync"

# zip tar or dd backup
DEFAULT_ZIP_BACKUP: "0"

# dd backup will save space used by partitions only
DEFAULT_DD_BACKUP_SAVE_USED_PARTITIONS_ONLY: 0

# how many backups to keep
DEFAULT_KEEPBACKUPS: "3"

the template module of ansible strips all and just displaying the value without regarding the type:

DEFAULT_BACKUPPATH=/backup
DEFAULT_BACKUPTYPE=rsync
DEFAULT_ZIP_BACKUP=0
DEFAULT_DD_BACKUP_SAVE_USED_PARTITIONS_ONLY=0
DEFAULT_KEEPBACKUPS=3
DEFAULT_KEEPBACKUPS_DD=0
DEFAULT_KEEPBACKUPS_DDZ=0

Template is fairly straight forward:

DEFAULT_BACKUPPATH={{ DEFAULT_BACKUPPATH }}
DEFAULT_BACKUPTYPE={{ DEFAULT_BACKUPTYPE }}
DEFAULT_ZIP_BACKUP={{ DEFAULT_ZIP_BACKUP }}
DEFAULT_DD_BACKUP_SAVE_USED_PARTITIONS_ONLY={{ DEFAULT_DD_BACKUP_SAVE_USED_PARTITIONS_ONLY }}
DEFAULT_KEEPBACKUPS={{ DEFAULT_KEEPBACKUPS }}
DEFAULT_KEEPBACKUPS_DD={{ DEFAULT_KEEPBACKUPS_DD }}
DEFAULT_KEEPBACKUPS_DDZ={{ DEFAULT_KEEPBACKUPS_DDZ }}

I don´t want to specify for every variable in template which type is should have. The angine could get this from the source, but how ?

Many thanks in advance and Greetings

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

楠木可依 2025-01-16 12:56:04

使用此模板:

DEFAULT_BACKUPPATH={{ DEFAULT_BACKUPPATH  if  DEFAULT_BACKUPPATH | regex_search('^[0-9]+

您可以使用 jinja2 宏简化编码:

{% macro check(name, value='') -%}
{{ name }}={{ value  if  value | regex_search('^[0-9]+
) != None else '"' ~ DEFAULT_BACKUPPATH ~ '"'}}
DEFAULT_BACKUPTYPE={{ DEFAULT_BACKUPTYPE if DEFAULT_BACKUPTYPE | regex_search('^[0-9]+

您可以使用 jinja2 宏简化编码:


) != None else '"' ~ DEFAULT_BACKUPTYPE ~ '"'}}
DEFAULT_ZIP_BACKUP={{ DEFAULT_ZIP_BACKUP if DEFAULT_ZIP_BACKUP | regex_search('^[0-9]+

您可以使用 jinja2 宏简化编码:


) != None else '"' ~ DEFAULT_ZIP_BACKUP ~ '"'}}
DEFAULT_DD_BACKUP_SAVE_USED_PARTITIONS_ONLY={{ DEFAULT_DD_BACKUP_SAVE_USED_PARTITIONS_ONLY if DEFAULT_DD_BACKUP_SAVE_USED_PARTITIONS_ONLY | regex_search('^[0-9]+

您可以使用 jinja2 宏简化编码:


) != None else '"' ~ DEFAULT_DD_BACKUP_SAVE_USED_PARTITIONS_ONLY ~ '"'}}
DEFAULT_KEEPBACKUPS={{ DEFAULT_KEEPBACKUPS if DEFAULT_KEEPBACKUPS | regex_search('^[0-9]+

您可以使用 jinja2 宏简化编码:


) != None else '"' ~ DDEFAULT_KEEPBACKUPS ~ '"'}}
DEFAULT_KEEPBACKUPS_DD={{ DEFAULT_KEEPBACKUPS_DD if DEFAULT_KEEPBACKUPS_DD | regex_search('^[0-9]+

您可以使用 jinja2 宏简化编码:


) != None else '"' ~ DEFAULT_KEEPBACKUPS_DD ~ '"'}}
DEFAULT_KEEPBACKUPS_DDZ={{ DEFAULT_KEEPBACKUPS_DDZ if DEFAULT_KEEPBACKUPS_DDZ | regex_search('^[0-9]+

您可以使用 jinja2 宏简化编码:


) != None else '"' ~ DEFAULT_KEEPBACKUPS_DDZ ~ '"'}}

您可以使用 jinja2 宏简化编码:


) != None else '"' ~ value ~ '"'}}
{%- endmacro %}

{{ check('DEFAULT_BACKUPPATH', DEFAULT_BACKUPPATH) }}
{{ check('DEFAULT_KEEPBACKUPS', DEFAULT_KEEPBACKUPS) }}
{{ check('DEFAULT_KEEPBACKUPS_DD', DEFAULT_KEEPBACKUPS_DD) }}
{{ check('DEFAULT_KEEPBACKUPS_DDZ', DEFAULT_KEEPBACKUPS_DDZ) }}
) != None else '"' ~ DEFAULT_BACKUPPATH ~ '"'}} DEFAULT_BACKUPTYPE={{ DEFAULT_BACKUPTYPE if DEFAULT_BACKUPTYPE | regex_search('^[0-9]+

您可以使用 jinja2 宏简化编码:

) != None else '"' ~ DEFAULT_BACKUPTYPE ~ '"'}} DEFAULT_ZIP_BACKUP={{ DEFAULT_ZIP_BACKUP if DEFAULT_ZIP_BACKUP | regex_search('^[0-9]+

您可以使用 jinja2 宏简化编码:

) != None else '"' ~ DEFAULT_ZIP_BACKUP ~ '"'}} DEFAULT_DD_BACKUP_SAVE_USED_PARTITIONS_ONLY={{ DEFAULT_DD_BACKUP_SAVE_USED_PARTITIONS_ONLY if DEFAULT_DD_BACKUP_SAVE_USED_PARTITIONS_ONLY | regex_search('^[0-9]+

您可以使用 jinja2 宏简化编码:

) != None else '"' ~ DEFAULT_DD_BACKUP_SAVE_USED_PARTITIONS_ONLY ~ '"'}} DEFAULT_KEEPBACKUPS={{ DEFAULT_KEEPBACKUPS if DEFAULT_KEEPBACKUPS | regex_search('^[0-9]+

您可以使用 jinja2 宏简化编码:

) != None else '"' ~ DDEFAULT_KEEPBACKUPS ~ '"'}} DEFAULT_KEEPBACKUPS_DD={{ DEFAULT_KEEPBACKUPS_DD if DEFAULT_KEEPBACKUPS_DD | regex_search('^[0-9]+

您可以使用 jinja2 宏简化编码:

) != None else '"' ~ DEFAULT_KEEPBACKUPS_DD ~ '"'}} DEFAULT_KEEPBACKUPS_DDZ={{ DEFAULT_KEEPBACKUPS_DDZ if DEFAULT_KEEPBACKUPS_DDZ | regex_search('^[0-9]+

您可以使用 jinja2 宏简化编码:

) != None else '"' ~ DEFAULT_KEEPBACKUPS_DDZ ~ '"'}}

您可以使用 jinja2 宏简化编码:

use this template:

DEFAULT_BACKUPPATH={{ DEFAULT_BACKUPPATH  if  DEFAULT_BACKUPPATH | regex_search('^[0-9]+

you can simplify the coding with a jinja2 macro:

{% macro check(name, value='') -%}
{{ name }}={{ value  if  value | regex_search('^[0-9]+
) != None else '"' ~ DEFAULT_BACKUPPATH ~ '"'}}
DEFAULT_BACKUPTYPE={{ DEFAULT_BACKUPTYPE if DEFAULT_BACKUPTYPE | regex_search('^[0-9]+

you can simplify the coding with a jinja2 macro:


) != None else '"' ~ DEFAULT_BACKUPTYPE ~ '"'}}
DEFAULT_ZIP_BACKUP={{ DEFAULT_ZIP_BACKUP if DEFAULT_ZIP_BACKUP | regex_search('^[0-9]+

you can simplify the coding with a jinja2 macro:


) != None else '"' ~ DEFAULT_ZIP_BACKUP ~ '"'}}
DEFAULT_DD_BACKUP_SAVE_USED_PARTITIONS_ONLY={{ DEFAULT_DD_BACKUP_SAVE_USED_PARTITIONS_ONLY if DEFAULT_DD_BACKUP_SAVE_USED_PARTITIONS_ONLY | regex_search('^[0-9]+

you can simplify the coding with a jinja2 macro:


) != None else '"' ~ DEFAULT_DD_BACKUP_SAVE_USED_PARTITIONS_ONLY ~ '"'}}
DEFAULT_KEEPBACKUPS={{ DEFAULT_KEEPBACKUPS if DEFAULT_KEEPBACKUPS | regex_search('^[0-9]+

you can simplify the coding with a jinja2 macro:


) != None else '"' ~ DDEFAULT_KEEPBACKUPS ~ '"'}}
DEFAULT_KEEPBACKUPS_DD={{ DEFAULT_KEEPBACKUPS_DD if DEFAULT_KEEPBACKUPS_DD | regex_search('^[0-9]+

you can simplify the coding with a jinja2 macro:


) != None else '"' ~ DEFAULT_KEEPBACKUPS_DD ~ '"'}}
DEFAULT_KEEPBACKUPS_DDZ={{ DEFAULT_KEEPBACKUPS_DDZ if DEFAULT_KEEPBACKUPS_DDZ | regex_search('^[0-9]+

you can simplify the coding with a jinja2 macro:


) != None else '"' ~ DEFAULT_KEEPBACKUPS_DDZ ~ '"'}}

you can simplify the coding with a jinja2 macro:


) != None else '"' ~ value ~ '"'}}
{%- endmacro %}

{{ check('DEFAULT_BACKUPPATH', DEFAULT_BACKUPPATH) }}
{{ check('DEFAULT_KEEPBACKUPS', DEFAULT_KEEPBACKUPS) }}
{{ check('DEFAULT_KEEPBACKUPS_DD', DEFAULT_KEEPBACKUPS_DD) }}
{{ check('DEFAULT_KEEPBACKUPS_DDZ', DEFAULT_KEEPBACKUPS_DDZ) }}
) != None else '"' ~ DEFAULT_BACKUPPATH ~ '"'}} DEFAULT_BACKUPTYPE={{ DEFAULT_BACKUPTYPE if DEFAULT_BACKUPTYPE | regex_search('^[0-9]+

you can simplify the coding with a jinja2 macro:

) != None else '"' ~ DEFAULT_BACKUPTYPE ~ '"'}} DEFAULT_ZIP_BACKUP={{ DEFAULT_ZIP_BACKUP if DEFAULT_ZIP_BACKUP | regex_search('^[0-9]+

you can simplify the coding with a jinja2 macro:

) != None else '"' ~ DEFAULT_ZIP_BACKUP ~ '"'}} DEFAULT_DD_BACKUP_SAVE_USED_PARTITIONS_ONLY={{ DEFAULT_DD_BACKUP_SAVE_USED_PARTITIONS_ONLY if DEFAULT_DD_BACKUP_SAVE_USED_PARTITIONS_ONLY | regex_search('^[0-9]+

you can simplify the coding with a jinja2 macro:

) != None else '"' ~ DEFAULT_DD_BACKUP_SAVE_USED_PARTITIONS_ONLY ~ '"'}} DEFAULT_KEEPBACKUPS={{ DEFAULT_KEEPBACKUPS if DEFAULT_KEEPBACKUPS | regex_search('^[0-9]+

you can simplify the coding with a jinja2 macro:

) != None else '"' ~ DDEFAULT_KEEPBACKUPS ~ '"'}} DEFAULT_KEEPBACKUPS_DD={{ DEFAULT_KEEPBACKUPS_DD if DEFAULT_KEEPBACKUPS_DD | regex_search('^[0-9]+

you can simplify the coding with a jinja2 macro:

) != None else '"' ~ DEFAULT_KEEPBACKUPS_DD ~ '"'}} DEFAULT_KEEPBACKUPS_DDZ={{ DEFAULT_KEEPBACKUPS_DDZ if DEFAULT_KEEPBACKUPS_DDZ | regex_search('^[0-9]+

you can simplify the coding with a jinja2 macro:

) != None else '"' ~ DEFAULT_KEEPBACKUPS_DDZ ~ '"'}}

you can simplify the coding with a jinja2 macro:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文