Ansible, set_fact using if then else statement

前端 未结 2 1855
孤城傲影
孤城傲影 2021-02-08 16:19

I am trying to set a variable in Ansible with set_fact at runtime based upon another variable. If uses first value no matter what the actual value is. Here is my code example:

2条回答
  •  攒了一身酷
    2021-02-08 17:13

    Firstly, dictionaries in YAML are not ordered (and the syntax used by Ansible here is a YAML dictionary), so you have no guarantee Ansible would first set jm_env before proceeding to l_env -- you need to split the assignment into two tasks.

    Secondly, your test expressions are incorrect -- '{{jm_env}}==Develop' is a string because it is quoted; and testing if 'string' will always evaluate to true (this is the direct reason you always get d in the output).

    Use:

    - 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 %}"
    

提交回复
热议问题