Search code examples
kubernetesansiblecloudrancher

How to use Kubectl commands to Acess a Rancher Cluster through Ansible


I am currently developing a project where I need to get the pod names of a Kubernetes Cluster running on Rancher using Ansible. The main thing here is that I have a couple of problems that are preventing me from advance. I am currently executing a playbook to try to retrieve this information, instead of running a CLI command, because I want to manipulate those Rancher machines later one (e.g. install an rpm file). Here is the playbook that I am executing tot try to retrieve the pods' names from Rancher:

---

- hosts: localhost
  connection: local
  remote_user: root
  roles:
    - role: ansible.kubernetes-modules
    - role: hello-world
  vars:
    ansible_python_interpreter: '{{ ansible_playbook_python }}'

  collections:
    - community.kubernetes

  tasks:
    -
      name: Gather openShift Dependencies
      python_requirements_facts:
        dependencies:
        - openshift

    -
      name: Get the pods in the specific namespace
      k8s_info:
        kubeconfig: '/etc/ansible/RCCloudConfig'
        kind: Pod
        namespace: redmine
      register: pod_list

    -
      name: Print pod names 
      debug:
         msg: "pod_list: {{ pod_list | json_query('resources[*].status.podIP')  }} "

    - set_fact:
        pod_names: "{{pod_list|json_query('resources[*].metadata.name')}}"

The problem is that I am getting a Kubernetes module error each time I am trying to run the playbook:

ERROR! the role 'ansible.kubernetes-modules' was not found in community.kubernetes:ansible           .legacy:/etc/ansible/roles:/home/jcp/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/           roles:/etc/ansible

The error appears to be in '/etc/ansible/GetKubectlPods': line 7, column 7, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

  roles:
    - role: ansible.kubernetes-modules
      ^ here

If I remove that line on the code, Where I try to retrieve that role, I still get a similar error:

An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ModuleNotFoundError: No module named 'kubernetes'
fatal: [localhost]: FAILED! => {"changed": false, "error": "No module named 'kubernetes'", "msg": "Failed to import the required Python library (openshift) on localhost.localdomain's Python /usr/bin/python3.6. Please read module documentation and install in the appropriate location. If the required library is installed, but Ansible is using the wrong Python interpreter, please consult the documentation on ansible_python_interpreter"}

I have already tried to install ansible-galaxy kubernetes module on the machine and openshift. Not sure what I am doing wrong since there are so many possibilities for what could be going wrong here.

Ansible Version Output:

ansible 2.9.9
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/jcp/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/jcp/.local/lib/python3.6/site-packages/ansible
  executable location = /home/jcp/.local/bin/ansible
  python version = 3.6.8 (default, Nov 21 2019, 19:31:34) [GCC 8.3.1 20190507 (Red Hat 8.3.1-4)]

I've debugged my python_required_info output from openshift dependencies and this is what I have:

ok: [localhost] => {
    "openshift_dependencies": {
        "changed": false,
        "failed": false,
        "mismatched": {},
        "not_found": [],
        "python": "/usr/bin/python3.6",
        "python_system_path": [
            "/tmp/ansible_python_requirements_info_payload_5_kb4a7s/ansible_python_requirements_info_payloa            d.zip",
            "/usr/lib64/python36.zip",
            "/usr/lib64/python3.6",
            "/usr/lib64/python3.6/lib-dynload",
            "/home/jcp/.local/lib/python3.6/site-packages",
            "/usr/local/lib/python3.6/site-packages",
            "/usr/local/lib/python3.6/site-packages/openshift-0.10.0.dev1-py3.6.egg",
            "/usr/lib64/python3.6/site-packages",
            "/usr/lib/python3.6/site-packages"
        ],
        "python_version": "3.6.8 (default, Nov 21 2019, 19:31:34) \n[GCC 8.3.1 20190507 (Red Hat 8.3.1-4)]"            ,
        "valid": {
            "openshift": {
                "desired": null,
                "installed": "0.10.0.dev1"
            }
        }
    }
}

Thanks for your help in advance!


Solution

  • Edit: The below answer was given for OP's specific Ansible version (i.e. 2.9.9) and is still valid if you still use it. Since version 2.10, you also need to install the relevant ansible collection if not already present

    ansible-galaxy collection install kubernetes.core
    

    See the latest module documentation for more information


    In Ansible 2.9.9, you're not supposed to do anything special to use the module except installing the needed python dependencies. See the module documentation for your Ansible version

    1. remove the line - role: ansible.kubernetes-modules, unless it is a module of yours in which case you have to tell us more because this is not a correct declaration.
    2. remove the collection declaration
    3. Add the following task somewhere before using the module:
      - name: Make sure python deps are installed
        pip:
          name: openshift
      

    Your actual python_requirement_facts task is doing nothing else than reporting the dependency is not found. Register the result and debug it to see for yourself.

    Now use the k8s_info module normally.