Constants in ansible playbooks

17 views
Skip to first unread message

livings...@gmail.com

unread,
Feb 16, 2021, 12:03:35 AM2/16/21
to Ansible Project
Hi,

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.

Dick Visser

unread,
Feb 16, 2021, 1:39:42 AM2/16/21
to ansible...@googlegroups.com
On Tue, 16 Feb 2021 at 06:03, livings...@gmail.com <livings...@gmail.com> wrote:
Hi,

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. 



--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/70d3c2b4-d3be-443d-9ff4-f2c3ea35948dn%40googlegroups.com.
--
Sent from a mobile device - please excuse the brevity, spelling and punctuation.

Brian Coca

unread,
Feb 16, 2021, 3:45:17 PM2/16/21
to Ansible Project
extra_vars are not overridable and could be considered constants.

--
----------
Brian Coca

Nuno Jordão

unread,
Feb 16, 2021, 5:11:38 PM2/16/21
to ansible...@googlegroups.com
but extra_vars are only defined as a command line parameter, right?

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.

Brian Coca

unread,
Feb 17, 2021, 9:34:26 AM2/17/21
to Ansible Project
correct
--
----------
Brian Coca

Lars Liedtke

unread,
Feb 18, 2021, 4:45:09 AM2/18/21
to ansible...@googlegroups.com
You could create a yaml file e.g. contants.yml and the include that via
commandline parameter with -e '@constants.yml'

Am 17.02.21 um 15:33 schrieb Brian Coca:
> correct

--
---
punkt.de GmbH
Lars Liedtke
.infrastructure

Kaiserallee 13a
76133 Karlsruhe

Tel. +49 721 9109 500
https://infrastructure.punkt.de
in...@punkt.de

AG Mannheim 108285
Geschäftsführer: Jürgen Egeling, Daniel Lienert, Fabian Stein

Reply all
Reply to author
Forward
0 new messages