Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 754
Roopendra Avatar asked Apr 24 '14 08:04

Roopendra


People also ask

How do you define a dynamic variable?

A dynamic variable is a variable you can declare for a StreamBase module that can thereafter be referenced by name in expressions in operators and adapters in that module. In the declaration, you can link each dynamic variable to an input or output stream in the containing module.

Can you dynamically create variables?

Use an Array of VariablesThe simplest JavaScript method to create the dynamic variables is to create an array. In JavaScript, we can define the dynamic array without defining its length and use it as Map. We can map the value with the key using an array and also access the value using a key.


2 Answers

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

like image 96
DomaNitro Avatar answered Sep 20 '22 10:09

DomaNitro


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' }}" 
like image 28
AnshBikram Avatar answered Sep 20 '22 10:09

AnshBikram