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

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

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

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…)

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
  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,

  -vlado

It looks good, thank you