Ansible, set_fact using if then else statement

I am trying to set a variable in Ansible with set_fact at runtime based on another variable. If the first value is used, regardless of the actual value. Here is my sample code:

- name: Global_vars - get date info
    set_fact:  
      jm_env: "{{lookup('env', 'Environment')}}"
      l_env: "{% if '{{jm_env}}==Develop' %}d{% elif '{{jm_env}}==Staging'%}s{% else %}p{% endif %}"

l_env dno matter what is installed jm_env.

+4
source share
1 answer

Firstly, the dictionaries in YAML are not ordered (and the syntax used by Ansible here is the YAML dictionary), so you have no guarantee that Ansible installed jm_envit before moving on to l_env- you need to divide the assignment into two tasks.

-, - '{{jm_env}}==Develop' - , ; if 'string' true ( , d ).

:

- name: Set the jm_env
    set_fact:  
      jm_env: "{{lookup('env', 'Environment')}}"

- name: Set the l_env
    set_fact:  
      l_env: "{% if jm_env=='Develop' %}d{% elif jm_env=='Staging'%}s{% else %}p{% endif %}"
+5

Source: https://habr.com/ru/post/1672859/


All Articles