-
Notifications
You must be signed in to change notification settings - Fork 23.7k
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
Add parent group feature to keyed_groups #52045
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
minor_changes: | ||
- keyed_groups now has a 'parent_group' keyword that allows assigning all generated groups to the same parent group |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
# Copyright 2019 Alan Rominger <arominge@redhat.net> | ||
# | ||
# This file is part of Ansible | ||
# | ||
# Ansible is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# Ansible is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
import pytest | ||
|
||
from ansible.plugins.inventory.constructed import InventoryModule | ||
from ansible.inventory.data import InventoryData | ||
from ansible.template import Templar | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def inventory_module(): | ||
r = InventoryModule() | ||
r.inventory = InventoryData() | ||
r.templar = Templar(None) | ||
return r | ||
|
||
|
||
def test_group_by_value_only(inventory_module): | ||
inventory_module.inventory.add_host('foohost') | ||
inventory_module.inventory.set_variable('foohost', 'bar', 'my_group_name') | ||
host = inventory_module.inventory.get_host('foohost') | ||
keyed_groups = [ | ||
{ | ||
'prefix': '', | ||
'separator': '', | ||
'key': 'bar' | ||
} | ||
] | ||
inventory_module._add_host_to_keyed_groups( | ||
keyed_groups, host.vars, host.name, strict=False | ||
) | ||
assert 'my_group_name' in inventory_module.inventory.groups | ||
group = inventory_module.inventory.groups['my_group_name'] | ||
assert group.hosts == [host] | ||
|
||
|
||
def test_keyed_group_separator(inventory_module): | ||
inventory_module.inventory.add_host('farm') | ||
inventory_module.inventory.set_variable('farm', 'farmer', 'mcdonald') | ||
inventory_module.inventory.set_variable('farm', 'barn', {'cow': 'betsy'}) | ||
host = inventory_module.inventory.get_host('farm') | ||
keyed_groups = [ | ||
{ | ||
'prefix': 'farmer', | ||
'separator': '_old_', | ||
'key': 'farmer', | ||
'unsafe': True | ||
}, | ||
{ | ||
'separator': 'mmmmmmmmmm', | ||
'key': 'barn', | ||
'unsafe': True | ||
} | ||
] | ||
inventory_module._add_host_to_keyed_groups( | ||
keyed_groups, host.vars, host.name, strict=False | ||
) | ||
for group_name in ('farmer_old_mcdonald', 'mmmmmmmmmmcowmmmmmmmmmmbetsy'): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not testing use of "unsafe" separators anymore, but I still thought there was value in testing separators in general within the unit tests. It was tough coming up with examples of separators that make sense without using special characters. So this is what you get. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cow puns welcome! |
||
assert group_name in inventory_module.inventory.groups | ||
group = inventory_module.inventory.groups[group_name] | ||
assert group.hosts == [host] | ||
|
||
|
||
def test_keyed_parent_groups(inventory_module): | ||
inventory_module.inventory.add_host('web1') | ||
inventory_module.inventory.add_host('web2') | ||
inventory_module.inventory.set_variable('web1', 'region', 'japan') | ||
inventory_module.inventory.set_variable('web2', 'region', 'japan') | ||
host1 = inventory_module.inventory.get_host('web1') | ||
host2 = inventory_module.inventory.get_host('web2') | ||
keyed_groups = [ | ||
{ | ||
'prefix': 'region', | ||
'key': 'region', | ||
'parent_group': 'region_list' | ||
} | ||
] | ||
for host in [host1, host2]: | ||
inventory_module._add_host_to_keyed_groups( | ||
keyed_groups, host.vars, host.name, strict=False | ||
) | ||
assert 'region_japan' in inventory_module.inventory.groups | ||
assert 'region_list' in inventory_module.inventory.groups | ||
region_group = inventory_module.inventory.groups['region_japan'] | ||
all_regions = inventory_module.inventory.groups['region_list'] | ||
assert all_regions.child_groups == [region_group] | ||
assert region_group.hosts == [host1, host2] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
parent name also needs to be 'safe'