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

Optionally get service account file path from env var #54407

Merged
merged 1 commit into from Apr 4, 2019

Conversation

AlanCoding
Copy link
Member

SUMMARY

Addresses #54406

ISSUE TYPE
  • Feature Pull Request
COMPONENT NAME

gcp_compute inventory plugin

ADDITIONAL INFORMATION

Tested with

GCE_CREDENTIALS_FILE_PATH="/Users/alancoding/Documents/repos/ansible-inventory-file-examples/private/gce/creds.json" ansible-inventory -i min.gcp_compute.yml --list --export

Where min.gcp_compute.yml has

auth_kind: serviceaccount
filters: null
strict: true
plugin: gcp_compute
projects:
- alans_project
zones:
  - us-east1-d

This will allow users to check their inventory config into source control.

@ansibot
Copy link
Contributor

ansibot commented Mar 26, 2019

@ansibot ansibot added affects_2.8 This issue/PR affects Ansible v2.8 community_review In order to be merged, this PR must follow the community review workflow. feature This issue/PR relates to a feature request. inventory Inventory category needs_triage Needs a first human triage before being processed. support:community This issue/PR relates to code supported by the Ansible community. labels Mar 26, 2019
@@ -335,6 +338,12 @@ def parse(self, inventory, loader, path, cache=True):
config_data = {}
config_data = self._read_config_data(path)

# this may optionally be read from environment variable
if 'service_account_file' not in config_data:
path = self.get_option('service_account_file')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure how you expect get_option to work since it pulls from config_data?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing in _read_config_data indicated to me that it reads from environment variables

https://github.com/ansible/ansible/blob/04ce72e80708bcc8139a9d7546aa088aefb0f492/lib/ansible/plugins/inventory/__init__.py#L206-L235

I tested this by putting breakpoints in, and confirmed, yes, that get_option works where the entry is not present in config_data. Not being able to explain it doesn't change the fact that I can confirm that it works this way.

Even the syntax in the BaseInventoryPlugin class seems to confirm that this syntax pattern is common:

if 'cache' in self._options and self.get_option('cache'):

I don't know if this is for the exact same reason, but maybe.

Now, one thing I didn't check, but maybe I should, is to compare self._options versus config_data in the locals here.

Copy link
Member

@bcoca bcoca Mar 26, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

config_data is not supposed to read env vars, get_option does, you should not access _options directly

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see #54426

@ansibot ansibot removed the needs_triage Needs a first human triage before being processed. label Mar 26, 2019
Copy link
Member

@bcoca bcoca left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so there is a basic problem with this plugin, it is doing it's own validation with incorrect assumptions

I would remove the existing validations like if 'zone' in config_data and just add required: true to the config definition.

@ansibot ansibot added needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. and removed community_review In order to be merged, this PR must follow the community review workflow. labels Mar 26, 2019
@@ -335,6 +338,12 @@ def parse(self, inventory, loader, path, cache=True):
config_data = {}
config_data = self._read_config_data(path)

# this may optionally be read from environment variable
if 'service_account_file' not in config_data:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this conditional is not needed, just use get_option since if read_config_data already consumes the options in the config file

@AlanCoding
Copy link
Member Author

It actually wasn't obvious to me from reading the source code that zones should be required. So I just had to try it without giving zones and see, it gave:

[WARNING]: * Failed to parse /Users/alancoding/Documents/repos/ansible-inventory-file-examples/private/gce/min.gcp_compute.yml with auto plugin: [{u'domain': u'global',
u'message': u"Invalid value for field 'zone': 'a'. Unknown zone.", u'reason': u'invalid'}]

So zones should probably be required.

tested the changes for zones, works if I give zones, if I don't give zones:

[WARNING]: * Failed to parse /Users/alancoding/Documents/repos/ansible-inventory-file-examples/private/gce/min.gcp_compute.yml with auto plugin: No setting was provided for
required configuration plugin_type: inventory plugin: gcp_compute setting: zones

It is obvious from the prior code that projects should be required, so I made that required in the docs.

I already dealt with filters in a prior issue, but this allows it to be handled in the standard way.

raise AnsibleParserError("Zones must be a list in GCP inventory YAML files")
zones = self.get_option('zones')
config_data['zones'] = zones
if not isinstance(zones, list):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also not needed, add type: list to configuration definition, nothing should be added back to config_data

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also not needed, add type: list to configuration definition

I can do that

nothing should be added back to config_data

I can't do that. The entire solution here pivots on putting "service_account_file" in config data, because I don't know how it is used for the request auth. I would be guessing at what you want this to look like. I need more detail.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nothing should use config_data, its only meant for 'extras not covered by get_option', but that is a preexisting problem here

@AlanCoding
Copy link
Member Author

I tried this:

diff --git a/lib/ansible/plugins/inventory/gcp_compute.py b/lib/ansible/plugins/inventory/gcp_compute.py
index 361f56233e..e6b1208b2f 100644
--- a/lib/ansible/plugins/inventory/gcp_compute.py
+++ b/lib/ansible/plugins/inventory/gcp_compute.py
@@ -26,9 +26,11 @@ DOCUMENTATION = '''
           description: A list of regions in which to describe GCE instances.
           default: all zones available to a given project
           required: True
+          type: list
         projects:
           description: A list of projects in which to describe GCE instances.
           required: True
+          type: list
         filters:
           description: >
             A list of filter value pairs. Available filters are listed here
@@ -344,20 +346,13 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
 
         # get user specifications
         zones = self.get_option('zones')
-        config_data['zones'] = zones
-        if not isinstance(zones, list):
-            raise AnsibleParserError("Zones must be a list in GCP inventory YAML files")
-
-        config_data['filters'] = self.get_option('filters')
-
-        service_account_path = self.get_option('service_account_file')
-        if service_account_path:
-            config_data['service_account_file'] = service_account_path
-
         projects = self.get_option('projects')
+        print('projects')
+        print(projects)
+        config_data['zones'] = zones
         config_data['projects'] = projects
-        if not isinstance(projects, list):
-            raise AnsibleParserError("Projects must be a list in GCP inventory YAML files")
+        config_data['filters'] = self.get_option('filters')
+        config_data['service_account_file'] = self.get_option('service_account_file')

This isn't working, I can put in dictionary, and get_option returns a dictionary.

@AlanCoding
Copy link
Member Author

rebased PR to use new standardized system

@AlanCoding
Copy link
Member Author

Related #54532

@AlanCoding
Copy link
Member Author

What's the status of this?

@ansibot ansibot added community_review In order to be merged, this PR must follow the community review workflow. and removed needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels Apr 4, 2019
@bcoca bcoca merged commit aa3f010 into ansible:devel Apr 4, 2019
AlanCoding added a commit to AlanCoding/awx that referenced this pull request Apr 23, 2019
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
AlanCoding added a commit to AlanCoding/awx that referenced this pull request Apr 23, 2019
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
AlanCoding added a commit to AlanCoding/awx that referenced this pull request Apr 23, 2019
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
@ansible ansible locked and limited conversation to collaborators Jul 25, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.8 This issue/PR affects Ansible v2.8 community_review In order to be merged, this PR must follow the community review workflow. feature This issue/PR relates to a feature request. inventory Inventory category small_patch support:community This issue/PR relates to code supported by the Ansible community.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants