OS variables variance

I would gladly get some advice here.

First, Ansible is so great - I replaced my Puppet proviosning for our UNIX hosts in a few days and everything is working so well and easy to maintain.

We support AIX, HP-UX, Solaris and Linux (RedHat, Suse, Ubuntu) so I have a commands variance between OS and also between OS variants.
Windows clients support is so so so so asked around here (simple zip extracting module, commands execution)

I use roles and at the moment I took all different shared roles parameters and shell command paths into param files at playbooks/vars/AIX.yml, HP-UX.yml and so…

For Linux - I have Linux.yml, Redhat_7.yml and Suse.yml for example as some commands are different between RHEL 5-6, RHEL 7, Ubuntu and Suse and some are the same.

In my playbook I use the vars_files feature as I found in the docs example below to match for the specific OS version or OS type or Linux in general:

vars_files:
    - "vars/common.yml"
    - [ "vars/{{ ansible_os_family }}.yml", "vars/os_defaults.yml" ]

It works well but introduces duplication as the first matching file is selected and I need the non different parameters inside this file too…

First question, in the above example, common.yml parameter X will get overidden by {{ansible_os_family}}.yml defined parameter X?

Second question, I would like to separate parameters of OS commands to one file, Application X to another and so…

I get a real big section of this vars_files that is very ugly to copy paste for each playbook…

Can you suggest a better way to handle this please?

Thanks

“It works well but introduces duplication as the first matching file is selected and I need the non different parameters inside this file too…”

Not sure what you mean by “non-different parameters”.

First question, in the above example, common.yml parameter X will get overidden by {{ansible_os_family}}.yml defined parameter X?”

Yes. It comes later.

Second question, I would like to separate parameters of OS commands to one file, Application X to another and so…”

Then consider using group_vars/ variables for these application things.

See also “include_vars” for a module that makes this easy to accomodate into roles.

  • include_vars: “{{ ansible_os_family }}.yml”, etc.

Thanks,
by 'Non different' i meant as an example, tar command path is different between RedHat 7 and the rest but gunzip is the same. I need to use both on all roles.
The files_vars list will include the rhel 7 or the linux for the rest.
so i need gunzip on both to get it into all roles...

Hi,

trying include_vars I encounter an issue

Trying this inside a role task:

  • include_vars: “{{item}}”
    with_first_found:
  • “vars/OS/{{ansible_distribution}}_{{ansible_distribution_major_version}}.yml”
  • “vars/OS/{{ansible_distribution}}.yml”
  • “vars/OS/{{ansible_system}}.yml”

I get an error:

TASK: [app1 | include_vars ] *******************************************
ESTABLISH CONNECTION FOR USER: root
failed: [linux1] => {“failed”: true, “item”: null}
msg: No source file given

In my server I have playbooks/vars/OS/Linux.yml

Is there something wrong with my usage?

Seems like the intermediate directory “playbooks/” may be the problem.

Didn’t help but I eventually got all Linux vars into a Linux.yml, all RedHat dependent or Suse into their own and more specific OS family with specific version into its own file.
Created an empty vars file and used the list includes feature.

In the playbook I include:
ansible_system file
than try [ansible_os_familly, empty.yml]
than try [os family with major version , empty.yml]

Works great.

Adding “gather_facts: true” in my playbook fixed the “No source file given” error.