Pages

Friday, 19 March 2021

Use Ansible to control k8s running on CentOS 7 (Multiple ansible python interpreters in a playbook)

 1 Env

  • Ansible controller: Ansible 2.10
  • Managed node: CentOS 7, K8S control plane node

2 Python version conflicts between yum and k8s modules

  • The yum module only works with Python 2 which is the default Python on CentOS 7.
  • the k8s module only works with Python 3

3 Solution

Use  Python 2 as the default ansible python interpreter, while using Python 3 only for k8s tasks.

3.1 Ansible controller 

ansible-galaxy collection install community.kubernetes

3.2 Managed node

On the managed node, install python3 and OpenShift python module which is required by the Ansible k8s module.

# yum install python3
# pip3 install openshift

3.3 Example playbook

e.g.

---
- hosts: k8s_control_plane
  tasks:
     - name: install httpd
       yum: 
          name: httpd
          state: present
     - name: Ensure mynamespace exists
       vars:
          ansible_python_interpreter: /usr/bin/python3
       community.kubernetes.k8s:
         api_version: v1
         kind: Namespace
         name: mynamespace
         state: present

No comments:

Post a Comment