I’m having a really hard time wrapping my head around how to organize my plays and roles with Ansible.
What I have:
I have multiple Linux distros I need to support with Ansible. Each distro has different requirements which manifest themselves as different playbooks/roles/tasks/host groups.
What I want (i.e. this is what my brain is telling me SHOULD be the correct solution for this type of situation. And yes, I DO have a programmer back ground):
Call a single, general Ansible playbook that determines the specific distro/host on client host and then executes a more specific playbook and/or set of roles for my target host or host group.
What I don’t want to do:
-
I don’t want to have to programmatically determine the distro of my target host BEFORE calling Ansible so I can pass some OS-specific playbook name to Ansible on the command line for specific host or groups of hosts.
-
I don’t want a SINGLE playbook which contains plays for all the OS’s I support – that’s just too large and unwieldy.
Obviously, Ansible doesn’t parse variables as part of include statements, but IDEALLY, the following example is how I feel I should be solving my problem:
Tasks:
- name: Determine distrbution name and release
set_fact: myos=“{{ansible_distribution|lower}}{{ansible_lsb.major_release}}”
- name: Group hosts by distro and release
action: group_by key={{myos}}
Roles:
So, how do other people solve/organize their solutions to problems similar to this?
Thx
Chris.
This will not work, since the hosts and roles must be known at the time the playbook is read, so you can’t do variable substitutions on those.
An alternative to this method is to have just the one role that includes different task files based on the values of the system facts. See https://github.com/ansible/ansible/blob/devel/test/integration/roles/test_apt/tasks/main.yml for example of how we limit the apt test to running on certain distributions. This way, you can keep common tasks together while only splitting out the distro-specific pieces.
This way, you could do the following:
- hosts: all
roles:
- { role: do_distro_stuff, target_os: “{{ansible_distribution|lower}}{{ansible_lsb.major_release}}” }
and then in roles/do_distro_stuff/main.yml:
includes for distro-version specific tasks
- include: ‘ubuntu12.yml’
when: target_os == ‘ubuntu12’
- include: ‘ubtunu14.yml’
when: target_os == ‘ubuntu14’
…
and then do common tasks here
So you’d have to add new lines there when new distros come out, but that’s not something that occurs frequently so it’s not a major concern.
Thanks for this example. I’m still pondering over this and it’s possibilities but I’m thinking that I’m probably going to have to rip out a lot of the roles I’ve already defined and make them into task files called from more general roles unless I do a lot of explicit ‘when’ statements for calling roles in my playbooks.
Or maybe not. I’m still trying to get my head around of it.
Here’s the top of my site.yml
file: site.yml
Set up groups automatically for OS
Perform a basic server configuration
The group.yml looks like this…
set_fact shouldn’t be needed for basic variable concation. You can actually just define a variable that references the other two variable names and it will work
vars:
“myos” : “{{ ansible_os_family }}-{{ other }}”
etc.
Another handy trick to include distro specific variables is:
tasks:
- include_vars: “{{ ansible_os_family }}.yml”