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

[demo] API side changes to allow running jobs against multiple inventories #13214

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions awx/main/migrations/0173_auto_20221118_1421.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Generated by Django 3.2.13 on 2022-11-18 14:21

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('main', '0172_prevent_instance_fallback'),
]

operations = [
migrations.AddField(
model_name='inventory',
name='source_inventories',
field=models.ManyToManyField(
blank=True,
help_text='Only valid for multiple inventories, this links to the inventories that will be used.',
related_name='destination_inventories',
to='main.Inventory',
),
),
migrations.AlterField(
model_name='inventory',
name='kind',
field=models.CharField(
blank=True,
choices=[
('', 'Hosts have a direct link to this inventory.'),
('smart', 'Hosts for inventory generated using the host_filter property.'),
('multiple', 'Contains links to several other inventories to run automation against them all at the same time.'),
],
default='',
help_text='Kind of inventory being represented.',
max_length=32,
),
),
]
7 changes: 7 additions & 0 deletions awx/main/models/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class Inventory(CommonModelNameNotUnique, ResourceMixin, RelatedJobsMixin):
KIND_CHOICES = [
('', _('Hosts have a direct link to this inventory.')),
('smart', _('Hosts for inventory generated using the host_filter property.')),
('multiple', _('Contains links to several other inventories to run automation against them all at the same time.')),
]

class Meta:
Expand Down Expand Up @@ -139,6 +140,12 @@ class Meta:
default=None,
help_text=_('Filter that will be applied to the hosts of this inventory.'),
)
source_inventories = models.ManyToManyField(
'Inventory',
blank=True,
related_name='destination_inventories',
help_text=_('Only valid for multiple inventories, this links to the inventories that will be used.'),
)
instance_groups = OrderedManyToManyField(
'InstanceGroup',
blank=True,
Expand Down
22 changes: 16 additions & 6 deletions awx/main/tasks/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,17 +315,27 @@ def build_env(self, instance, private_data_dir, private_data_files=None):

return env

def _write_inventory_file(self, inventory, private_data_dir, file_name, script_params):
script_data = inventory.get_script_data(**script_params)
for hostname, hv in script_data.get('_meta', {}).get('hostvars', {}).items():
# maintain a list of host_name --> host_id
# so we can associate emitted events to Host objects
self.runner_callback.host_map[hostname] = hv.pop('remote_tower_id', '')
file_content = '#! /usr/bin/env python3\n# -*- coding: utf-8 -*-\nprint(%r)\n' % json.dumps(script_data)
return self.write_private_data_file(private_data_dir, file_name, file_content, sub_dir='inventory', file_permissions=0o700)

def build_inventory(self, instance, private_data_dir):
script_params = dict(hostvars=True, towervars=True)
if hasattr(instance, 'job_slice_number'):
script_params['slice_number'] = instance.job_slice_number
script_params['slice_count'] = instance.job_slice_count
script_data = instance.inventory.get_script_data(**script_params)
# maintain a list of host_name --> host_id
# so we can associate emitted events to Host objects
self.runner_callback.host_map = {hostname: hv.pop('remote_tower_id', '') for hostname, hv in script_data.get('_meta', {}).get('hostvars', {}).items()}
file_content = '#! /usr/bin/env python3\n# -*- coding: utf-8 -*-\nprint(%r)\n' % json.dumps(script_data)
return self.write_private_data_file(private_data_dir, 'hosts', file_content, sub_dir='inventory', file_permissions=0o700)
self.runner_callback.host_map = {}
if instance.inventory.kind == 'multiple':
ret = []
for source_inventory in instance.inventory.source_inventories.all():
ret.append(self._write_inventory_file(source_inventory, private_data_dir, f'hosts_{source_inventory.id}', script_params))
return ret
return self._write_inventory_file(instance.inventory, private_data_dir, 'hosts', script_params)

def build_args(self, instance, private_data_dir, passwords):
raise NotImplementedError
Expand Down