Project

General

Profile

Actions

Feature #19522

closed

REST call for facts

Added by Matteo Castellarin over 7 years ago. Updated over 7 years ago.

Status:
Feedback
Priority:
Normal
Assignee:
-
Category:
Facts
Target version:
-
Difficulty:
Triaged:
Fixed in Releases:
Found in Releases:

Description

Hi

PuppetDB provided a nice REST feature to return a list of all facts related to the machines matching a name.
This was achieved with something like:

curl -k -i -X GET https://<<PUPPET_DB_IP>>/api/pdb/query/v4 --data-urlencode "query=inventory  { facts.project.name = 'TEST-PROJECT' }" 

In Foreman we could not get this in one step. We needed to:

  • Get a list of the machines
  • Loop over each machine to get its facts

Would it be possible to have a feature like the one of PuppetDB (i.e. one single REST call providing a list of machines matching a REGEX, each one having the list of all its facts)?

Thank you

Actions #1

Updated by Dominic Cleal over 7 years ago

  • Category set to Facts
  • Status changed from New to Feedback

The GET /api/v2/fact_values API will return facts for all hosts: https://theforeman.org/api/1.14/apidoc/v2/fact_values/index.html

Actions #2

Updated by Anonymous over 7 years ago

  • Status changed from Feedback to Resolved

no reaction, closing

Actions #3

Updated by Matteo Castellarin over 7 years ago

Michael Moll wrote:

no reaction, closing

Hi

Sorry for the late answer. The Puppet facts seem to be not visible via the /api/fact_values endpoints.
Perhaps I am not using the call properly, but even with the Hammer CLI tool they do not appear.
If this is the case, it would be really nice to re-open the feature request :-)

Thank you

Actions #4

Updated by Dominic Cleal over 7 years ago

  • Status changed from Resolved to Feedback

Can you provide any more information? Are the facts visible under Monitor > Facts in the UI? If yes, are there any results in the API call?

If there are no facts stored at all, check your fact upload process is working and correctly configured - usually via the ENC script. See https://theforeman.org/manuals/1.15/index.html#3.5.5FactsandtheENC. If there are no facts uploaded successfully then the UI, Hammer and the UI can't display them. If you have configuration difficulties, the mailing list and IRC channel may be best for support: https://theforeman.org/support.html

Actions #5

Updated by Matteo Castellarin over 7 years ago

Hi Dominic

Thank you for your answer. We can see all the uploaded facts in the GUI by following:

Hosts --> All Hosts --> [click on whatever machine in the list] --> Facts

I can fetch them via the REST API with:

curl -k -u '<USERNAME>@<ORGANIZATION>.com:<PASSWORD>' https://<FOREMAN-URL>/api/hosts/<HOST_ID>/facts

However I can only specify one machine per time (i.e. <HOST-ID>). It seems it is not possible to fetch them in one goal.
NOTE - We use Puppet behind the scenes to store tha facts (PuppetDB), as an inventoring system for Foreman.

Thank you

Dominic Cleal wrote:

Can you provide any more information? Are the facts visible under Monitor > Facts in the UI? If yes, are there any results in the API call?

If there are no facts stored at all, check your fact upload process is working and correctly configured - usually via the ENC script. See https://theforeman.org/manuals/1.15/index.html#3.5.5FactsandtheENC. If there are no facts uploaded successfully then the UI, Hammer and the UI can't display them. If you have configuration difficulties, the mailing list and IRC channel may be best for support: https://theforeman.org/support.html

Actions #6

Updated by Dominic Cleal over 7 years ago

Did you try the API call in comment 1? Try GET /api/v2/fact_values.

Actions #7

Updated by Matteo Castellarin over 7 years ago

Dominic Cleal wrote:

Did you try the API call in comment 1? Try GET /api/v2/fact_values.

When calling the API with:

curl -k -i -u 'username@company.com' "https://foreman.company.com/api/v2/fact_values" 

The result looks like:

{
  "total": 0,
  "subtotal": 0,
  "page": 1,
  "per_page": 20,
  "search": "",
  "sort": {
    "by": null,
    "order": null
  },
  "results": {
    "machine-01.company.com": {
      "augeas": null
    },
    "machine-02.company.com": {
      "augeas": null
    },
    "machine-03.company.com": {
      "augeas": null
    }
  }
}

The fact values are not visible. Does this happen because we are using PuppetDB as an inventory for Foreman, maybe?

Actions #8

Updated by Dominic Cleal over 7 years ago

Assuming no custom facts, "augeas" is a hash of facts in Facter 3 and has no value itself, which is why it's listed as null.

You should see other facts with values, or look for a specific fact within the augeas tree, e.g. "augeas::version":

$ curl https://foreman.example.com/api/v2/fact_values?search=name=augeas::version
{
  "total": 336,
  "subtotal": 1,
  "page": 1,
  "per_page": 20,
  "search": "name=augeas::version",
  "sort": {
    "by": null,
    "order": null
  },
  "results": {"foreman.example.com":{"augeas::version":"1.4.0"}}
}
Actions #9

Updated by Matteo Castellarin over 7 years ago

Dominic Cleal wrote:

Assuming no custom facts, "augeas" is a hash of facts in Facter 3 and has no value itself, which is why it's listed as null.

You should see other facts with values, or look for a specific fact within the augeas tree, e.g. "augeas::version":

[...]

This is a very nice solution, I didn't know about augeas. Thank you!

However this shows only facts that are known a-priori. If it could be possible to just list all the available facts of a machine, then this feature request can definitely be closed (I couldn't figure out how, though).

Actions #10

Updated by Dominic Cleal over 7 years ago

To list facts for a specific machine then use the API call you mentioned earlier, /api/v2/hosts/ID/fact_values.

To list every fact, use /api/v2/fact_values. The search I demonstrated isn't necessary, it was only to show you that there are also non-null facts. Every fact will be listed in this call, it will just be paginated as usual.

Actions #11

Updated by Matteo Castellarin over 7 years ago

Dominic Cleal wrote:

To list facts for a specific machine then use the API call you mentioned earlier, /api/v2/hosts/ID/fact_values.

To list every fact, use /api/v2/fact_values. The search I demonstrated isn't necessary, it was only to show you that there are also non-null facts. Every fact will be listed in this call, it will just be paginated as usual.

Can augeas make it possible to have a list of all machines (i.e. matching a regex) with the full list of the facts?

More specifically: the only way I could achieve this was by listing the VMs (matching a regex) and then perform a REST call per-each machine, which ends up in being extremely heavy (while, if performed server-side would make it so much faster).

Actions #12

Updated by Dominic Cleal over 7 years ago

The search capability is the same as the UI facts page, use the auto-complete there to see what's available. You can search by "fact" (the name) or "host" (hostname) for example, e.g. host ~ example.

Actions #13

Updated by Matteo Castellarin over 7 years ago

I guess this means the feature request of having the API returning a list of machines, each one containing all of its facts is rejected.

I'd like anyway to expose the idea to provide some usage-examples of the API + Augeas: currently the Examples page (http://projects.theforeman.org/projects/foreman/wiki/Examples) is pretty poor and does not really show the power of the tool.

Actions #14

Updated by Dominic Cleal over 7 years ago

Matteo Castellarin wrote:

I guess this means the feature request of having the API returning a list of machines, each one containing all of its facts is rejected.

The /fact_values API does just this already, it returns a hash of host names containing a hash of their facts (see output in comment 7).

Actions

Also available in: Atom PDF