Set host content view via API/Ansible

Problem: Hello,
I want to set/update content view on a host via ansible, it’s possible to achieve this using API? I didn’t find anything on the foreman API doc API documentation

Really need your help! :sweat_smile:
I know that is possible with hammer cli.

Foreman Ansible Modules’ host module seems to allow you to set the content view: theforeman.foreman.host module – Manage Hosts — Ansible Documentation

2 Likes

To set via the API, you’ll need the content view ID and lifecycle environment ID. Then you can update the host via PUT /api/v2/hosts/:id by sending the parameters

host[content_facet_attributes][content_view_id]
host[content_facet_attributes][lifecycle_environment_id]

For example, if the host ID is 2, CV ID is 2 and LCE ID is 1:

PUT /api/v2/hosts/2

with the request payload like

{"id":2,"host":{"content_facet_attributes":{"content_view_id":2,"lifecycle_environment_id":1}}}
3 Likes

Side note: I would recommend to always look at the API doc at your local foreman, at http://yourforeman.example.com/apidoc, this has the benefit that the docs always match your running Foreman version and includes the API information for all your installed plugins.

3 Likes

:grimacing: hoo how did I miss the ansible doc :sweat_smile: it was there crystal and clear… Thanks @jeremylenz I managed to test with ansible and was successful, and I will try the with API later today.

1 Like

noted