How would you set variables in this case?

I have been building up a bunch of tasks for some standard configuration items. I have the basic tasks all working perfectly for new installs for both AIX and RedHat/CentOs 6. Now I’m testing against one of the older RedHat installs that we have (RedHat 5) and having to make some changes… So far things have been going well until I got to configuring syslog. On RedHat 6 I have to modify /etc/rsyslog.conf and restart rsylogd. On AIX it is slightly different so I set variables in my current OS specific group_vars files RedHat and AIX… On RedHat 5 most of the variables work perfectly for me, except /etc/rsyslog.conf becomes /etc/syslog.conf and rsyslogd becomes syslogd. So I can easily modify the variables I’m using to take that into account. But this means I need to set different variables for RedHat 5 and RedHat 6. although most of the variables are the same.

So how would you suggest setting variables in this case? I can split tasks out so I don’t need to set variables, but then I have much larger task files that contain essentially the same steps. I’m trying to reuse as much as possible, but I’m at a bit of a loss here.

I was wondering if I could include a RedHat variable file into a RedHat5 or RedHat6 variable file but I can’t see how to do it.

I could keep the tasks the same and have almost identical variable files with minor changes for RedHat 5 and RedHat 6, but that is still not as clean as I would like.

Any thoughts.

Adam

How about keep your default variables in roles/vars/main.yml and then for variables that would be considered special case or overrides you put them elsewhere with higher precedence. For your RHEL 5 example, take a look at http://docs.ansible.com/playbooks_variables.html#conditional-imports and you could use something similar like
vars/{{ ansible_lsb.id }}_{{ ansible_lsb.major_release }}.yml where you’d populate the RedHat_5.yml file with any of the variables you need overridden (my variable names/values might need some checking though).

Brian

I guess I wasn’t as clear as I could have been…

I currently have a single task that updates the syslog configuration file and restarts the syslog daemon. It works perfectly for AIX, RedHat 5 and RedHat 6. This is one of many tasks I have written so far.

The problem I have is that for most of my tasks I have differences between RedHat and AIX that require variables… So I had two variables files that I am picking up, one for AIX and one for RedHat. This is the one place that RedHat 5 and RedHat 6 differ in a way that can easily be fixed with variables (rsylog versus syslog)… But I either copy my RedHat variable file and create two almost identical files with a difference of two lines out of 11, or I find another way of doing this…

For the moment I’ve copied the RedHat file into RedHat5 and RedHat6. I was just wondering if there was a better way than treating RedHat 5 and RedHat 6 as different operating systems.

Adam

Would the following work? For the few variables that differ, substitute them at the beginning of the playbook. It could even allow you to use a single variable file if you extrapolate all your variables in this manner.

vars:
syslog_var: “{{ ansible_lsb.id }}_{{ ansible_lsb.major_release ))syslog_var"
syslog_var2: "{{ ansible_lsb.id }}
{{ ansible_lsb.major_release ))_syslog_var2”
and use {{ syslog_var }} in the tasks/templates.

Then in your RHEL vars file:
RedHat_5_syslog_var: foo
RedHat_6_syslog_var: bar

RedHat_5_syslog_var2: foo2
RedHat_6_syslog_var2: bar2

And similar for the AIX vars file.

You could use conditionals at the task level, or in templates, but if you only have a couple differences, I think this is a simpler method.

Lots of ways to accomplish this, depends on your wants and needs. My biggest concern is having others use/maintain the playbooks and being able to understand what’s going on, and without having to grok the more difficult concepts. Ansible is pretty simple, but having a junior level sysadmin try to do things like address complex data or understanding variable precedence are items I’d like to avoid.

Brian

Indeed, and I think that that idea might expand further to help simplify some of my other tasks. Of course I won’t be able to resolve every issue like that, RedHat Registration requires different modules on RedHat 5 and RedHat 6 unfortunately, but I’m going to have to see how much I can improve by changing variables around.

Thank you very much for the idea.

Adam

Here’s what I ended up with in my group_vars file…

syslog_RedHat_5: syslog
syslogd_RedHat_5: syslog
syslog_RedHat_6: rsyslog
syslogd_RedHat_6: rsyslog

syslog: “{% if ansible_distribution_version|truncate(1,true,‘’) >= ‘6’ %}{{ sysl
og_RedHat_6 }}{% else %}{{ syslog_RedHat_5 }}{% endif %}”
syslogd: “{% if ansible_distribution_version|truncate(1,true,‘’) >= ‘6’ %}{{ sys
logd_RedHat_6 }}{% else %}{{ syslogd_RedHat_5 }}{% endif %}”

Of course I could remove the top four variables and just set the values properly using the Jinja2 conditionals below… I probably will clean that up in my next pass, but it was a slowly evolving solution…

Adam

Yup reducing the entries and combining the conditional with setting the variable worked perfectly…
(plus reformatting)

Here’s an example from my group_vars/RedHat file which sets the syslog variable differently dependent on whether the RHEL version is less than six or six or greater.

syslog: “{% if ansible_distribution_version|truncate(1,true,‘’) >= ‘6’ %}rsyslog{% else %}syslog{% endif %}”

Adam