Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use if elif and else jinja condition in same statement in ansible

Tags:

ansible

jinja2

Below is my part of my meta file in ansible.

---
dependencies:
- { role: nomad-agent,
      nomad_agent_type: client,
      client_dc: "{{ 'jira' if 'jira-app' in host_name else host_env }}",
      nomad_servers: ["{{ nomad1_ip }}","{{ nomad2_ip }}","{{ nomad3_ip }}"],
      tags: "nomad-agent"
  }

In client_dc option, I need to add elif in such a manner that client_dc would be assigned to "jira-next" if 'jira-next' is found in the hostname. So, if 'jira-app' is found in hostname, client_dc: jira elif 'jira-next-app' is found in hostname, client_dc: jira-next otherwise client_dc: host_name (variable pre assigned in variable file)

How can I achieve that?

like image 976
EnigmaticNeo Avatar asked Oct 14 '25 07:10

EnigmaticNeo


2 Answers

Try this :

  client_dc: "{% if 'jira-app' in hostname %}jira{% elif 'jira-next' in hostname %}jira-next{% else %}{{ hostenv }}{% endif %}"
like image 112
chenchuk Avatar answered Oct 16 '25 22:10

chenchuk


Another example of using else if in Ansible Jinja

- name: Set Service status
   set_fact:
     service_status: "{{ 'Not Installed' if status is not defined else ( 'Running' if status == 200 else 'Not Running' ) }}"
like image 34
JRomio Avatar answered Oct 16 '25 21:10

JRomio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!