4

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?

2 Answers 2

17

Try this :

  client_dc: "{% if 'jira-app' in hostname %}jira{% elif 'jira-next' in hostname %}jira-next{% else %}{{ hostenv }}{% endif %}"
Sign up to request clarification or add additional context in comments.

2 Comments

You can also add newlines to improve readability if you like.
@JohnMcGehee How? When i use Block Scalar Chomp (>-) i get an additional trailing whitespace (cause \n to space conversion).
6

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.