Unregister a host entirely from the CLI?

Hello,

I cannot remove a host from Foreman using subscription-manager.

I’d expect the following to remove the host from Foreman’s UI, but it doesn’t.
subscription-manager remove --all
subscription-manager unregister

The host is still visible in the UI after these steps complete successfully.

Is it possible to remove a host from Foreman from the managed host itself? Are there additional/different steps I need to perform?

Thanks!

Hi,

to my knowledge, there is no way to completely remove a host from Foreman from the managed hosts side.
The commands you are using remove the subscription related part of a Foreman/Katello host, but the “Foreman only” parts (provisioning, config management, etc) stay untouched.
What exactly are you trying to achieve? Maybe there is another way to get there.

Regards

Thanks for the reply areyus.

I’m using molecule to test a variety of ansible roles. As part of that process, I create a host, test my ansible stuff, then destroy the host. Since this host is ephemeral and only used for testing, I would like to be able to remove it completely from foreman when I’m done using it, and it would be ideal if I can do that from the host itself, rather than manually interacting with Foreman each time I do a build/test/destroy cycle. This is just one example – we have several similar efforts going on simultaneously, so we wind up with a variety of “ghost hosts” in Foreman.

Is there a way to delete an ephemeral host from Foreman, but initiating that deletion from the host itself?

The easiest way to achieve this would probably be to use Foreman’s REST API.
The API Documentation for Foreman and all your installed Plugins can be found under http://your-foreman.example.com/apidoc. The API endpoint DELETE /api/hosts/:id should be what you are looking for. That should delete the host and all associated objects.

Regards

2 Likes

OMG! I had no idea that documentation existed!

Thanks areyus – this should definitely provide the functionality I’m looking for. Thank you!

1 Like

You’re welcome :slight_smile:
The API documentation is also available somewhere online, though the docs on your own instance have the added upside that they include all your plugins (and only those) and are always the right one for your current version.
Please mark the thread as solved if you are happy with that solution so others know a solution has been fund (The “checkbox” icon on the post with the solution). Thanks :slight_smile:

1 Like

If you’re already working with ansible, https://github.com/theforeman/foreman-ansible-modules might also be useful. If it can’t already delete a host, that’s probably a good feature request.

It totally can destroy hosts. Creating them is harder, but destroying is easy :wink:

1 Like

Ah, that’s a good idea as well! In this particular case, I think the API approach will be a better fit, but I can definitely see the value of using the ansible modules as well. Thanks for the heads-up!

The modules use the API, so you’re good :wink:

katello-client-bootstrap can also remove hosts using the API, and so can hammer, so you have multiple ways to achieve your goal :slight_smile:

If you’re using the API from Python, I’d recommend apypie:


It’s driving the foreman-ansible-modules.

There is also a setting if you REALLY want to Delete a host from subscription-manager:

Go to Administer > Settings > Content
Flip ‘unregister_delete_host’ to true.

Note that if you are using a virtual managed provisioned by foreman, that vm will get deleted! Generally this setting should be avoided unless you are aboslutely sure it meets your workflow needs. It is also a server-wide setting, so would apply to ALL subscription-manager unregister commands’