How to use group_by to map a role only for a specific OS

1,620 views
Skip to first unread message

Jörg Kastning

unread,
Jul 25, 2016, 7:20:08 AM7/25/16
to Ansible Project
Dear all,

I'd like to map a role to a group of hosts with a specific operating system.

So far I read Applying ‘when’ to roles and includes and the module description of group_by. But I could not figure out how to use group_by to use a specific role only in cases where a variable matches an operating system. Could someone give an example, please?

Do I have to set some group_var first to use with group_by?

Kind regards,
Joerg

Kai

unread,
Jul 25, 2016, 10:22:23 AM7/25/16
to ansible...@googlegroups.com
Hi Jörg!

On 25-07-16 13:20, Jörg Kastning wrote:
> Dear all,
>
> I'd like to map a role to a group of hosts with a specific operating system.

This should be possible with a playbook somewhat along these lines:

First play working on the relevant hosts (not filtered by OS) with one
pre_task:
- name: group by OS
group_by: key=bla_bla_{{ [ansible_fact_for_OS_name] }} # (you may add
a default, if necessary)
changed_when: False

The you'll want to have a second play in the same playbook working on
one (or more) of the just defined group(s), like:
- hosts: bla_bla_Debian # (or bla_bla_Windows, or whatever)

roles:
- [add-your-role-here]

You'll get an output for all hosts of the bigger group of play one, but
play two will run for just that hosts you want it to run for. At least
this way it works for me - hope it helps.

Cheers, Kai


>
> So far I read Applying ‘when’ to roles and includes
> <http://docs.ansible.com/ansible/playbooks_conditionals.html#applying-when-to-roles-and-includes>
> and the module description of group_by
> <http://docs.ansible.com/ansible/group_by_module.html>. But I could not
> figure out how to use group_by to use a specific role only in cases
> where a variable matches an operating system. Could someone give an
> example, please?
>
> Do I have to set some group_var first to use with group_by?
>
> Kind regards,
> Joerg
>
> --
> 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
> <mailto:ansible-proje...@googlegroups.com>.
> To post to this group, send email to ansible...@googlegroups.com
> <mailto:ansible...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/9ea0b46e-d70d-422e-95c2-48e3136a9a58%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/9ea0b46e-d70d-422e-95c2-48e3136a9a58%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

Jörg Kastning

unread,
Aug 2, 2016, 8:05:49 AM8/2/16
to Ansible Project
Hello Kai,

Thanks for your answer. But the method you mentioned don't work for me. I created the following plays:

---
- hosts: all

- name: group by OS
  group_by: key=os_{{ [ansible_distribution] }}
  changed_when: False

- hosts: os_RedHat
  roles:
    - common

But when I try to run this playbook I got an error:

ansible-playbook -i staging site.yml -C
ERROR! 'group_by' is not a valid attribute for a Play

The error appears to have been in '/root/ansible/set_baseline.yml': line 2, column 3, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

---

- name: group by OS
  ^ here

At first I thought my problem could be using the "- hosts" option two times in one playbook. But I got the same error without the first "- hosts" option in the file. What am I doing wrong?

Thanks in advance for you help.

Kind regards,
Joerg

Jörg Kastning

unread,
Aug 2, 2016, 8:21:21 AM8/2/16
to Ansible Project
Ah, sorry. My mistake.

Here is my current playbook, which works as designed:
---
- hosts: all

  tasks:
    - name: Group by OS

      group_by: key=os_{{ ansible_distribution }}
      changed_when: False

- hosts: os_RedHat
  roles:
    - common

Regards,
Joerg
Reply all
Reply to author
Forward
0 new messages