EnigmaticNeo
EnigmaticNeo

Reputation: 45

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

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?

Upvotes: 4

Views: 42913

Answers (2)

JRomio
JRomio

Reputation: 2545

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' ) }}"

Upvotes: 6

chenchuk
chenchuk

Reputation: 5742

Try this :

  client_dc: "{% if 'jira-app' in hostname %}jira{% elif 'jira-next' in hostname %}jira-next{% else %}{{ hostenv }}{% endif %}"

Upvotes: 16

Related Questions