Search code examples
ansibleansible-2.xansible-inventory

How to skip ansible roles for certain servers


I have following code wherein I have 3 roles as below. I do not want to execute "def" role for hosts which has group_name "er". So when servers also belong to "er" group, skip def role.

hosts: qw
vars:
roles:
  abc
  def
  fgh


Solution

  • You can use Conditionals with roles

    - hosts: qw
      vars:
      roles:
        - role: abc
        - role: def
          when: 'er' not in group_names
        - role: fgh
    

    Something like that would likely work for your usecase.