Ansible 模板变量类型评估
Ansible Template variable type evaluation
只是想问一下,我如何可以轻松地评估 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,
(如您所见,有 STRINGS 和 NUMBERS)
现在我想在模板中使用它们。在模板中,我需要在字符串周围加上引号,而在数字周围没有任何内容,因为这很常见。
源代码是一个如下所示的 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 }}
我不想为模板中的每个变量指定应该具有的类型。 angine 可以从源头得到这个,但是如何?
非常感谢和问候
使用此模板:
DEFAULT_BACKUPPATH={{ DEFAULT_BACKUPPATH if DEFAULT_BACKUPPATH | regex_search('^[0-9]+$') != None else '"' ~ DEFAULT_BACKUPPATH ~ '"'}}
DEFAULT_BACKUPTYPE={{ DEFAULT_BACKUPTYPE if DEFAULT_BACKUPTYPE | regex_search('^[0-9]+$') != None else '"' ~ DEFAULT_BACKUPTYPE ~ '"'}}
DEFAULT_ZIP_BACKUP={{ DEFAULT_ZIP_BACKUP if DEFAULT_ZIP_BACKUP | regex_search('^[0-9]+$') != 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]+$') != None else '"' ~ DEFAULT_DD_BACKUP_SAVE_USED_PARTITIONS_ONLY ~ '"'}}
DEFAULT_KEEPBACKUPS={{ DEFAULT_KEEPBACKUPS if DEFAULT_KEEPBACKUPS | regex_search('^[0-9]+$') != None else '"' ~ DDEFAULT_KEEPBACKUPS ~ '"'}}
DEFAULT_KEEPBACKUPS_DD={{ DEFAULT_KEEPBACKUPS_DD if DEFAULT_KEEPBACKUPS_DD | regex_search('^[0-9]+$') != None else '"' ~ DEFAULT_KEEPBACKUPS_DD ~ '"'}}
DEFAULT_KEEPBACKUPS_DDZ={{ DEFAULT_KEEPBACKUPS_DDZ if DEFAULT_KEEPBACKUPS_DDZ | regex_search('^[0-9]+$') != None else '"' ~ DEFAULT_KEEPBACKUPS_DDZ ~ '"'}}
您可以使用 jinja2 宏简化编码:
{% macro check(name, value='') -%}
{{ name }}={{ value if value | regex_search('^[0-9]+$') != 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) }}
只是想问一下,我如何可以轻松地评估 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,
(如您所见,有 STRINGS 和 NUMBERS) 现在我想在模板中使用它们。在模板中,我需要在字符串周围加上引号,而在数字周围没有任何内容,因为这很常见。
源代码是一个如下所示的 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 }}
我不想为模板中的每个变量指定应该具有的类型。 angine 可以从源头得到这个,但是如何?
非常感谢和问候
使用此模板:
DEFAULT_BACKUPPATH={{ DEFAULT_BACKUPPATH if DEFAULT_BACKUPPATH | regex_search('^[0-9]+$') != None else '"' ~ DEFAULT_BACKUPPATH ~ '"'}}
DEFAULT_BACKUPTYPE={{ DEFAULT_BACKUPTYPE if DEFAULT_BACKUPTYPE | regex_search('^[0-9]+$') != None else '"' ~ DEFAULT_BACKUPTYPE ~ '"'}}
DEFAULT_ZIP_BACKUP={{ DEFAULT_ZIP_BACKUP if DEFAULT_ZIP_BACKUP | regex_search('^[0-9]+$') != 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]+$') != None else '"' ~ DEFAULT_DD_BACKUP_SAVE_USED_PARTITIONS_ONLY ~ '"'}}
DEFAULT_KEEPBACKUPS={{ DEFAULT_KEEPBACKUPS if DEFAULT_KEEPBACKUPS | regex_search('^[0-9]+$') != None else '"' ~ DDEFAULT_KEEPBACKUPS ~ '"'}}
DEFAULT_KEEPBACKUPS_DD={{ DEFAULT_KEEPBACKUPS_DD if DEFAULT_KEEPBACKUPS_DD | regex_search('^[0-9]+$') != None else '"' ~ DEFAULT_KEEPBACKUPS_DD ~ '"'}}
DEFAULT_KEEPBACKUPS_DDZ={{ DEFAULT_KEEPBACKUPS_DDZ if DEFAULT_KEEPBACKUPS_DDZ | regex_search('^[0-9]+$') != None else '"' ~ DEFAULT_KEEPBACKUPS_DDZ ~ '"'}}
您可以使用 jinja2 宏简化编码:
{% macro check(name, value='') -%}
{{ name }}={{ value if value | regex_search('^[0-9]+$') != 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) }}