<p>Inventory file (inventory/k8s.yaml):</p>
<pre><code>plugin: kubernetes.core.k8s
connections:
  - kubeconfig: ~/.kube/config
    context: 'cluster-2'
</code></pre>
<p>Task file (roles/common/tasks/main.yaml):</p>
<pre><code># Method 1: Using `kubernetes.core` plugin to list the pod names:
- name: Get a list of all pods from any namespace
  kubernetes.core.k8s_info:
    kind: Pod
  register: pod_list
- name: Print pod names
  debug:
    msg: &quot;pod_list: {{ pod_list | json_query('resources[*].metadata.name') }} &quot;

# Method 2: Using `shell` command to list the pod names:
- name: Get node names
  shell: kubectl get pods
  register: pod_list2
- name: Print pod names
  debug:
    msg: &quot;{{ pod_list2.stdout }}&quot;
</code></pre>
<p>Ansible config (ansible.cfg):</p>
<pre><code>[inventory]
enable_plugins = host_list, auto, yaml, ini, kubernetes.core.k8s
</code></pre>
<p>Main file (main.yaml):</p>
<pre><code>---
- hosts: localhost
  gather_facts: false
  collections:
    - azure.azcollection
    - kubernetes.core
  roles:
    - &quot;common&quot;
</code></pre>
<p>Running command to execute task: <code>ansible-playbook main.yaml -i cluster-2/k8s.yaml -e role=common -e cluster_name=cluster-2</code></p>
<p>Question:
I am running the above configs to run get the pods from the remote cluster mentioned in the inventory file. But, the problem is, I am still getting the pod names from the local cluster and not the cluster-2 in Method 1 and 2.</p>
<p>k8s plugin should get the list of pods from cluster-2 as described in the inventory file. How can I connect to remote kubernetes cluster?</p>
<p>I also checked output with <code>-vvvv</code>:</p>
<pre><code>ansible-playbook [core 2.14.0]
config file = /Users/test/u/apps/ansible.cfg
configured module search path = ['/Users/test/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /Users/test/Library/Python/3.9/lib/python/site-packages/ansible
ansible collection location = /Users/test/.ansible/collections:/usr/share/ansible/collections
executable location = /Users/test/Library/Python/3.9/bin/ansible-playbook
python version = 3.9.12 (main, Mar 26 2022, 15:52:10) [Clang 13.0.0 (clang-1300.0.29.30)] (/usr/local/opt/python@3.9/bin/python3.9)
jinja version = 3.1.2
libyaml = True
Using /Users/test/u/apps/ansible.cfg as config file
setting up inventory plugins
Loading collection kubernetes.core from /Users/test/.ansible/collections/ansible_collections/kubernetes/core
</code></pre>

<p>You're trying to use both the kubernetes inventory plugin <strong>and</strong> the <code>k8s_info</code> module, and because of that you're getting conflicting results. The two don't have anything to do with each other.</p>
<h2>The inventory module</h2>
<p>The kubernetes inventory module is -- I think -- a weird beast; it produces an ansible inventory in which the pods in your cluster are presented as Ansible hosts. To see a list of all the pod names in your cluster, you could write a playbook like this:</p>
<pre><code>- hosts: all
  gather_facts: false
  tasks:
  - name: Print pod names
    debug:
      msg: &quot;{{ inventory_hostname }}&quot;
</code></pre>
<p>This will respect the context you've configured in your kubernetes inventory plugin configuration. For example, if I have in <code>inventory/k8s.yaml</code> the following:</p>
<pre><code>plugin: kubernetes.core.k8s
connections:
  - kubeconfig: ./kubeconfig
    context: 'kind-cluster2'
</code></pre>
<p>Then the above playbook will list the pod names from <code>kind-cluster2</code>, regardless of the <code>current-context</code> setting in my <code>kubeconfig</code> file. In my test environment, this produces:</p>
<pre><code>PLAY [all] *********************************************************************

TASK [Print pod names] *********************************************************
ok: [kubernetes] =&gt; {
    &quot;msg&quot;: &quot;kubernetes&quot;
}
ok: [coredns-565d847f94-2shl6_coredns] =&gt; {
    &quot;msg&quot;: &quot;coredns-565d847f94-2shl6_coredns&quot;
}
ok: [coredns-565d847f94-md57c_coredns] =&gt; {
    &quot;msg&quot;: &quot;coredns-565d847f94-md57c_coredns&quot;
}
ok: [kube-dns] =&gt; {
    &quot;msg&quot;: &quot;kube-dns&quot;
}
ok: [etcd-cluster2-control-plane_etcd] =&gt; {
    &quot;msg&quot;: &quot;etcd-cluster2-control-plane_etcd&quot;
}
ok: [kube-apiserver-cluster2-control-plane_kube-apiserver] =&gt; {
    &quot;msg&quot;: &quot;kube-apiserver-cluster2-control-plane_kube-apiserver&quot;
}
ok: [kube-controller-manager-cluster2-control-plane_kube-controller-manager] =&gt; {
    &quot;msg&quot;: &quot;kube-controller-manager-cluster2-control-plane_kube-controller-manager&quot;
}
ok: [kube-scheduler-cluster2-control-plane_kube-scheduler] =&gt; {
    &quot;msg&quot;: &quot;kube-scheduler-cluster2-control-plane_kube-scheduler&quot;
}
ok: [kindnet-nc27b_kindnet-cni] =&gt; {
    &quot;msg&quot;: &quot;kindnet-nc27b_kindnet-cni&quot;
}
ok: [kube-proxy-9chgt_kube-proxy] =&gt; {
    &quot;msg&quot;: &quot;kube-proxy-9chgt_kube-proxy&quot;
}
ok: [local-path-provisioner-684f458cdd-925v5_local-path-provisioner] =&gt; {
    &quot;msg&quot;: &quot;local-path-provisioner-684f458cdd-925v5_local-path-provisioner&quot;
}

PLAY RECAP *********************************************************************
coredns-565d847f94-2shl6_coredns : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
coredns-565d847f94-md57c_coredns : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
etcd-cluster2-control-plane_etcd : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
kindnet-nc27b_kindnet-cni  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
kube-apiserver-cluster2-control-plane_kube-apiserver : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
kube-controller-manager-cluster2-control-plane_kube-controller-manager : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
kube-dns                   : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
kube-proxy-9chgt_kube-proxy : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
kube-scheduler-cluster2-control-plane_kube-scheduler : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
kubernetes                 : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
local-path-provisioner-684f458cdd-925v5_local-path-provisioner : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

</code></pre>
<p>The key point here is that your inventory will consist of a list of pods. I've never found this particularly useful.</p>
<h2>The <code>k8s_info</code> module</h2>
<p>The <code>k8s_info</code> queries a kubernetes cluster for a list of objects. It doesn't care about your inventory configuration -- it will run on whichever target host you've defined for your play (probably <code>localhost</code>) and perform the rough equivalent of <code>kubectl get &lt;whatever&gt;</code>. If you want to use an explicit context, you need to set that as part of your module parameters. For example, to see a list of pods in <code>kind-cluster2</code>, I could use the following playbook:</p>
<pre><code>- hosts: localhost
  gather_facts: false
  tasks:
    - kubernetes.core.k8s_info:
        kind: pod
        kubeconfig: ./kubeconfig
        context: kind-cluster2
      register: pods

    - debug:
        msg: &quot;{{ pods.resources | json_query('[].metadata.name') }}&quot;
</code></pre>
<p>Which in my test environment produces as output:</p>
<pre><code>PLAY [localhost] ***************************************************************

TASK [kubernetes.core.k8s_info] ************************************************
ok: [localhost]

TASK [debug] *******************************************************************
ok: [localhost] =&gt; {
    &quot;msg&quot;: [
        &quot;coredns-565d847f94-2shl6&quot;,
        &quot;coredns-565d847f94-md57c&quot;,
        &quot;etcd-cluster2-control-plane&quot;,
        &quot;kindnet-nc27b&quot;,
        &quot;kube-apiserver-cluster2-control-plane&quot;,
        &quot;kube-controller-manager-cluster2-control-plane&quot;,
        &quot;kube-proxy-9chgt&quot;,
        &quot;kube-scheduler-cluster2-control-plane&quot;,
        &quot;local-path-provisioner-684f458cdd-925v5&quot;
    ]
}

PLAY RECAP *********************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
</code></pre>
<hr />
<p>In conclusion: you probably want to use <code>k8s_info</code> rather than the inventory plugin, and you'll need to configure the module properly by setting the <code>context</code> (and possibly the <code>kubeconfig</code>) parameters when you call the module.</p>
<hr />
<blockquote>
<p>Is there any way I can define context and kubeconfig outside of the tasks (globally) if I am using k8s_info module?</p>
</blockquote>
<p>According to <a href="https://docs.ansible.com/ansible/latest/collections/kubernetes/core/k8s_info_module.html" rel="nofollow noreferrer">the documentation</a>, you could set the <code>K8S_AUTH_KUBECONFIG</code> and <code>K8S_AUTH_CONTEXT</code> environment variables if you want to globally configure the settings for the <code>k8s_info</code> module. You could also write your task like this:</p>
<pre><code>    - kubernetes.core.k8s_info:
        kind: pod
        kubeconfig: &quot;{{ k8s_kubeconfig }}&quot;
        context: &quot;{{ k8s_context }}&quot;
      register: pods
</code></pre>
<p>And then define the <code>k8s_kubeconfig</code> and <code>k8s_context</code> variables somewhere else in your Ansible configuration (e.g., as group vars). This makes it easy to retarget things to a different cluster with only a single change.</p>
