<p>I try to run some ansible tasks with the k8s module. Locally this works perfect, but on my Jenkins instance, it fails with the following error message:</p>

<blockquote>
  <p>...</p>
  
  <p>MaxRetryError(_pool, url, error or
  ResponseError(cause))\nurllib3.exceptions.MaxRetryError:
  HTTPSConnectionPool(host='xxxxxxxxxxxxxx',
  port=443): Max retries exceeded with url: /version (Caused by
  NewConnectionError(': Failed to establish a new connection: [Errno -2]
  Name or service not known',))\n", 
      "module_stdout": "", 
      "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", 
      "rc": 1 }</p>
</blockquote>

<p>I am quite sure this is because the Jenkins requires a proxy to communicate to the outside world. I´ve seen how to set up ansible for using a proxy, but that does not seem to work with the k8s module. Any ideas? Here´s what I´ve tried so far:</p>

<pre><code> - hosts: ansible_server
   connection: local
   gather_facts: no
   environment:
    https_proxy: "xxx"
    http_proxy: "xxx"
   tasks:
    - name: Gather facts to check connectivity
      k8s_facts:
       api_key: "{{api_key}}"
       host: "{{cluster_url}}"
       kind: Project
      register: listed_projects
</code></pre>

<p>PS: I added the -vvv flag and can see that it tries to use the proxy somehow:</p>

<blockquote>
  <p> EXEC /bin/sh -c '/usr/bin/python &amp;&amp; sleep 0' Using module
  file
  /usr/lib/python2.7/site-packages/ansible/modules/clustering/k8s/k8s_facts.py
   PUT /root/.ansible/tmp/ansible-local-1fHx5f6/tmpDUhlNa TO
  /root/.ansible/tmp/ansible-tmp-1570565569.96-190678136757098/AnsiballZ_k8s_facts.py
   EXEC /bin/sh -c 'chmod u+x
  /root/.ansible/tmp/ansible-tmp-1570565569.96-190678136757098/
  /root/.ansible/tmp/ansible-tmp-1570565569.96-190678136757098/AnsiballZ_k8s_facts.py
  &amp;&amp; sleep 0'  EXEC /bin/sh -c
  'https_proxy=xxx
  http_proxy=xxx
  /usr/bin/python
  /root/.ansible/tmp/ansible-tmp-1570565569.96-190678136757098/AnsiballZ_k8s_facts.py
  &amp;&amp; sleep 0'  EXEC /bin/sh -c 'rm -f -r
  /root/.ansible/tmp/ansible-tmp-1570565569.96-190678136757098/ >
  /dev/null 2>&amp;1 &amp;&amp; sleep 0'</p>
</blockquote>

EllisTheEllice
<p>I agree with @ilias-sp but it also appears that <a href="https://github.com/ansible/ansible/blob/v2.8.5/lib/ansible/module_utils/k8s/common.py" rel="nofollow noreferrer"><code>k8s/common.py</code></a> does not support the <a href="https://github.com/kubernetes-client/python/blob/v10.0.1/kubernetes/client/configuration.py#L103" rel="nofollow noreferrer"><code>configuration.proxy</code> attribute</a>, , and as best I can tell <code>urllib3</code> does not honor those proxy environment variables the way "normal" urllib does, opting instead to use its own <code>ProxyManager</code> that is driven by an explicit constructor kwarg</p>

<p>However, thanks to the "override" mechanism of ansible, I believe you can test this theory:</p>

<ol>
<li>Copy <code>k8s_facts.py</code> into the <code>library</code> folder of your playbook</li>
<li>Modify it to expose <code>proxy</code> in the <a href="https://github.com/ansible/ansible/blob/v2.8.5/lib/ansible/module_utils/k8s/common.py#L128-L139" rel="nofollow noreferrer"><code>AUTH_ARG_MAP</code></a>, which I believe the patch below will do (the patch is against v2.8.5 so you may need to fiddle with it if your version is different)</li>
<li><p>Explicitly set your <code>proxy:</code> attribute on your new <code>k8s_facts</code> module and see if it works</p>

<pre><code>- k8s_facts:
    host: api-server-whatever
    kind: Project
    proxy: http://my-proxy:3128
</code></pre></li>
<li>Assuming it does, <a href="https://github.com/ansible/ansible/issues" rel="nofollow noreferrer">open an issue in ansible</a> to let them know</li>
</ol>

<pre><code>--- a/library/k8s_facts.py  2019-10-08 22:23:24.000000000 -0700
+++ b/library/k8s_facts.py  2019-10-08 22:24:50.000000000 -0700
@@ -130,13 +130,14 @@
 '''


-from ansible.module_utils.k8s.common import KubernetesAnsibleModule, AUTH_ARG_SPEC
+from ansible.module_utils.k8s.common import KubernetesAnsibleModule, AUTH_ARG_SPEC, AUTH_ARG_MAP
 import copy


 class KubernetesFactsModule(KubernetesAnsibleModule):

     def __init__(self, *args, **kwargs):
+        AUTH_ARG_MAP['proxy'] = 'proxy'
         KubernetesAnsibleModule.__init__(self, *args,
                                          supports_check_mode=True,
                                          **kwargs)
@@ -163,6 +164,7 @@
                 namespace=dict(),
                 label_selectors=dict(type='list', default=[]),
                 field_selectors=dict(type='list', default=[]),
+                proxy=dict(type='str', required=False),
             )
         )
         return args
</code></pre>
