# Constants in ansible playbooks

**URL:** <https://forum.ansible.com/t/constants-in-ansible-playbooks/34623>\
**Category:** Ansible Project\
**Created:** [February 16, 2021, 5:03am UTC](https://forum.ansible.com/t/constants-in-ansible-playbooks/34623 "2021-02-16T05:03:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Emmanuel\_Livingstone](https://avatars.discourse-cdn.com/v4/letter/e/5f8ce5/32.png) [@Emmanuel\_Livingstone](https://forum.ansible.com/u/Emmanuel_Livingstone)\
**Post date:** [February 16, 2021, 5:03am UTC](https://forum.ansible.com/t/constants-in-ansible-playbooks/34623/1 "2021-02-16T05:03:35Z")

</div>

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](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](https://docs.ansible.com/ansible/latest/index.html) 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](https://docs.ansible.com/ansible/latest/user_guide/playbooks_best_practices.html) doesn’t seem to talk about this.

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [February 16, 2021, 6:39am UTC](https://forum.ansible.com/t/constants-in-ansible-playbooks/34623/2 "2021-02-16T06:39:14Z")

</div>

> 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](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](https://docs.ansible.com/ansible/latest/index.html) 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](https://docs.ansible.com/ansible/latest/user_guide/playbooks_best_practices.html) 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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [February 16, 2021, 8:44pm UTC](https://forum.ansible.com/t/constants-in-ansible-playbooks/34623/3 "2021-02-16T20:44:39Z")

</div>

extra\_vars are not overridable and could be considered constants.

---

<div class="post-metadata">

**Author:** ![Nuno\_Jordao](https://avatars.discourse-cdn.com/v4/letter/n/a4c791/32.png) [@Nuno\_Jordao](https://forum.ansible.com/u/Nuno_Jordao)\
**Post date:** [February 16, 2021, 10:10pm UTC](https://forum.ansible.com/t/constants-in-ansible-playbooks/34623/4 "2021-02-16T22:10:51Z")

</div>

but extra\_vars are only defined as a command line parameter, right?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [February 17, 2021, 2:33pm UTC](https://forum.ansible.com/t/constants-in-ansible-playbooks/34623/5 "2021-02-17T14:33:53Z")

</div>

correct

---

<div class="post-metadata">

**Author:** ![Lars\_Liedtke](https://avatars.discourse-cdn.com/v4/letter/l/c68b51/32.png) [@Lars\_Liedtke](https://forum.ansible.com/u/Lars_Liedtke)\
**Post date:** [February 18, 2021, 9:44am UTC](https://forum.ansible.com/t/constants-in-ansible-playbooks/34623/6 "2021-02-18T09:44:51Z")

</div>

You could create a yaml file e.g. contants.yml and the include that via  
commandline parameter with -e '@constants.yml'
