On Thu, Feb 15, 2018 at 2:11 PM John Bishop <jbishop....@gmail.com> wrote:

> 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"]]]]]]'
>
>
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
<https://puppet.com/docs/puppetdb/5.1/api/query/v4/pql.html> rather than
the AST query language
<https://puppet.com/docs/puppetdb/5.1/api/query/v4/ast.html> 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+unsubscr...@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
> <https://groups.google.com/d/msgid/puppet-users/89e396e9-e4f0-44b1-bc71-efaf99829556%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/puppet-users/CANa5_qKnL22Wpwfdd6%3Dt%3Da%2BhzTHb8P_40fVnFLkfoUb%3DcOz_Yg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to