-
Notifications
You must be signed in to change notification settings - Fork 667
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
var-naming warns on variables with dynamically set names #1680
Comments
Can you please provide a valid use case for this scenario? |
Let's say you want to enable some features in a template configuration file based on whether plugins/dependencies are installed or not. You could do this by using dynamic variable names, by creating tasks like: - name: "Check which plugin dependencies are installed"
vars:
dependencies:
- "ansible"
- "docker"
block:
- name: "Check if plugin dependencies are installed"
command: "which {{ item }}"
register: "which_output"
loop: "{{ dependencies }}"
- name: "Set '_installed' boolean values"
set_fact:
"{{ item.item }}_installed": "{{ item.rc == 0 }}"
loop: "{{ which_output.results }}" You can then use these variables in a template: # global configuration
option1=value
option2=false
{% if ansible_installed %}
# Enable ansible specific configuration
ansible_option=value
{% endif %} |
I have used this in a role that combines a few common tasks, but needs to "return" some value (like a
---
rolename_register: rolename_status
---
# do some tasks
- name: Set return values
set_fact:
'{{ rolename_register }}':
thing1: value1
thing2: value2 This allows for a default but lets you override the variable with the returned status on role invocation by setting |
PR welcome here, please add a comment if you start working on it. |
Summary
var-naming
warns when a variable with a dynamic name is set usingset_fact
. For example, a variable with the nametest_var
can be set using{{ 'test_' }}var
. Althoughtest_var
follows the naming conventions,var-naming
still warns about{{ 'test_' }}var
.This is most likely because the string
{{ 'test_' }}var
does not pass the regex used byvar-naming
, even though the resulting variable name (i.e.test_var
) does.Issue Type
Ansible and Ansible Lint details
OS / ENVIRONMENT
Arch Linux 5.13.6-arch1-1
STEPS TO REPRODUCE
Desired Behaviour
var-naming
does not warn about valid variable names.Actual Behaviour
The text was updated successfully, but these errors were encountered: