Score:0

Ansible- Define variables based on conditions with if elif else

in flag

I have multiple roles as follows : haproxy, java, nginx, tomcat

├── ansible-test-host.yml
├── hapoxy
├── java
├── myplaybook.yml
├── nginx
└── tomcat

Base playbook is : myplaybook.yml and looks like below:

---
- hosts: test-local
  connection: local
  vars:
    java_version: "1.8.0"

  roles:
    - java

So my requirement is depending upon the roles specified java/tomcat/nginx etc I have a template file to populate. Say my logic is to do as follows:

if 
    role == java then in  template.j2  I have to set `JAVA_TOOL_OPTIONS= -Xmx10g -Xms5g` 
elif 
    role == tomcat then in  template.j2  I have to set `JAVA_OPTS= -Xmx10g -Xms5g` 
else    
    NOTHING 
endif

I have other underlying scenarios to sort but if I get the base of it rest I can do.

What I have tried in java role main.yml is here

---
# tasks file for java
- name: output the 'ansible_' magic variables
  debug:
    msg: [
      "ansible_role_names = {{ ansible_role_names | default({}) }}",
      "ansible_dependent_role_names =  {{ ansible_dependent_role_names }}",
      "ansible_play_role_names = {{ ansible_play_role_names | default([]) | join(',') }}"
    ]

- name: set facts
  set_fact:
     DEFAULT_VAR: >
                {% if "{{ ansible_play_role_names | default([]) | join(',') }}" == "java" %}
                JAVA_TOOL_OPTIONS
                {% elif "{{ ansible_play_role_names | default([]) | join(',') }}" == 'tomcat' %}
                CATALINA_OPTS
                {% else %}
                NOTHING
                {% endif %}

- name: output my custom var- try1
  debug:
    msg: >
        {% if "{{ ansible_play_role_names | default([]) | join(',') }}" == "java" %}
        JAVA_TOOL_OPTIONS
        {% else %}
        NOTHING
        {% endif %}


- name: output my custom var - echo 
  debug:
    msg: "{{ DEFAULT_VAR }}"

My output is as

TASK [java : output the 'ansible_' magic variables] ****************************************************
ok: [127.0.0.1] => {
    "msg": [
        "ansible_role_names = ['java']",
        "ansible_dependent_role_names =  []",
        "ansible_play_role_names = java"
    ]
}

TASK [java : set facts] ********************************************************************************
ok: [127.0.0.1]

TASK [java : output my custom var- try1] ***************************************************************
ok: [127.0.0.1] => {
    "msg": " NOTHING \n"
}

TASK [java : output my custom var - echo] **************************************************************
ok: [127.0.0.1] => {
    "msg": " NOTHING \n"
}

PLAY RECAP *********************************************************************************************
127.0.0.1                  : ok=5    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

Please help with pointers to achieve this.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.