[jinja2] How to write dynamic variable in Ansible playbook

Based on extra vars parameter I Need to write variable value in ansible playbook

ansible-playbook playbook.yml -e "param1=value1 param2=value2 param3=value3"

If only param1 passed

myvariable: 'param1'  

If only param1,param2 passed

myvariable: 'param1,param2' 

If param1,param2,param3 are passed then variable value will be

myvariable: 'param1,param2,param3' 

When I try to create variable dynamically through template then my playbook always takes previous variable value. But inside dest=roles/myrole/vars/main.yml its writing correct value.

What I make a try here

- hosts: local
  user: roop
  gather_facts: yes
  connection: local

  tasks: 

  - template: src=roles/myrole/templates/myvar.j2 dest=roles/myrole/vars/main.yml

  - debug: var=myvariable

  roles:
        - { role: myrole }

So inside myrole directory I have created template and vars

- roles
  - myrole
    - vars/main.yml
    - templates/myvar.j2 

templates/myvar.j2

{% if param1 is defined and param2 is defined and param3 is defined %}
  myvariable: 'param1,param2,param3'
{% elif param1 is defined and param2 is defined %}
  myvariable: 'param1,param2'
{% elif param1 is defined %}
  myvariable: 'param1'
{% else %}
  myvariable: 'default-param'
{% endif %} 

As I know if only two condition then I can do this using inline expression like below

{{ 'param1,param2' if param1 is defined and param2 is defined else 'default-param' }}

<do something> if <something is true> else <do something else>

Is it possible if - elif - else in inline expression like above. Or any other way to assign value dynamically in ansible playbook?

This question is related to jinja2 ansible ansible-playbook

The answer is


I am sure there is a smarter way for doing what you want but this should work:

- name         : Test var
  hosts        : all
  gather_facts : no
  vars:
    myvariable : false
  tasks:
    - name: param1
      set_fact:
        myvariable: "{{param1}}"
      when: param1 is defined

    - name: param2
      set_fact:
        myvariable: "{{ param2 if not myvariable else myvariable + ',' + param2 }}"
      when: param2 is defined

    - name: param3
      set_fact:
        myvariable: "{{ param3 if not myvariable else myvariable + ',' + param3 }}"
      when: param3 is defined

    - name: default
      set_fact:
        myvariable: "default"
      when: not myvariable

    - debug:
       var=myvariable

Hope that helps. I am not sure if you can construct variables dynamically and do this in an iterator. But you could also write a small python code or any other language and plug it into ansible


my_var: the variable declared

VAR: the variable, whose value is to be checked

param_1, param_2: values of the variable VAR

value_1, value_2, value_3: the values to be assigned to my_var according to the values of my_var

my_var: "{{ 'value_1' if VAR == 'param_1' else 'value_2' if VAR == 'param_2' else 'value_3' }}"

I would first suggest that you step back and look at organizing your plays to not require such complexity, but if you really really do, use the following:

   vars:
    myvariable: "{{[param1|default(''), param2|default(''), param3|default('')]|join(',')}}"

Examples related to jinja2

'if' statement in jinja2 template Ansible: filter a list by its attributes Split string into list in jinja? How to iterate through a list of dictionaries in Jinja template? How to write dynamic variable in Ansible playbook Jinja2 template not rendering if-elif-else statement properly Jinja2 template variable if None Object set a default value Convert integer to string Jinja Link to Flask static files with url_for How to pass a list from Python, by Jinja2 to JavaScript

Examples related to ansible

Specifying ssh key in ansible playbook file Ansible: how to get output to display How to do multiline shell script in Ansible Accessing inventory host variable in Ansible playbook Ansible: get current target host's IP address Ansible Ignore errors in tasks and fail at end of the playbook if any tasks had errors Ansible: How to delete files and folders inside a directory? How to pass a user / password in ansible command How to test that a registered variable is not empty? Copy multiple files with Ansible

Examples related to ansible-playbook

Ansible Ignore errors in tasks and fail at end of the playbook if any tasks had errors Ansible: create a user with sudo privileges Run an Ansible task only when the variable contains a specific string Ansible: Store command's stdout in new variable? Checking for multiple conditions using "when" on single task in ansible how to define ssh private key for servers fetched by dynamic inventory in files Override hosts variable of Ansible playbook from the command line Proper way to concatenate variable strings How can I pass variable to ansible playbook in the command line? How to run a task when variable is undefined in ansible?