'Ansible: Conditional "when" clause with empty variable
I am tasked with creating an Ansible role, where I set a variable within the defaults/main.yml file in the same role.
role_variable: ""
This variable is then recalled within another task, which uses it as conditional, in order to perform operations:
ROLE
-name: "Task which is called"
# Stuff which will be done given that the conditional below is correct
set_fact:
role_variable: "{{ variable_1 if 'string' not in issuer else variable_2 }} "
when: (role_variable | length == 0)
Within the playbook, this variable is not set anywhere else -it is only used for debug- and if I print the above condition within a debug clause, it correctly returns 0.
PLAYBOOK
-name: "Import secrets"
vars_files:
- secrets.yml
hosts: "{{ target }}"
gather_facts: false
tasks:
- include_vars:
file: "{{ credentials }}"
- ansible.builtin.setup:
gather_subset: min ### This is used in other parts of the complete playbook
delegate_to: localhost
- name: "Task to call"
import_role:
name: role_name
tasks_from: role_task_name
When this variable is not set a value within the playbook, it evaluates correctly, e.g. :
ansible-playbook playbook.yaml -e ""
But whenever this is evaluated even with an empty string (e.g.):
ansible-playbook playbook.yaml -e "role_variable="
This is necessary for example within Jenkins builds, where this variable can either be passed or not by the user. If not, it should use a default, scripted within the role or play.
I have tried additional methods of doing the same thing:
when: role_variable is none
when: role_variable is null
when: role_variable is none
when: role_variable is match("")
So far, I had no luck.
The objective is to pass the role_variable as default with an empty string "", and re-set it only if such variable is empty.
Does anybody have any past experiences with this, on how it is possible to make this work?
Thanks in advance for any help
Solution 1:[1]
I verified - also thanks to the guys who contributed - that what I am trying to achieve is in fact not possible.
According to Ansible documentation, setting the variable with the --extra-vars or -e flag takes precedence in setting the variable.
Even the single act of defining it, and leaving it as empty, just triggers the condition, and it is not possible to override it.
In my case, since I have to pass it to Jenkins via a .groovy file, I have set up a condition where I instruct when variable is empty, it doesn't pass the argument.
Solution 2:[2]
you have to do this test:
- name: "Task which is called"
debug: msg="ok"
when: role_variable is not defined or role_variable == ''
you'll see the task executed if role_variable is not defined or role_variable is empty
for your problem
- hosts: localhost
gather_facts: no
tasks:
- name: loop over
pause:
prompt: "role_variable is empty, give the content "
when: role_variable is not defined or role_variable == ''
register: out
- name: "Task which is called"
set_fact:
role_variable: "{{ out.user_input }}"
when: out.user_input is defined
- name: display
debug:
msg: "{{ role_variable }}"
but it seems the result is empty...when launching ansible-playbook playbook.yaml -e "role_variable=" i dunno if its bug
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Alessandro |
| Solution 2 |
