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

Allow filtering of instances by tags in GCE dynamic inventory #35770

Merged
merged 3 commits into from
Mar 14, 2018
Merged

Allow filtering of instances by tags in GCE dynamic inventory #35770

merged 3 commits into from
Mar 14, 2018

Conversation

pschiffe
Copy link

@pschiffe pschiffe commented Feb 6, 2018

SUMMARY

This enables to exclude instances in the project not relevant to the play, what benefits performance and can help to avoid mistakes.

ISSUE TYPE
  • Feature Pull Request
COMPONENT NAME

GCE dynamic inventory

ANSIBLE VERSION
ansible 2.4.2.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/pschiffe/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.14 (default, Jan 17 2018, 14:28:32) [GCC 7.2.1 20170915 (Red Hat 7.2.1-2)]

/cc @smarterclayton

@ansibot ansibot added c:inventory/contrib_script feature_pull_request inventory Inventory category needs_triage Needs a first human triage before being processed. new_contributor This PR is the first contribution by a new community member. support:community This issue/PR relates to code supported by the Ansible community. labels Feb 6, 2018
@smarterclayton
Copy link

We’ve found value in this in automation where many inventories are in the same GCP project. The filter prevents accidental typos from blowing out every host within the project. Effectively a safety barrier for automation.

@pschiffe
Copy link
Author

pschiffe commented Feb 8, 2018

@@ -247,6 +247,20 @@ def get_config(self):
if states:
self.instance_states = states.split(',')

# Set the instance_tags filter
self.instance_tags = []
if config.has_option('gce', 'instance_tags'):
Copy link
Contributor

Choose a reason for hiding this comment

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

Rather than using conditionals for each option, which can be a bit expensive, set a default value when the config object is created. Then you can just call config.get() and it will use the default if the key isn't in the file, or it will pull the value from the key in the file.

@samdoran samdoran removed the needs_triage Needs a first human triage before being processed. label Feb 8, 2018
@samdoran samdoran self-requested a review February 8, 2018 16:11
@ansibot
Copy link
Contributor

ansibot commented Feb 9, 2018

The test ansible-test sanity --test pep8 [explain] failed with 1 error:

contrib/inventory/gce.py:257:21: E128 continuation line under-indented for visual indent

click here for bot help

@ansibot ansibot added ci_verified Changes made in this PR are causing tests to fail. needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels Feb 9, 2018
@ansibot ansibot removed ci_verified Changes made in this PR are causing tests to fail. needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels Feb 9, 2018
@ansibot ansibot added the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Feb 17, 2018
@ansibot ansibot added feature This issue/PR relates to a feature request. performance and removed feature_pull_request labels Mar 2, 2018
@samdoran
Copy link
Contributor

rebuild_merge

@ansibot ansibot removed the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Mar 14, 2018
@ansibot ansibot merged commit 8c992cc into ansible:devel Mar 14, 2018
@samdoran
Copy link
Contributor

@pschiffe There is a new inventory plugin system in Ansible. In the near future, we will stop accepting new features to scripts in favor of using inventory plugins. While we don't have docs yet, I can point you at some good examples of existing inventory plugins if you're interested in porting this to a plugin.

openstack.py
virtualbox.py

Thanks for your contribution!

@pschiffe pschiffe deleted the gce-tags branch March 14, 2018 21:15
@ansible ansible locked and limited conversation to collaborators Apr 27, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
c:inventory/contrib_script feature This issue/PR relates to a feature request. inventory Inventory category new_contributor This PR is the first contribution by a new community member. performance 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

4 participants