<p>I'm trying to run a kubectl command from ansible.<br>
Basically the command will tell me if at least one pod is running from a deployment.  </p>

<pre><code>kubectl get deploy sample-v1-deployment -o json -n sample | jq '.status.conditions[] | select(.reason == "MinimumReplicasAvailable") | .status' | tr -d '"'
</code></pre>

<p>I tried to run it from a playbook but I'm getting </p>

<blockquote>
  <p>Unable to connect to the server: net/http: TLS handshake timeout</p>
</blockquote>

<p>This is my playbook:</p>

<pre><code>- hosts: master
  gather_facts: no
  become: true

  tasks:
    - name: test command
      shell: kubectl get deploy sample-v1-deployment -o json -n sample | jq '.status.conditions[] | select(.reason == "MinimumReplicasAvailable") | .status' | tr -d '"'
      register: result
</code></pre>

<p>This is the output from ansible:</p>

<pre><code>changed: [k8smaster01.test.com] =&gt; {
    "changed": true,
    "cmd": "kubectl get deploy sample-v1-deployment -o json -n sample | jq '.status.conditions[] | select(.reason == \"MinimumReplicasAvailable\") | .status' | tr -d '\"'",
    "delta": "0:00:10.507704",
    "end": "2019-04-02 20:59:17.882277",
    "invocation": {
        "module_args": {
            "_raw_params": "kubectl get deploy sample-v1-deployment -o json -n sample | jq '.status.conditions[] | select(.reason == \"MinimumReplicasAvailable\") | .status' | tr -d '\"'",
            "_uses_shell": true,
            "argv": null,
            "chdir": null,
            "creates": null,
            "executable": null,
            "removes": null,
            "stdin": null,
            "warn": true
        }
    },
    "rc": 0,
    "start": "2019-04-02 20:59:07.374573",
    "stderr": "Unable to connect to the server: net/http: TLS handshake timeout",
    "stderr_lines": [
        "Unable to connect to the server: net/http: TLS handshake timeout"
    ],
    "stdout": "",
    "stdout_lines": []
}
</code></pre>

<p>I can run the command manually on the master server without problems. I was also able to use k8s module to create different things on my kubernetes cluster.<br>
I know there is a kubectl module on ansible, could it be the problem?</p>

<p>Thanks</p>

radicaled
<p>I found a couple of workarounds.<br>
One was to use the k8s_facts module</p>

<pre><code>- name: Ensure running application
  k8s_facts:
    namespace: sample
    kind: Pod
    label_selectors:
      - app=sample-v1-app
  register: pod_list
  until: pod_list.resources[0].status.phase == 'Running'
  delay: 10
  retries: 3
</code></pre>

<p>Its simple and gets the works done.  </p>

<p>The second workaround was to use the raw module instead of shell or command</p>

<pre><code>- name: Get running status
  raw: kubectl get deploy sample-v1-deployment -o json -n sample | jq -r '.status.conditions[] | select(.reason == "MinimumReplicasAvailable") | .status'
</code></pre>

<p>I'm not sure about using raw. It looks like a hammer for a simple task.<br>
But reading about the module makes me think this problem is related with the syntax (quotes, double quotes, |) more than the command it self.</p>

<blockquote>
  <p>Executes a low-down and dirty SSH command, not going through the
  module subsystem. This is useful and should only be done in a few
  cases. A common case is installing python on a system without python
  installed by default. Another is speaking to any devices such as
  routers that do not have any Python installed.</p>
</blockquote>
