Need help with puppetdb query from manifest using puppetdb_query (PQL)

827 views
Skip to first unread message

John Bishop

unread,
Feb 15, 2018, 5:11:10 PM2/15/18
to Puppet Users
Hello,

   I'm new to using PQL and i'm having a bit of difficulty.   I'm trying to return the ipaddress of any node where the value of three trusted facts (pp_application, pp_role and pp_environment) meets some criteria.

I have a query which will return only the nodes that i care about,  but I'm having a problem structuring the query to also return the top level ipaddress fact with the results.  Any help would be appreciated.  Thank you. 

$test_query = '["from", "facts",
                 ["and",
                   ["subquery", "fact_contents",
                   ["and",
                     ["~>", "path", ["trusted", "extensions", "pp_application"]],
                     ["=", "value", "someapp"]]],
                   ["subquery", "fact_contents",
                   ["and",
                     ["~>", "path", ["trusted", "extensions", "pp_role"]],
                     ["=", "value", "appserver"]]],
                   ["subquery", "fact_contents",
                   ["and",
                     ["~>", "path", ["trusted", "extensions", "pp_environment"]],
                     ["=", "value", "development"]]]]]]'

$test_results = puppetdb_query($test_query)

Notify { '*** query results ***\r':
  message => "data: ${test_results}",
}

Nick Lewis

unread,
Feb 15, 2018, 6:07:34 PM2/15/18
to puppet...@googlegroups.com
In PuppetDB, a "fact" is an entry with [certname, environment, name, value]. A subquery between "facts" and "fact_contents" means "find facts whose value matches this fact_contents query". In this case, that will return the "trusted" fact. You then want to lookup the corresponding "ipaddress" fact for matching nodes. Since you're really looking up the value of one fact using a query based on another fact, you want to use your existing query as a fact subquery.

["from", "facts",
  ["and",
    ["=", "name", "ipaddress"],
    ["subquery", "facts",
      ["and",
        ["subquery", "fact_contents",
          ["and",
            ["~>", "path", ["trusted", "extensions", "pp_application"]],
            ["=", "value", "someapp"]]],
        ["subquery", "fact_contents",
          ["and",
            ["~>", "path", ["trusted", "extensions", "pp_role"]],
            ["=", "value", "appserver"]]],
        ["subquery", "fact_contents",
          ["and",
            ["~>", "path", ["trusted", "extensions", "pp_environment"]],
            ["=", "value", "development"]]]]]]]

The facts subquery will restrict the outer facts query to return facts only for nodes that match the subquery.

However, there's a more straightforward way to achieve this using PQL rather than the AST query language you're using.

facts[certname, value] {
  name = "ipaddress" and
  certname in inventory[certname] {
    trusted.extensions.pp_application = 'someapp' and
    trusted.extensions.pp_role = 'appserver' and
    trusted.extensions.pp_environment = 'development'
  }
}

This query uses the inventory entity to find nodes with the three specific trusted extensions, and then looks up the ipaddress fact for each of those nodes and returns the node name and the value of the ipaddress fact.

I don't have an environment available with those particular trusted extensions in use, so I can't verify it's 100% correct, but it should at least be on the right track.

$test_results = puppetdb_query($test_query)

Notify { '*** query results ***\r':
  message => "data: ${test_results}",
}

--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/89e396e9-e4f0-44b1-bc71-efaf99829556%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

John Bishop

unread,
Feb 15, 2018, 6:31:21 PM2/15/18
to Puppet Users
Thank you very much for the great explanation.  :)
Reply all
Reply to author
Forward
0 new messages