Home > Net >  Ansible Template variable type evaluation
Ansible Template variable type evaluation

Time:02-25

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

CodePudding user response:

use this template:

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 ~ '"'}}

you can simplify the coding with a jinja2 macro:

{% 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) }}
  • Related