Is it possible to use jinja withing Ansible playbook yml file

17 views
Skip to first unread message

Aidan Wong

unread,
May 20, 2020, 2:42:21 PM5/20/20
to Ansible Project
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

Matt Martz

unread,
May 20, 2020, 2:47:45 PM5/20/20
to ansible...@googlegroups.com
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

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/567771c1-88fb-4f1c-b26e-5c2a3ee4eb78%40googlegroups.com.


--
Matt Martz
@sivel
sivel.net
Reply all
Reply to author
Forward
0 new messages