During a recent code review of an ansible playbook, I noticed one dev using variables defined in playbook group_vars/all.yml as constants. This is inline with general coding guideline of replacing magic numbers/literals with constants as per the Clean Code book(https://moderatemisbehaviour.github.io/clean-code-smells-and-heuristics/general/g25-replace-magic-numbers-with-named-constants.html). The only problem I see with this approach is that technically the variables defined in group_vars/all.yml are overridable as per the variable precedence which is against the general coding guideline of using a constant.
I wanted to check with the community if ansible provides a way of defining constants. From preliminary google search, posts on stackoverflow and searching ansible docs suggest that ansible doesn’t have the feature of constants.
In that case, is there some general convention in ansible similar to python where constants are defined in upper case with words separated by underscores? The ansible tips and tricks doesn’t seem to talk about this.
During a recent code review of an ansible playbook, I noticed one dev using variables defined in playbook group_vars/all.yml as constants. This is inline with general coding guideline of replacing magic numbers/literals with constants as per the Clean Code book(https://moderatemisbehaviour.github.io/clean-code-smells-and-heuristics/general/g25-replace-magic-numbers-with-named-constants.html). The only problem I see with this approach is that technically the variables defined in group_vars/all.yml are overridable as per the variable precedence which is against the general coding guideline of using a constant.
I wanted to check with the community if ansible provides a way of defining constants.
I haven’t come across it. AFAIK variables can always be changed in ansible.
From preliminary google search, posts on stackoverflow and searching ansible docs suggest that ansible doesn’t have the feature of constants.
In that case, is there some general convention in ansible similar to python where constants are defined in upper case with words separated by underscores? The ansible tips and tricks doesn’t seem to talk about this.
I don’t think Python has constants either.
And I haven’t seen the uppercase+underscores var name formatting in ansible so much to call it a convention.
But, nothing stops you or your team from adopting it as one.