Skip to content



ansible tips


ansible tips

combining variable and string to form another variable

# "service" variable is set and has "ldap"
# and you want to check the presence of ldap_backup_target variable...

# this gives you this warning
# [WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: {{ vars[service + '_backup_target'] }}
- name: variable requirement check
  ansible.builtin.assert:
    that:
      - "{{ service }}_backup_target is defined"

# use vars[] to concatenate variable content and string to form a separate variable name
- name: variable requirement check
    ansible.builtin.assert:
    that:
      - vars[service + '_backup_target'] is defined