# how to best accomodate different default values for variables in a role. eg list of ports to open may change when a new version of the software (= role ) is installed

**URL:** <https://forum.ansible.com/t/how-to-best-accomodate-different-default-values-for-variables-in-a-role-eg-list-of-ports-to-open-may-change-when-a-new-version-of-the-software-role-is-installed/26498>\
**Category:** Ansible Project\
**Created:** [January 17, 2018, 10:09am UTC](https://forum.ansible.com/t/how-to-best-accomodate-different-default-values-for-variables-in-a-role-eg-list-of-ports-to-open-may-change-when-a-new-version-of-the-software-role-is-installed/26498 "2018-01-17T10:09:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris7444](https://avatars.discourse-cdn.com/v4/letter/c/8edcca/32.png) [@Chris7444](https://forum.ansible.com/u/Chris7444)\
**Post date:** [January 17, 2018, 10:09am UTC](https://forum.ansible.com/t/how-to-best-accomodate-different-default-values-for-variables-in-a-role-eg-list-of-ports-to-open-may-change-when-a-new-version-of-the-software-role-is-installed/26498/1 "2018-01-17T10:09:38Z")

</div>

Sorry of the subject is not very descriptive. Let me be more explicit here

I have a role that installs a piece of software at version N. this role by default open ports number x and y in the firewall. This list of ports is defined in /default/main.yml

Now I want to install version N+1 of the same software and version N+1 requires ports x, y and z to be open in the firewall but other than that, everything remains the same

I am not sure what the best practice is here

Of course, I am asking because would like to avoid to create a role for each version of the software just because the list of ports changes

How do you guys generally address this type of question

Thank you  
Christophe

---

<div class="post-metadata">

**Author:** ![mkempster22uu](https://avatars.discourse-cdn.com/v4/letter/m/e0b2c6/32.png) [@mkempster22uu](https://forum.ansible.com/u/mkempster22uu)\
**Post date:** [January 17, 2018, 10:33am UTC](https://forum.ansible.com/t/how-to-best-accomodate-different-default-values-for-variables-in-a-role-eg-list-of-ports-to-open-may-change-when-a-new-version-of-the-software-role-is-installed/26498/2 "2018-01-17T10:33:23Z")

</div>

I’m not much of an expert but I have done similar things based on OS version which can be done by having a task for each version then using ‘when’ conditionals and using the ansible OS version fact.  
In a similar way if you could assign the version number to a variable you could base it off that

E.g

- name: Add ssh public key file - RedHat  
lineinfile:  
dest: /home/user/.ssh/authorized\_keys  
line: “{{ keyvar }}”  
state: present  
create: yes  
owner: user  
group: user  
mode: 0640  
when: ansible\_distribution == ‘RedHat’  
become: yes

---

<div class="post-metadata">

**Author:** ![Chris7444](https://avatars.discourse-cdn.com/v4/letter/c/8edcca/32.png) [@Chris7444](https://forum.ansible.com/u/Chris7444)\
**Post date:** [January 17, 2018, 12:12pm UTC](https://forum.ansible.com/t/how-to-best-accomodate-different-default-values-for-variables-in-a-role-eg-list-of-ports-to-open-may-change-when-a-new-version-of-the-software-role-is-installed/26498/3 "2018-01-17T12:12:13Z")

</div>

Well, this is not what I want. I dont want to modify the tasks everytime the list of ports changes. I guess the answer is by using some include statement (which will include a file containing the version specific list of ports). I am just wondering where this include file should be included. What the best place is for doing this include (eg inside /defaults/main.yml vs playbooks using the role, etc…)

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [January 17, 2018, 12:45pm UTC](https://forum.ansible.com/t/how-to-best-accomodate-different-default-values-for-variables-in-a-role-eg-list-of-ports-to-open-may-change-when-a-new-version-of-the-software-role-is-installed/26498/4 "2018-01-17T12:45:12Z")

</div>

Hi Chris,

> Well, this is not what I want. I dont want to modify the tasks everytime  
> the list of ports changes. I guess the answer is by using some include  
> statement (which will include a file containing the version specific list  
> of ports). I am just wondering where this include file should be included.  
> What the best place is for doing this include (eg inside  
> \<role\>/defaults/main.yml vs playbooks using the role, etc...)

An option would be to use "include\_vars" in a task, that suits  
your purpose best (for example tasks/main.yml)

- name: Add {{ package\_version }} specific variables  
&nbsp;&nbsp;include\_vars: "{{ package\_version }}.yml"

And put the files {{ package\_version }}.yml into the vars/  
directory.

Then the variable package\_version controls what's going to be  
installed. You might want to specify the default  
package\_version in defaults/.

HTH, Cheers,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![Chris7444](https://avatars.discourse-cdn.com/v4/letter/c/8edcca/32.png) [@Chris7444](https://forum.ansible.com/u/Chris7444)\
**Post date:** [January 17, 2018, 9:25pm UTC](https://forum.ansible.com/t/how-to-best-accomodate-different-default-values-for-variables-in-a-role-eg-list-of-ports-to-open-may-change-when-a-new-version-of-the-software-role-is-installed/26498/5 "2018-01-17T21:25:37Z")

</div>

It looks good, thank you
