How to delete group using puppet when main group of another user

puppet

I have the following Puppet recipe:

group{'pablo':
  ensure => absent,
  gid    => 1020,
}

user{'pablo':
  ensure     => absent,
  gid        => 1020,
  managehome => false,
}

But the dependencies are "reversed": user depends on group. That works fine when creating resources, but when we want to delete, the relationship should be the reverse. At the moment, the user resource will depend on the group, but the group can't be deleted because a user has itself as a main group. Oops…

The exact error messages are:

Error: Could not delete group pablo: Execution of '/usr/sbin/groupdel pablo' returned 8: groupdel: cannot remove the primary group of user 'pablo'
Error: /Stage[main]/Seevibes::Admins/Seevibes::User[pablo]/Group[pablo]/ensure: change from present to absent failed: Could not delete group pablo: Execution of '/usr/sbin/groupdel pablo' returned 8: groupdel: cannot remove the primary group of user 'pablo'
Error: Could not delete group pablo: Execution of '/usr/sbin/groupdel pablo' returned 8: groupdel: cannot remove the primary group of user 'pablo'
Error: /Stage[main]/Seevibes::Admins/Seevibes::User[pablo]/Group[pablo]/ensure: change from present to absent failed: Could not delete group pablo: Execution of '/usr/sbin/groupdel pablo' returned 8: groupdel: cannot remove the primary group of user 'pablo'
Notice: /User[pablo]: Dependency Group[pablo] has failures: true
Warning: /User[pablo]: Skipping because of failed dependencies

How can I delete a user and group using Puppet?

Best Answer

Try removing the user first then only remove the group once user has been removed. eg.

user {'pablo':
   ensure  =>  absent,
   }

group {'pablo':
   ensure => absent,
   require => User['pablo'],
     }