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

K8s inventory plugin #34920

Merged
merged 1 commit into from Jan 20, 2018
Merged

Conversation

chouseknecht
Copy link
Contributor

@chouseknecht chouseknecht commented Jan 16, 2018

SUMMARY

Adding k8s and openshift inventory plugin

ISSUE TYPE
  • New Module Pull Request
COMPONENT NAME

lib/ansible/plugins/inventory/k8s.py
lib/ansible/plugins/inventory/openshift.py

ANSIBLE VERSION
ansible 2.5.0 (feature/k8s-inventory bec8ebe9fb) last updated 2018/01/16 05:21:05 (GMT -400)
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/Users/chouseknecht/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /Users/chouseknecht/projects/ansible/lib/ansible
  executable location = /Users/chouseknecht/projects/ansible/bin/ansible
  python version = 2.7.14 (default, Nov 14 2017, 23:24:24) [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.38)]

@ansibot ansibot added WIP This issue/PR is a work in progress. Nevertheless it was shared for getting input from peers. affects_2.5 This issue/PR affects Ansible v2.5 needs_triage Needs a first human triage before being processed. new_plugin This PR includes a new plugin. support:core This issue/PR relates to code supported by the Ansible Engineering Team. ci_verified Changes made in this PR are causing tests to fail. labels Jan 16, 2018
@sivel sivel removed the needs_triage Needs a first human triage before being processed. label Jan 17, 2018
@ansibot ansibot removed the ci_verified Changes made in this PR are causing tests to fail. label Jan 18, 2018
@ansible ansible deleted a comment from ansibot Jan 18, 2018
@chouseknecht chouseknecht changed the title WIP: K8s inventory plugin K8s inventory plugin Jan 18, 2018
@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. and removed WIP This issue/PR is a work in progress. Nevertheless it was shared for getting input from peers. labels Jan 18, 2018
@ansibot ansibot removed the ci_verified Changes made in this PR are causing tests to fail. label Jan 18, 2018
@chouseknecht
Copy link
Contributor Author

chouseknecht commented Jan 18, 2018

@maxamillion @flaper87 @samdoran @jcpowermac @fabianvf @rcarrillocruz

I think this is in a good place. Would appreciate any feedback.

I'm testing with the following ansible.cfg:

[defaults]
remote_tmp = /tmp

[inventory]
enable_plugins = k8s, openshift

I'm just using the default ~/.kube/config, and active context against a local minishift instance.

So my simple k8s.yml contains:

plugin: k8s
connections: []

And for openshift.yml, I have the following:

plugin: openshift
connections: []

Here's a test playbook. You'll need to changes hosts on the second and third plays to a group _pods that works for your environment.

- name: Test inventory
  hosts: localhost
  gather_facts: no

  tasks:
  - name: View the inventory
    debug:
      msg: '{{ groups }}'

- name: Test labels
  hosts: awx_pods
  gather_facts: no

  tasks:
  - name: Set labels fact
    set_fact:
      host_labels: '{{ labels }}'
    when: "'name_postgresql' in labels"

  - debug:
      var: host_labels
    when: "'name_postgresql' in labels"

  - name: Labels should have 4 values
    assert:
      that: "host_labels | count == 4"
    when: "'name_postgresql' in labels"

- name: Test container connection
  hosts: awx_pods
  gather_facts: no

  tasks:
  - raw: ls -l /var
    when: container_ready
    register: output

  - assert:
      that: "output.stdout_lines | count > 1"
    when: container_ready

Run the above using either plugin. So for openshift, you would do the following:

ansible-playbook  --inventory=openshift.yml test.yml

@ansible ansible deleted a comment from ansibot Jan 18, 2018
@ansibot ansibot added the ci_verified Changes made in this PR are causing tests to fail. label Jan 18, 2018
@ansible ansible deleted a comment from ansibot Jan 18, 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 Jan 18, 2018
@ansibot ansibot added needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. and removed needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels Jan 18, 2018
@chouseknecht
Copy link
Contributor Author

chouseknecht commented Jan 19, 2018

@smarterclayton

It adds labels to all the returned objects, to enable selecting by label. But just realized, I'm adding labels to the object as key_value strings. Wondering if they can be added as a dict(), and be more usable. Experimenting...

@chouseknecht
Copy link
Contributor Author

@smarterclayton

Pushed a change. Now all structured data is handled as such, and not as strings. For example, with labels, you can now do the following:

- name: Test labels
  hosts: awx_pods
  gather_facts: no

  tasks:
  - name: Show labels
    debug:
      var: labels
    when: "'name' in labels"

  - name: Show labels for a single container
    debug:
      var: labels
    when: labels.name == 'postgresql'

  - name: Check label count for a given container
    assert:
      that: 'labels | count == 4'
    when: labels.name == 'postgresql'

@ansibot ansibot added the needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. label Jan 19, 2018
@chouseknecht
Copy link
Contributor Author

Ansible Inventory plugin

@ansibot ansibot removed the needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. label Jan 19, 2018
@maxamillion
Copy link
Contributor

I think this is great 👍

@flaper87
Copy link
Contributor

LGTM 🥞

@fabianvf
Copy link
Contributor

/lgtm

@flaper87
Copy link
Contributor

shipit

@chouseknecht chouseknecht merged commit a0bb193 into ansible:devel Jan 20, 2018
@dagwieers dagwieers added the k8s label Feb 8, 2019
@ansible ansible locked and limited conversation to collaborators Apr 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.5 This issue/PR affects Ansible v2.5 k8s new_plugin This PR includes a new plugin. support:core This issue/PR relates to code supported by the Ansible Engineering Team.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants