Ansible 模板变量类型评估
只是想问,我如何可以轻松地评估 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用此模板:
) != 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:
) != 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: