Skip to content

Commit

Permalink
Simplify gce inventory plugin injector
Browse files Browse the repository at this point in the history
This consumes the change made in Ansible core
ansible/ansible#54407
which is in Ansible 2.8, allowing the plugin
injection logic to share the script logic and
to be simplified
  • Loading branch information
AlanCoding committed Apr 23, 2019
1 parent 864fef4 commit 11d246c
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 12 deletions.
2 changes: 0 additions & 2 deletions awx/main/models/credential/injectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ def gce(cred, env, private_data_dir):
project = cred.get_input('project', default='')
username = cred.get_input('username', default='')

env['GCE_EMAIL'] = username
env['GCE_PROJECT'] = project
json_cred = {
'type': 'service_account',
'private_key': cred.get_input('ssh_key_data', default=''),
Expand Down
16 changes: 8 additions & 8 deletions awx/main/models/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -1911,13 +1911,14 @@ def _get_shared_env(self, inventory_update, private_data_dir, private_data_files
# some sources may have no credential, specifically ec2
if credential is None:
return injected_env
if self.base_injector in ('managed', 'template'):
injected_env['INVENTORY_UPDATE_ID'] = str(inventory_update.pk) # so injector knows this is inventory
if self.base_injector == 'managed':
from awx.main.models.credential import injectors as builtin_injectors
cred_kind = inventory_update.source.replace('ec2', 'aws')
if cred_kind in dir(builtin_injectors):
getattr(builtin_injectors, cred_kind)(credential, injected_env, private_data_dir)
elif self.base_injector == 'template':
injected_env['INVENTORY_UPDATE_ID'] = str(inventory_update.pk) # so injector knows this is inventory
safe_env = injected_env.copy()
args = []
credential.credential_type.inject_credential(
Expand Down Expand Up @@ -2326,6 +2327,12 @@ class gce(PluginFileInjector):

def get_script_env(self, inventory_update, private_data_dir, private_data_files):
env = super(gce, self).get_script_env(inventory_update, private_data_dir, private_data_files)
cred = inventory_update.get_cloud_credential()
# these environment keys are unique to the script operation, and are not
# concepts in the modern inventory plugin or gce Ansible module
# email and project are redundant with the creds file
env['GCE_EMAIL'] = cred.get_input('username', default='')
env['GCE_PROJECT'] = cred.get_input('project', default='')
env['GCE_ZONE'] = inventory_update.source_regions if inventory_update.source_regions != 'all' else '' # noqa

# by default, the GCE inventory source caches results on disk for
Expand Down Expand Up @@ -2366,8 +2373,6 @@ def inventory_as_dict(self, inventory_update, private_data_dir):
credential = inventory_update.get_cloud_credential()

# auth related items
from awx.main.models.credential.injectors import gce as builtin_injector
ret['service_account_file'] = builtin_injector(credential, {}, private_data_dir)
ret['projects'] = [credential.get_input('project', default='')]
ret['auth_kind'] = "serviceaccount"

Expand Down Expand Up @@ -2413,11 +2418,6 @@ def inventory_as_dict(self, inventory_update, private_data_dir):
ret['zones'] = inventory_update.source_regions.split(',')
return ret

def get_plugin_env(self, inventory_update, private_data_dir, private_data_files):
# gce wants everything defined in inventory & cred files
# this explicitly turns off injection of environment variables
return {}


class vmware(PluginFileInjector):
# plugin_name = 'vmware_vm_inventory' # FIXME: implement me
Expand Down
3 changes: 2 additions & 1 deletion awx/main/tests/data/inventory/plugins/gce/env.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"ANSIBLE_TRANSFORM_INVALID_GROUP_CHARS": "never"
"ANSIBLE_TRANSFORM_INVALID_GROUP_CHARS": "never",
"GCE_CREDENTIALS_FILE_PATH": "{{ file_reference }}"
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ plugin: gcp_compute
projects:
- fooo
retrieve_image_info: true
service_account_file: {{ file_reference }}
use_contrib_script_compatible_sanitization: true
zones:
- us-east4-a
Expand Down

0 comments on commit 11d246c

Please sign in to comment.