Ansible for Kubernetes by Example by Unknown

Ansible for Kubernetes by Example by Unknown

Author:Unknown
Language: eng
Format: epub


Chapter 4 ansible for K8s tasKs

(the default path), where the ~ (tilde symbol) indicates the home of your user. You can customize this path using the kubeconfig parameter for each Ansible module. The most common usage with the k8s Ansible module is as follows:

kubernetes.core.k8s:

kubeconfig: '~/.kube/config'

Configure a Python Virtual Environment

A Python Virtual Environment is a tool to keep your application environment separate from the system-wide environment. Ansible, like all Python applications, requires some Python libraries as dependencies. Maintaining up-to-date all the Python dependencies of Ansible and of the Ansible collection without interfering with your Linux system can be challenging without a Python Virtual Environment.

The first step is to create the virtual environment using the Python venv module, preceded by the -m parameter. In the following example, the virtual environment is named venv; feel free to customize it according to your needs:

$ python3 -m venv venv

After creating the venv virtual environment, you need to activate it using the script for your operating system activate for Linux/macOS e activate.bat for Windows. You can find the script in the directory named like your virtual environment (venv). You can return every time to this state using the activate script.

$ source venv/bin/activate

Once inside the venv virtual environment, prepend the prompt to the name (venv).

It’s a best practice to upgrade the package manager pip and setuptools to the latest versions:

(venv) $ pip3 install --upgrade pip setuptools

On some systems, the pip/pip3 command tool is not available out of the box.

It might be necessary to install the python3-pip package or use following a manual installation process.

$ python -m ensurepip --upgrade

120

Chapter 4 ansible for K8s tasKs

After upgrading the pip installed, you can install the Python PyYAML, jsonpatch and kubernetes dependency:

(venv) $ pip3 install PyYAML jsonpatch kubernetes

For Ansible modules that interact with the Helm package manager, the extra helm Python library is required with the --use-pep517 parameter workaround:

(venv) $ pip3 install --use-pep517 helm

You can save all the Python libraries and their dependencies using the pip3 freeze command and save the result to requirements.txt. Sharing this file with someone else or another system will result in the same configuration of this virtual environment.

(venv) $ pip3 freeze > requirements.txt

Listing 4-3 shows the contents of the requirements.txt at the time of writing this book; it includes the ansible-builder and ansible-runner utilities.

Listing 4-3. The requirements.txt File

ansible-builder==1.2.0

ansible-runner==2.3.1

bindep==2.11.0

cachetools==5.2.0

certifi==2022.12.7

charset-normalizer==2.1.1

distro==1.8.0

docutils==0.19

google-auth==2.15.0

helm==0.4

idna==3.4

kubernetes==25.3.0

lockfile==0.12.2

oauthlib==3.2.2

packaging==22.0

Parsley==1.3

pbr==5.11.0

pexpect==4.8.0

121

Chapter 4 ansible for K8s tasKs

ptyprocess==0.7.0

pyasn1==0.4.8

pyasn1-modules==0.2.8

python-daemon==2.3.2

python-dateutil==2.8.2

PyYAML==6.0

requests==2.28.1

requests-oauthlib==1.3.1

requirements-parser==0.5.0

rsa==4.9

six==1.16.0

types-setuptools==65.6.0.2

urllib3==1.26.13

websocket-client==1.4.2

If you want to use the Ansible Turbo feature, you must install the cloud.common Ansible collection using the ansible-galaxy command-line utility:

(venv) $ ansible-galaxy collection install cloud.common

The kubernetes.core Ansible collection is a must-have in your system to execute Kubernetes automation:

(venv) $ ansible-galaxy collection install kubernetes.core

You can deactivate your virtual environment at any time and return to your system console. You should activate the virtual environment every time you want to use it. The virtual environment preserves all installed libraries and collections.

$ deactivate

When you want to use your virtual environment, just execute the activate script again.

When the complexity of the Python Virtual Environment grows too much, it can become challenging to manage the Python library dependencies and Ansible module dependencies.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.