Something Similar to Ruby's Case Operator

Hi all
We have a mix of Debian and Redhat in our infrastructure so basically for every single step, I have to add two of the same operators for both of them, like this:

  • name: Install prerequisite packages
    action: apt pkg={{item}} state=installed
    with_items:

  • sysstat
    when: ansible_os_family == ‘Debian’

  • name: Install prerequisite packages
    action: yum pkg={{item}} state=installed
    with_items:

  • sysstat
    when: ansible_os_family == ‘RedHat’

Does Ansible have an operator similar to the case operator in Ruby or Chef and Puppet for that matter?

There is no case statement in ansible.

You can look into “group_by” when eliminating repeated conditionals if this works for you.

  • hosts: webservers
    tasks:

  • group_by: key=os-{{ ansible_os_family }}

  • hosts:webservers:&os-Debian
    tasks:

etc