how to have general playlist call specific playlist and/or roles?

171 views
Skip to first unread message

Snyder, Chris

unread,
May 7, 2014, 1:27:41 PM5/7/14
to ansible...@googlegroups.com

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:

 

- hosts: all

  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}}

 

- hosts: {{myos}}

  Roles:

    - {{myos}}

 

So, how do other people solve/organize their solutions to problems similar to this?

 

Thx

Chris.

James Cammarata

unread,
May 7, 2014, 2:48:53 PM5/7/14
to ansible...@googlegroups.com
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.



--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/BFD6B7398AEB474A9A28B39B9B5D00CB588A9696%40SRAexMBX05.sra.com.
For more options, visit https://groups.google.com/d/optout.

Snyder, Chris

unread,
May 8, 2014, 10:36:08 AM5/8/14
to ansible...@googlegroups.com

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.

Adam Morris

unread,
May 8, 2014, 4:58:26 PM5/8/14
to ansible...@googlegroups.com


On Thursday, May 8, 2014 7:36:08 AM UTC-7, Snyder, Chris wrote:

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
- include: group.yml

# Perform a basic server configuration
- include: checklist.yml


The group.yml looks like this...
---
# file: group.yml
- hosts: all
  gather_facts: true 
  tasks:
    - group_by: key={{ ansible_os_family }} 
    - group_by: key={{ ansible_product_name.split()[0] }}

Now I can do things like have playbooks like this...
---
- hosts: RedHat
  gather_facts: false
  roles:
    - common
    - redhat

If you can split tasks into groups then you can work out how to split your servers into similar groups and you're set.


I hope that this helps,
   Adam
  
 

Michael DeHaan

unread,
May 9, 2014, 7:53:53 PM5/9/14
to ansible...@googlegroups.com

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"


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages