Skip to content

Commit

Permalink
(puppetlabsGH-2818) Support globbing group names
Browse files Browse the repository at this point in the history
This updates the inventory to support globbing group names in addition
to target names and aliases.

!feature

* **Support globbing group names**
  ([puppetlabs#2818](puppetlabs#2818))

  Bolt now supports selecting group names with glob patterns when
  selecting targets.
  • Loading branch information
beechtom committed Jul 27, 2022
1 parent 2b6ba07 commit fb55ed0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
19 changes: 14 additions & 5 deletions lib/bolt/inventory/inventory.rb
Expand Up @@ -125,16 +125,25 @@ def match_wildcard?(wildcard, target_name, ext_glob: false)
end

# If target is a group name, expand it to the members of that group.
# Else match against targets in inventory by name or alias.
# Else match against groups and targets in inventory by name or alias.
# If a wildcard string, error if no matches are found.
# Else fall back to [target] if no matches are found.
def resolve_name(target, ext_glob: false)
if (group = group_lookup[target])
group.all_targets
else
# Try to wildcard match targets in inventory
# Ignore case because hostnames are generally case-insensitive
targets = groups.all_targets.select { |targ| match_wildcard?(target, targ, ext_glob: ext_glob) }
targets = []

# Find groups that match the glob
group_lookup.each do |name, group|
next unless match_wildcard?(target, name, ext_glob: ext_glob)
targets += group.all_targets.to_a
end

# Find target names that match the glob
targets += groups.all_targets.select { |targ| match_wildcard?(target, targ, ext_glob: ext_glob) }

# Find target aliases that match the glob
targets += groups.target_aliases
.select { |tgt_alias, _| match_wildcard?(target, tgt_alias, ext_glob: ext_glob) }
.values
Expand All @@ -143,7 +152,7 @@ def resolve_name(target, ext_glob: false)
raise(WildcardError, target) if target.include?('*')
[target]
else
targets
targets.uniq
end
end
end
Expand Down
5 changes: 5 additions & 0 deletions spec/unit/inventory/inventory_spec.rb
Expand Up @@ -307,6 +307,11 @@ def get_target(inventory, name, alia = nil)
target9])
end

it 'globs against group names' do
targets = inventory.get_targets('group*')
expect(targets.map(&:name).sort).to eq(%w[ssh://target8 target4 target5 target6 target7 target9])
end

it 'should fail if wildcard selector matches nothing' do
expect {
inventory.get_targets('*target')
Expand Down

0 comments on commit fb55ed0

Please sign in to comment.