# Inventory from kubernetes?

**URL:** <https://forum.ansible.com/t/inventory-from-kubernetes/29251>\
**Category:** AWX Project\
**Tags:** awx, kubernetes\
**Created:** [December 12, 2018, 9:29pm UTC](https://forum.ansible.com/t/inventory-from-kubernetes/29251 "2018-12-12T21:29:58Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![kurrent93](https://avatars.discourse-cdn.com/v4/letter/k/a4c791/32.png) [@kurrent93](https://forum.ansible.com/u/kurrent93)\
**Post date:** [December 12, 2018, 9:29pm UTC](https://forum.ansible.com/t/inventory-from-kubernetes/29251/1 "2018-12-12T21:29:58Z")

</div>

Hello

Im new to awx - and am wondering if it is possible to have a dynamic inventory where the hosts are containers from kubernetes?

Thanks

---

<div class="post-metadata">

**Author:** ![AlanCoding](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/alancoding/32/301_2.png) [@AlanCoding](https://forum.ansible.com/u/AlanCoding)\
**Post date:** [December 13, 2018, 11:38am UTC](https://forum.ansible.com/t/inventory-from-kubernetes/29251/2 "2018-12-13T11:38:37Z")

</div>

Oh, you know what, I forgot an important details. If you’re using any decently recent version of Ansible, these Kubernetes plugins are already shipped with Ansible. All you need to do is put in the plugin form of the file into your source control.

[https://docs.ansible.com/ansible/2.6/plugins/inventory/openshift.html](https://docs.ansible.com/ansible/2.6/plugins/inventory/openshift.html)

Actually, I had an open question of what the best way to encrypt the token for OpenShift is doing this. I don’t think the vault tricks with with the inventory plugin files.

---

<div class="post-metadata">

**Author:** ![Anton\_Hughes](https://avatars.discourse-cdn.com/v4/letter/a/b5ac83/32.png) [@Anton\_Hughes](https://forum.ansible.com/u/Anton_Hughes)\
**Post date:** [December 13, 2018, 9:33pm UTC](https://forum.ansible.com/t/inventory-from-kubernetes/29251/3 "2018-12-13T21:33:28Z")

</div>

Thanks Alan

This looks very promising.

I’m missing a piece of the puzzle though.

I see that, in order to enable the k8s plugin I need to add it to the ansible.cfg file. So I have the following in a local ansible.cfg file:

[inventory]  
enable\_plugins = k8s

For the plugin settings, as specified by [https://docs.ansible.com/ansible/2.5/plugins/inventory/k8s.html](https://docs.ansible.com/ansible/2.5/plugins/inventory/k8s.html), I have the following:

A file named k8s.yaml, with the following contents:

```auto
plugin: k8s
connections:
    host: [https://192.168.64.4:8443](https://192.168.64.4:8443)
    token: xxxxxxxxxxxxxxxx
    ssl_verify: false

```

So, my question is, assuming the plugin is enabled, and the settings are correct, how do I write an inventory file - or specify in a playbook - to use this plugin?

I have the following playbook. I’ve set the hosts to default, as I have a k8s namespace called default. However, when I run this it doesnt work

---

<div class="post-metadata">

**Author:** ![AlanCoding](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/alancoding/32/301_2.png) [@AlanCoding](https://forum.ansible.com/u/AlanCoding)\
**Post date:** [December 14, 2018, 12:26pm UTC](https://forum.ansible.com/t/inventory-from-kubernetes/29251/4 "2018-12-14T12:26:56Z")

</div>

The main thing you need to do is create an inventory source for an inventory which sources from a project. For this source, provide the relative location for k8s.yaml inside of your repo. Then click the button to sync that source, and after its finished you should be able to see logs from the inventory update.

You don’t actually need to modify ansible.cfg here. Ansible tries a series of standard plugins by default. One of those is a router for all other officially shipped plugins, that plugin sees “plugin: k8s” inside your file (with that file name) and then sends it to the k8s plugin.

I still anticipate further pain points after you’ve done this. See:

[https://github.com/ansible/ansible/pull/49901](https://github.com/ansible/ansible/pull/49901)

In my own personal testing, I’ve been having authentication problems when counting on the provided parameters to be used. I suspect that it may have to be used via providing a kube config file, and pointing the inventory file to that file.

---

<div class="post-metadata">

**Author:** ![kurrent93](https://avatars.discourse-cdn.com/v4/letter/k/a4c791/32.png) [@kurrent93](https://forum.ansible.com/u/kurrent93)\
**Post date:** [December 16, 2018, 9:04pm UTC](https://forum.ansible.com/t/inventory-from-kubernetes/29251/5 "2018-12-16T21:04:28Z")

</div>

Thanks Alan

This does make it a bit clearer.

However, I’m unable to select the k8s.yaml file in the source editor window.

I have the k8s.yaml in git:

 ![Selection_012.png](https://us1.discourse-cdn.com/ansible/original/2X/8/8c2ddea8637ba90598df5baf94c69df2c99cd084.png)

But I’m unable to select this in the Awx:

 ![Selection_013.png](https://us1.discourse-cdn.com/ansible/original/2X/d/dffdf052aaec389cb352867829e467c0a82b7b57.png)

---

<div class="post-metadata">

**Author:** ![kurrent93](https://avatars.discourse-cdn.com/v4/letter/k/a4c791/32.png) [@kurrent93](https://forum.ansible.com/u/kurrent93)\
**Post date:** [December 16, 2018, 9:23pm UTC](https://forum.ansible.com/t/inventory-from-kubernetes/29251/6 "2018-12-16T21:23:52Z")

</div>

So it looks like the problem is the awx docker image doesnt have the openshift python module installed:

`

2018-12-16 21:05:43,544 INFO awx.main.commands.inventory\_import Updating inventory 2: dev-rancher

2  
2018-12-16 21:05:43,557 INFO awx.main.commands.inventory\_import Reading Ansible inventory source: /var/lib/awx/projects/\_6\_\_k8s\_inventory

3  
Traceback (most recent call last):

4  
File “/usr/bin/awx-manage”, line 9, in

5  
load\_entry\_point(‘awx==2.1.2.0’, ‘console\_scripts’, ‘awx-manage’)()

6  
File “/usr/lib/python2.7/site-packages/awx/ **init**.py”, line 150, in manage

7  
execute\_from\_command\_line(sys.argv)

8  
File “/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/core/management/ **init**.py”, line 364, in execute\_from\_command\_line

9  
utility.execute()

10  
File “/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/core/management/ **init**.py”, line 356, in execute

11  
self.fetch\_command(subcommand).run\_from\_argv(self.argv)

12  
File “/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/core/management/base.py”, line 283, in run\_from\_argv

13  
self.execute(\*args, \*\*cmd\_options)

14  
File “/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/core/management/base.py”, line 330, in execute

15  
output = self.handle(\*args, \*\*options)

16  
File “/usr/lib/python2.7/site-packages/awx/main/management/commands/inventory\_import.py”, line 994, in handle

17  
self.is\_custom)

18  
File “/usr/lib/python2.7/site-packages/awx/main/management/commands/inventory\_import.py”, line 245, in load\_inventory\_source

19  
is\_custom=is\_custom).load()

20  
File “/usr/lib/python2.7/site-packages/awx/main/management/commands/inventory\_import.py”, line 180, in load

21  
data = self.command\_to\_json(base\_args + [‘–list’])

22  
File “/usr/lib/python2.7/site-packages/awx/main/management/commands/inventory\_import.py”, line 163, in command\_to\_json

23  
self.method, proc.returncode, stdout, stderr))

24  
RuntimeError: ansible-inventory failed (rc=1) with stdout:

26  
stderr:

27  
[WARNING]: \* Failed to parse /var/lib/awx/projects/\_6\_\_k8s\_inventory/k8s.yaml

28  
with k8s plugin: This module requires the OpenShift Python client. Try `pip

29  
install openshift`

30

31  
[WARNING]: Unable to parse /var/lib/awx/projects/\_6\_\_k8s\_inventory/k8s.yaml as

32  
an inventory source

33

34  
[WARNING]: \* Failed to parse

35  
/var/lib/awx/projects/\_6\_\_k8s\_inventory/playbook.yaml with k8s plugin:

36  
‘AnsibleSequence’ object has no attribute ‘get’

37

38  
[WARNING]: Unable to parse

39  
/var/lib/awx/projects/\_6\_\_k8s\_inventory/playbook.yaml as an inventory source

40

41  
[WARNING]: Unable to parse /var/lib/awx/projects/\_6\_\_k8s\_inventory as an

42  
inventory source

43

44  
ERROR! No inventory was parsed, please check your configuration and options.

`

---

<div class="post-metadata">

**Author:** ![AlanCoding](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/alancoding/32/301_2.png) [@AlanCoding](https://forum.ansible.com/u/AlanCoding)\
**Post date:** [December 17, 2018, 12:41pm UTC](https://forum.ansible.com/t/inventory-from-kubernetes/29251/7 "2018-12-17T12:41:05Z")

</div>

Yes, if it’s not officially vendored, as in this case, you have to shell into the container, active the Ansible virtual environment (or a custom venv) and install it there.

---

<div class="post-metadata">

**Author:** ![kurrent93](https://avatars.discourse-cdn.com/v4/letter/k/a4c791/32.png) [@kurrent93](https://forum.ansible.com/u/kurrent93)\
**Post date:** [December 17, 2018, 8:17pm UTC](https://forum.ansible.com/t/inventory-from-kubernetes/29251/8 "2018-12-17T20:17:57Z")

</div>

Thanks Alan

It looks like the docker container is hardened, so I do not have the permissions.

`

pip install openshift  
Collecting openshift  
Using cached [https://files.pythonhosted.org/packages/5d/e4/b63be784c69be189f7902ace3251c1c1c78701f8c57f1318a9daaea4cfa8/openshift-0.8.1.tar.gz](https://files.pythonhosted.org/packages/5d/e4/b63be784c69be189f7902ace3251c1c1c78701f8c57f1318a9daaea4cfa8/openshift-0.8.1.tar.gz)  
Complete output from command python setup.py egg\_info:  
Traceback (most recent call last):  
File “”, line 1, in   
File “/tmp/pip-build-tP31DU/openshift/setup.py”, line 50, in   
packages=find\_packages(include=‘openshift.\*’),  
TypeError: find\_packages() got an unexpected keyword argument ‘include’
