Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to use Puppet DSL expression (lambda) to filter Bolt targets from CLI #3359

Open
jay7x opened this issue Nov 18, 2024 · 2 comments
Open
Labels
Feature New features and improvements.

Comments

@jay7x
Copy link
Contributor

jay7x commented Nov 18, 2024

Use Case

Sometimes I'd like to run a command or task on a subset of my inventory, that is based on a Target facts or variables. Currently there is no way to do so. You can read this as "I miss mco filters" :)

Describe the Solution You Would Like

We can add the -F, --filter <filter_expression> CLI option, where <filter_expression> is a Puppet DSL expression to be evaluated for every Target object we got from --query or --targets CLI option.

Actually, the filtering logic is like the following:

$targets.filter |$target| {
  #<evaluate filter_expression here>
}

That can open some security issues, because anything can be evaluated there.. though being able to evaluate anything is the main idea actually. From other side, it should be possible to expose only $target variable scope when filtering. Nobody prevents user from using generate() inside though.

Few examples:

# Run `racadm ...` on Dell servers only (based on `bmc_type` target variable set in inventory)
bolt command run 'racadm ...' -t servers --filter '$target.vars["bmc_type"] == "idrac"'

# Run `apt update` on Debian servers only (based on facts)
# This is sub-optimal, because "facts" plan should be called once for all targets ideally
bolt command run 'apt update' -t all --filter 'run_plan("facts", $target); $target.facts["os.family"] == "Debian"'

WRT facts query above, it might be profitable to add another CLI option (--filter-facts?) to pre-collect facts from targets specified first, then do the filtering.

Describe Alternatives You've Considered

It's not that critical for plans, because there I can filter targets easily. But I don't want to create plans for every ad-hoc one-liner. Also I cannot create a generic plan to filter targets, because there is no way to evaluate a String as a Puppet DSL expression (or I'm not aware of it).

@jay7x jay7x added the Feature New features and improvements. label Nov 18, 2024
@hlindberg
Copy link
Contributor

because there is no way to evaluate a String as a Puppet DSL expression (or I'm not aware of it).

Actually, there is an eval function in my module named tahu at github if you want to be able to evaluate a string of puppet DSL.

@jay7x
Copy link
Contributor Author

jay7x commented Nov 21, 2024

because there is no way to evaluate a String as a Puppet DSL expression (or I'm not aware of it).

Actually, there is an eval function in my module named tahu at github if you want to be able to evaluate a string of puppet DSL.

Ah! Then I have some alternatives to consider at least :) Thank you @hlindberg!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature New features and improvements.
Projects
None yet
Development

No branches or pull requests

2 participants