# How would you set variables in this case?

**URL:** <https://forum.ansible.com/t/how-would-you-set-variables-in-this-case/14318>\
**Category:** Ansible Project\
**Created:** [January 10, 2014, 11:59pm UTC](https://forum.ansible.com/t/how-would-you-set-variables-in-this-case/14318 "2014-01-10T23:59:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adam\_Morris](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@Adam\_Morris](https://forum.ansible.com/u/Adam_Morris)\
**Post date:** [January 10, 2014, 11:59pm UTC](https://forum.ansible.com/t/how-would-you-set-variables-in-this-case/14318/1 "2014-01-10T23:59:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Brian\_Green](https://avatars.discourse-cdn.com/v4/letter/b/e495f1/32.png) [@Brian\_Green](https://forum.ansible.com/u/Brian_Green)\
**Post date:** [January 11, 2014, 12:16am UTC](https://forum.ansible.com/t/how-would-you-set-variables-in-this-case/14318/2 "2014-01-11T00:16:47Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![Adam\_Morris](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@Adam\_Morris](https://forum.ansible.com/u/Adam_Morris)\
**Post date:** [January 11, 2014, 1:19am UTC](https://forum.ansible.com/t/how-would-you-set-variables-in-this-case/14318/3 "2014-01-11T01:19:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Brian\_Green](https://avatars.discourse-cdn.com/v4/letter/b/e495f1/32.png) [@Brian\_Green](https://forum.ansible.com/u/Brian_Green)\
**Post date:** [January 11, 2014, 4:11am UTC](https://forum.ansible.com/t/how-would-you-set-variables-in-this-case/14318/4 "2014-01-11T04:11:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Adam\_Morris](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@Adam\_Morris](https://forum.ansible.com/u/Adam_Morris)\
**Post date:** [January 13, 2014, 5:56pm UTC](https://forum.ansible.com/t/how-would-you-set-variables-in-this-case/14318/5 "2014-01-13T17:56:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Adam\_Morris](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@Adam\_Morris](https://forum.ansible.com/u/Adam_Morris)\
**Post date:** [January 13, 2014, 8:53pm UTC](https://forum.ansible.com/t/how-would-you-set-variables-in-this-case/14318/6 "2014-01-13T20:53:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Adam\_Morris](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@Adam\_Morris](https://forum.ansible.com/u/Adam_Morris)\
**Post date:** [January 14, 2014, 12:04am UTC](https://forum.ansible.com/t/how-would-you-set-variables-in-this-case/14318/7 "2014-01-14T00:04:06Z")

</div>

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
