Query rest api for YAML parameters

Is there some way to query the rest api to pull down the same YAML parameters that is displayed when editing a host in the GUI and clicking on the YAML button? I will even be happy with a hammer command. Anything really that I can run from a script on the command line.

You should be able to get these via /api/hosts/:host_id/parameters

Ok… Only problem how do you get the host_id? Does not seem to be the same as the id from what I can tell.

you can drop the full hostname there, or the database id of the host (as show in hammer host list).

Here are the results of using both hostname and the ID from hammer host list strangly both report the same thing. But the result is not what I see in the GUI at all.

[root@fm ansible]# curl -u admin:password -H "Content-Type:application/json" \
>  -H "Accept:application/json" -k -X GET "https://fm.domain.com/api/hosts/581/parameters"
{
  "total": 1,
  "subtotal": 1,
  "page": 1,
  "per_page": 20,
  "search": null,
  "sort": {
    "by": null,
    "order": null
  },
  "results": [{"priority":70,"created_at":"2022-03-29 00:47:45 UTC","updated_at":"2022-03-29 00:47:45 UTC","id":212,"name":"tds_tm_app_fqdn","parameter_type":"string","value":"cloudreach-hostname.domain2.net"}]
}

[root@fm ansible]# curl -u admin:password -H "Content-Type:application/json"  -H "Accept:application/json" -k -X GET "https://fm.domain.com/api/hosts/somehostname-host01.tm.domain.net/parameters"
{
  "total": 1,
  "subtotal": 1,
  "page": 1,
  "per_page": 20,
  "search": null,
  "sort": {
    "by": null,
    "order": null
  },
  "results": [{"priority":70,"created_at":"2022-03-29 00:47:45 UTC","updated_at":"2022-03-29 00:47:45 UTC","id":212,"name":"tds_tm_app_fqdn","parameter_type":"string","value":"cloudreach-hostname.domain2.net"}]
}

Can you post a screenshot from the GUI? I might have misunderstood your original question.

Sorry had to strip a lot out …

Sure, no worries.

So this is the “ENC” view, as fed to Puppet, it should contain things you get via the /parameters API I mentioned earlier, but also other data. There is no other API (besides this one, technically) that would get you exactly the same data, as this is a composition of multiple data sources.

What are you trying to achieve with this?

So we ran into an issue with ansible-runner where its not running ansible anymore via foreman. As a work around I can manually take the parameters page (remove the second line and push everything over 2 space) then feed this into our ansible and all runs fine.

This gets us past our issues until I can build a new Foreman env in AWS for our hosts. At the same time we have been exploring other ways to run ansible and what benifits or not they provide. But regardless of how we opt to do ansible we would like to keep Foreman as the CMDB and since we run Katello on Foreman the we will be keeping this as the patch manager.

If I can find a way to get the same list of parameters programaticly out of foreman as I can via the web then I can write a simple script to dump that out for ansible to read.

Does this make sense?

This sounds like a bug we should try to address and fix? (Regardless of whether you’d end up using the feature or not).

If you want to run Ansible outside of Foreman, but use data Foreman knows about your systems, we offer an Inventory plugin that should do exactly that. I think this is the real path you should be aproaching, instead of writing something own. Not saying this will immediately solve all your needs, but it’d be a better starting point.

We are already using that.

And which data are you missing from the one the inventory gathers from you?

After much googling and guessing I it looks like the equivilent to that GUI page is this:

hammer --output yaml host enc-dump --name <hostname>

You can also pull that page directly via curl, no need for hammer.

I am having a similar problem, and I tried to pull that page directly with curl, and got a 500 error. I guess I might be able to do some screen scraping, pretending to be a browser, to login and get a cookie etc. But I can’t just do

curl -u user:pass https://foreman.example.com/hosts/host1.example.com/externalNodes?name=host1.example.com

which is the url that I get if I use the web ui to query the yaml for a host.

I was able to get it via hammer. But on vacation so cannot look up the
command.

Which Foreman version are you running? Which version of the foreman_puppet plugin?

You need at least to also pass &format=yml to the URL so that it actually tries to render YAML.

But I also think that this endpoint wants other authentication by default :frowning:

I am sure you can pull the data via the curl. However, the solution I
found was to run:
hammer --output yaml host enc-dump --name ${HOST}
This I can send to a file and then pass in to an ansible run. The only
changes to the file is at the very top and I do that with this:
echo “Sanitizing yaml output…”
sed -i ‘1,2d’ ${TMPDir}/${HOST}.yml
sed -i ‘s/^ //’ ${TMPDir}/${HOST}.yml
sed -i ‘1 i—’ ${TMPDir}/${HOST}.yml

Enjoy.