Is it possible to use jinja withing Ansible playbook yml file

Hi

Is it possible to write jinja “if” conditionals within a playbook like so? If not can someone suggest an alternative?

`

  • name: Deploy the VMs without an anti-affinity group.
    cs_instance:
    zone: “{{ cs_zone }}”
    api_region: “{{ cs_api_region | upper }}”
    display_name: “{{ item.instance }}”
    name: “{{ item.instance | regex_replace(‘\.’,‘-’) }}”
    project: “{{ cs_network }}”
    networks: [ “{{ cs_network }}” ]
    template: “{{ cs_template }}”
    {% if item.cs_instance is null %}
    service_offering: “{{ cs_template }}”
    {% else %}
    service_offering: “{{ item.cs_instance }}”
    {% endif %}
    state: "{{ cs_state }}
    poll_async: “yes”
    tags: “{{ final_tags }}”
    user_data: “{{ user_data | string }}”
    delegate_to: localhost
    loop: “{{ myvmsfqdn }}”
    when: cs_anti_affinity_group is not defined and cs_state == “started”

`

Thanks

Not in that manner, you need to use a jinja within service_offering instead:

service_offering: ‘{{ item.cs_instance|default(cs_template, true) }}’

That will use cs_template if cs_instance is undefined or falsy