I want to set snmp_group to “wheel” when ansible_os_family==‘RedHat’, and “snmp” when ansible_os_family==‘Debian’. This logic definitely belongs in the role, not in any playbook which wraps it.
At the moment all I can think of is some Jinja2 conditional buried directly into the assignment:
snmp_group: “{% if ansible_os_family==‘RedHat’ %}wheel{% else %}snmp{% endif %}”
This seems to work, but it’s pretty ugly. I can’t see how to do anything equivalent to vars_files in a role, or indeed how to include anything under vars/ except vars/main.yml
I think the way I would do this would be to create a var named
"snmp_group" in vars/{{ ansible_os_family}}.yml which would take on
different values for different OS types.
I'm not sure if there is a better way but even if there was I think
that's a better/cleaner way of specifying it rather than adding logic
in your play to set it.
-John
when ansible_os_family==‘Debian’, set variable snmp_group=snmp
when ansible_os_family==‘RedHat’, set variable snmp_group=wheel
write the task once, with group={{snmp_group}}
It seems there is no good way of doing this in ansible. Options are:
Write the tasks multiple times with conditions, as shown above
Split into two separate but almost identical roles in separate files. Use groups to select the correct role. That was your suggestion. I think it makes maintaining the logic harder because it’s duplicated in different parts of the filesystem, and it puts the onus on the role user to select the correct role.
Set up vars from the playbook: this means the role is split into two parts which you must remember to invoke correctly. It’s because playbooks have a feature (vars_files) that roles don’t.
hosts: xxx
vars_files:
roles/snmp/vars/{{ansible_os_family}}.yml
roles:
snmp
Use inline jinja2 conditionals in vars/main.yml. This seems like the least bad option here.
[roles/snmp/vars/main.yml]
snmp_group: “{% if ansible_os_family==‘RedHat’ %}wheel{% else %}snmp{% endif %}”
Maybe write a parameterised role, and have two other roles snmp_redhat and snmp_debian which call it via ‘meta’, passing the correct value for snmp_group? Obscure, and in any case the meta roles can only be executed before the roles within the main role.
A question on the use case: Is that example used to add new things to existing hosts, or to initially configure recently created hosts? Also (if this is for new hosts), do you use both OS families or are you future-proofing in case you ever need to do so?
Wait are you saying if I created Debian.yml in a role’s vars folder and then call that role then all the Debian hosts would import that file? Or, just that I could create Debian.yml and do an ‘include:’ for vars/{{ ansible_os_family}}.yml manually?
I have both Ubuntu and CentOS hosts that I’m managing, and I have a fairly complicated role for configuring snmpd (which includes the mad-hacking MDRAID and SMARTCTL MIBs) that I want to share between them.
This role is generally run when the machine is first installed, but it can be re-run e.g. to push out a modified version of the mad-hacking scripts, new community string etc.