less than 1 minute read

How to use Jinja2 inside a playbook

  1. To create variables
- set_fact:
    foo: |
      {% set aks_modified = dict() %}
      {% for ak in activation_keys %}
      {{ aks_modified.update({ak:[]}) }}
      {% endfor %}
      {{ aks_modified | to_json }}
  1. To loop in range
  - debug:
      msg: |
       {%- for i in range(0,10) -%}
          {{ i }}
       {%- endfor -%}
  1. To dynamically assign variables
  vars:
    foo: 'hello'
    bar: 'byebye'
    x: "{{ 'foo' if ansible_distribution_major_version == '7' else 'bar' }}"
  1. If-then-else
  vars:
    test: this is a test environment
    uat: this is a uat environment
    prod: this isa prod environment
  tasks:
  - set_facts:
      env_vars:  "{{ test if ( env == 'test' ) else uat if ( env == 'uat' ) else prod if ( env == 'prod' ) }}"
  1. Create a list from another list with additional info
  - name: Generate routes for DC1 PER01
    set_fact:
      new_routes: |
        {% set routes = [] %}
        {% for i in routes_to_add %}
        {% set _ = routes.append("router static vrf " + vrf|string + " address-family ipv4 unicast " + i + " " + bundle + "." + vlan|string + " " + firewall) %}
        {% endfor %}
        {{ routes }}

Leave a comment