Foreman assign mutiple hosts to a config group

foremanpuppet

On foreman 1.6.0 is it possible to easily assign multiple hosts to a config group (Under the Configure->Puppet->Config Groups menu)?

I've had a pretty good look around the web interface and hammer cli but can't seem to find anything.

Best Answer

There isn't a GUI way of doing it, though you could assign config groups to a host group and assign multiple hosts to that via the host list (Hosts > All hosts, tickboxes, Select Action > Change Group).

You're right in that we're missing config group support in the CLI too, which surprised me. I've filed #7520 to fix that.

Probably the best way for now is via the REST API. Here's an example to update just the one host, but obviously you can put this in a simple shell loop and do much more with it.

(I like to pipe the curl output through json_reformat, very handy if you have it installed.)

First you may want to get a list of config groups, or do it via the GUI:

$ curl -k -u admin:password https://localhost/api/v2/config_groups

Now you can fetch an individual host and see which config groups are already assigned:

$ curl -k -u admin:password https://localhost/api/v2/hosts/foreman.example.com
...
"config_groups": [
]

So nothing's there right now.

Here I update the host's associated config groups with an array of names ("test" in this instance):

$ curl -k -u admin:password -H 'Content-Type: application/json' -d '{"host":{"config_group_names":["test"]}}' -X PUT https://localhost/api/v2/hosts/foreman.example.com
$ curl -k -u admin:password https://localhost/api/v2/hosts/foreman.example.com
...
"config_groups": [
    {
        "id": 1,
        "name": "test",
        "created_at": "2014-09-18T10:34:58Z",
        "updated_at": "2014-09-18T10:52:39Z",
        "puppetclasses": [

        ]
    }
]

You can also use IDs:

{"host":{"config_group_ids":[2, 3, 5, 7]}}

So putting that together you could do something like:

for h in a.example.com b.example.com c.example.com; do
  curl -k -u admin:password -H 'Content-Type: application/json' -X PUT \
    -d '{"host":{"config_group_names":["Base", "Web server"]}}' \
    https://localhost/api/v2/hosts/${h}
done
Related Topic