Ansible Error: "[Errno 2] No such file or directory" - kubernetes

I'm not able to execute kubectl(v1.16.3) commands in the ansible command module.
For e.g. Creation of Namespace using ansible.
tasks:
- name: "Creating Directory"
file:
path: ~/ansible_ns/demo_namespaces
state: directory
- name: "Creating Namespaces(1/2)"
copy:
content: "apiVersion: v1 \nkind: Namespace \nmetadata: \n name: {{item}} "
dest: "~/ansible_ns/demo_namespaces/{{item}}.yml"
with_items:
- "{{ namespace }}"
- name: "Creating Namespaces(2/2)"
command: "kubectl create -f {{item}}.yml --kubeconfig=/var/lib/kubernetes/kubeconfig.yaml"
args:
chdir: ~/ansible_ns/demo_namespaces/
ignore_errors: true
with_items:
- "{{ namespace }}"
I'm ending up with the below error:
(item=ns) => {
"ansible_loop_var": "item",
"changed": false,
"cmd": "kubectl create -f ns.yml --kubeconfig=/var/lib/kubernetes/kubeconfig.yaml",
"invocation": {
"module_args": {
"_raw_params": "kubectl create -f ns.yml --kubeconfig=/var/lib/kubernetes/kubeconfig.yaml",
"_uses_shell": false,
"argv": null,
"chdir": "/root/ansible_ns/demo_namespaces/",
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"stdin_add_newline": true,
"strip_empty_ends": true,
"warn": true
}
},
"item": "ns",
"msg": "[Errno 2] No such file or directory",
"rc": 2
}
NOTE: But I'm able to do "kubectl create -f .." manually..and it is creating the stuff.
My Ansible version:
$ ansible --version
ansible 2.9.2
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/mdupaguntla/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.5 (default, Aug 4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]
FYI, I also tried with Ansible - 2.4.2 as well. But No luck.
My System OS: CentOS 7
My queries:
What is this error mean "[Errno 2] No such file or directory" in my context?
I came to know that Ansible introduced kubectl & k8s module: Is there anyone in the community using these.. If Yes, please let me know how to use them. If they any prerequisites - please share them
For kubectl Module: Came to know that the pre-requisite is kubectl go library.May I know where can I
get this Library.
when the kubectl version is 1.8 and ansible version is 2.4.2 - I'm able to get the K8s resources created using "kubectl create -f ..." using command module. But when I upgraded my cluster from v1.8 to v1.16.3 - I'm not able to create the resources using "kubectl create -f ..." using command module. Let me if I missed doing things.
Thanks in advance for the Community

You have to add the path for kubectl in the command module.
command: "/the/path/kubectl create -f {{item}}.yml .........."
This is because the $PATH is not updated with the path of kubectl. You can add the path to $PATH also instead of giving the path in command module.

If you use ansible 2.9.2, it has k8s module available. It provides fully declarative approach ( versus issuing imperative commands ) which is more similar to what you can find in kubernetes itself.
For example if you want to create a new namespace, just use:
- name: Create a k8s namespace
k8s:
name: testing
api_version: v1
kind: Namespace
state: present
You have to admit it looks much simpler.

Well, there are two ways to make this process better and functional.
You can try to use k8s module like this way.
- name: Create k8s catota namespace
k8s:
name: catota
api_version: v1
kind: Namespace
state: present
Or you can use the shell module as well:
- name: Create k8s catota namespace
shell: "kubectl create namespace catota"
args:
executable: /bin/bash

Troubleshooting This Issue
The Error:
(item=ns) => {
"ansible_loop_var": "item",
"changed": false,
"cmd": "kubectl create -f ns.yml --kubeconfig=/var/lib/kubernetes/kubeconfig.yaml",
"invocation": {
"module_args": {
"_raw_params": "kubectl create -f ns.yml --kubeconfig=/var/lib/kubernetes/kubeconfig.yaml",
"_uses_shell": false,
"argv": null,
"chdir": "/root/ansible_ns/demo_namespaces/",
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"stdin_add_newline": true,
"strip_empty_ends": true,
"warn": true
}
},
"item": "ns",
"msg": "[Errno 2] No such file or directory",
"rc": 2
}
First, notice that this states "_uses_shell": false,. This is because it is using Command instead of shell. This is also disguising the error code. If we switch to shell and re-run we get:
"msg": "non-zero return code",
"rc": 127,
"start": "2021-09-03 13:48:12.184639",
"stderr": "/bin/bash: <PROGRAM>: command not found",
(details on exit codes https://www.gnu.org/software/bash/manual/html_node/Exit-Status.html )
The /bin/bash is the giveaway. Doing which kubectl you might get something like /usr/local/bin/kubectl.
You either need to update the ansible command to use /usr/local/bin/kubectl or update the $PATH for /bin/bash/ to find it.

Related

etc/kubernetes/admin.conf didn't created - kubespray

I want to install the new cluster on 3 machines.
I ran this command:
ansible-playbook -i inventory/local/hosts.ini --become --become-user=root cluster.yml
but the installation failed:
TASK [remove-node/pre-remove : remove-node | List nodes] *********************************************************************************************************************************************************
fatal: [node1 -> node1]: FAILED! => {"changed": false, "cmd": ["/usr/local/bin/kubectl", "--kubeconfig", "/etc/kubernetes/admin.conf", "get", "nodes", "-o", "go-template={{ range .items }}{{ .metadata.name }}{{ "\n" }}{{ end }}"], "delta": "0:00:00.057781", "end": "2022-03-16 21:27:20.296592", "msg": "non-zero return code", "rc": 1, "start": "2022-03-16 21:27:20.238811", "stderr": "error: stat /etc/kubernetes/admin.conf: no such file or directory", "stderr_lines": ["error: stat /etc/kubernetes/admin.conf: no such file or directory"], "stdout": "", "stdout_lines": []}
Why the installation step tried to remove and why /etc/kubernetes/admin.conf has not been created?
Please assist.
There could be a couple of ways how can you solve your problem. First look at this github issue. Probably you can manually copy the missing file and it should work:
I solved it myself.
I copied the /etc/kubernetes/admin.conf and /etc/kubernetes/ssl/ca.* to the new node and now the scale playbook works. Maybe this is not the right way, but it worked...
The another way is to use wait for module on Ansible. You can find example of usage in this thread.
To another solution I will recommend to read this similar problem:
cluster_initialized.txt created on first fail and ansible never runs kubeadm init again. just delete that file on fail, fix the problem and run again.

How to create a secret in k8s using Ansible-K8s

I've create the following task in my ansible playbook.
- name: Create a k8s namespace
k8s:
state: present
definition:
apiVersion: v1
kind: Secret
metadata:
name: bigip-login
namespace: kube-system
data:
password: dGVzdA==
username: YWRtaW4=
type: Opaque
However when I run my playbook I got the following error:
The full traceback is:
Traceback (most recent call last):
File "/tmp/ansible_k8s_payload_n071fcyu/ansible_k8s_payload.zip/ansible_collections/kubernetes/core/plugins/module_utils/common.py", line 92, in <module>
from kubernetes.dynamic.resource import ResourceInstance
ModuleNotFoundError: No module named 'kubernetes'
fatal: [master.madebeen.com]: FAILED! => {
"changed": false,
"error": "No module named 'kubernetes'",
"invocation": {
"module_args": {
"api_key": null,
"api_version": "v1",
"append_hash": false,
"apply": false,
"ca_cert": null,
"client_cert": null,
"client_key": null,
"context": null,
"continue_on_error": false,
"definition": {
"apiVersion": "v1",
"data": {
"password": "VGFyLk1pZC5GdW4tNDU2",
"username": "YWRtaW4="
},
"kind": "Secret",
"metadata": {
"name": "bigip-login",
"namespace": "kube-system"
},
"type": "Opaque"
},
"delete_options": null,
"force": false,
"host": null,
"kind": null,
"kubeconfig": null,
"label_selectors": null,
"merge_type": null,
"name": null,
"namespace": null,
"password": null,
"persist_config": null,
"proxy": null,
"proxy_headers": null,
"resource_definition": {
"apiVersion": "v1",
"data": {
"password": "VGFyLk1pZC5GdW4tNDU2",
"username": "YWRtaW4="
},
"kind": "Secret",
"metadata": {
"name": "bigip-login",
"namespace": "kube-system"
},
"type": "Opaque"
},
"src": null,
"state": "present",
"template": null,
"username": null,
"validate": null,
"validate_certs": null,
"wait": false,
"wait_condition": null,
"wait_sleep": 5,
"wait_timeout": 120
}
},
"msg": "Failed to import the required Python library (kubernetes) on master's Python /usr/bin/python3. Please read the module documentation and install it 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"
According to the example provided here that should've worked. I have also tried the following suggested (without any success) due to not having the json file provided here as an example:
---
apiVersion: v1
data:
password: dGVzdA==
username: YWRtaW4=
kind: Secret
metadata:
name: bigip-login
namespace: kube-system
type: Opaque
What intrigues me is the fact that both community/core kubernetes versions are currently installed:
marlon#ansible:~/.ansible$ ansible-galaxy collection install community.kubernetes
Process install dependency map
Starting collection install process
Skipping 'community.kubernetes' as it is already installed
marlon#ansible:~/.ansible$ ansible-galaxy collection install kubernetes.core
Process install dependency map
Starting collection install process
Skipping 'kubernetes.core' as it is already installed
marlon#ansible:~/.ansible$
Here is my python version that ansible is currently using:
marlon#ansible:~$ python3 --version
Python 3.8.10
marlon#ansible:~$ ansible --version | grep "python version"
python version = 3.8.10 (default, Sep 28 2021, 16:10:42) [GCC 9.3.0]
marlon#ansible:~$
Installed ubuntu like recommended on ansible installation file:
$ sudo apt update
$ sudo apt install software-properties-common
$ sudo add-apt-repository --yes --update ppa:ansible/ansible
$ sudo apt install ansible
Do you have any suggestions for use cases 1 and 2 so we can once and for all leave it here for future reference for others to benefit from them?
This error
"Failed to import the required Python library (kubernetes) on master's Python /usr/bin/python3.
means you don't have kubernetes module installed. Normally you could solve this problem by executing a command
pip3 install kubernetes
However, you are using an ansible, so you will have to take a different approach. Try to add this dependency to your system image. A similar question has already been asked here.
The problem was with a different module, but the procedure is the same for you as well.
You can find an example system image definition here. (Note, that this guy use Python 2 and your version is Python 3).
In your situation, you will have to put the command
pip3 install kubernetes
in your system image definition. If you are using the base system image, try to create your custom by adding the line as above. This Python dependency should be coded and installed into the image before it can be used by Ansible.

Openshift/Kubernetes: disoriented by the various configuration files

I'm learning openshift origin , in the master container I found a number of config files:
[root#openshift] cd /var/lib/origin
[root#openshift origin]# find . -name *kubeconfig
./openshift.local.config/node-localhost/node.kubeconfig
./openshift.local.config/master/admin.kubeconfig
./openshift.local.config/master/openshift-master.kubeconfig
[root#openshift origin]# find . -name *config.yaml
./openshift.local.config/node-localhost/node-config.yaml
./openshift.local.config/master/master-config.yaml
I found out these files also inspecting the origin container:
$ docker inspect 671fb8df3752 | grep config
"--master-config=/var/lib/origin/openshift.local.config/master/master-config.yaml",
"--node-config=/var/lib/origin/openshift.local.config/node-localhost/node-config.yaml"
"/var/lib/origin/openshift.local.config:/var/lib/origin/openshift.local.config:z",
"Source": "/var/lib/origin/openshift.local.config",
"Destination": "/var/lib/origin/openshift.local.config",
"KUBECONFIG=/var/lib/origin/openshift.local.config/master/admin.kubeconfig",
"--master-config=/var/lib/origin/openshift.local.config/master/master-config.yaml",
"--node-config=/var/lib/origin/openshift.local.config/node-localhost/node-config.yaml"
Could you help me to schematize / summarize the role and use of each of these files?
Specifically when executing commands of this type:
oadm policy add-scc-to-group anyuid system:authenticated --config=/var/lib/origin/openshift.local.config/master/admin.kubeconfig
they must be directed to each of the configurations I have found or only to the specific one?

jhipster kubectl - unable to decode " ": Object 'Kind' is missing

When running this command:
kubectl apply -f tenten
I get this error:
unable to decode "tenten\.angular-cli.json": Object 'Kind' is missing in '{
"project": {
"$schema": "./node_modules/#angular/cli/lib/config/schema.json",
"name": "tenten"
},
"apps": [{
"root": "src/main/webapp/",
"outDir": "target/www/app",
"assets": [
"content",
"favicon.ico"
],
"index": "index.html",
"main": "app/app.main.ts",
"polyfills": "app/polyfills.ts",
"test": "",
"tsconfig": "../../../tsconfig.json",
"prefix": "jhi",
"mobile": false,
"styles": [
"content/scss/vendor.scss",
"content/scss/global.scss"
],
"scripts": []
}],
It looks like you're running this from the parent directory of your applications. You should 1) create a directory that's parallel to your applications and 2) run yo jhipster:kubernetes in it. Then run kubectl apply -f tenten in that directory after you've built and pushed your docker images. For example, here's the output when I run it from the kubernetes directory in my jhipster-microservices-example project.
± yo jhipster:kubernetes
_-----_
| | ╭──────────────────────────────────────────╮
|--(o)--| │ Update available: 2.0.0 (current: 1.8.5) │
`---------´ │ Run npm install -g yo to update. │
( _´U`_ ) ╰──────────────────────────────────────────╯
/___A___\ /
| ~ |
__'.___.'__
´ ` |° ´ Y `
⎈ [BETA] Welcome to the JHipster Kubernetes Generator ⎈
Files will be generated in folder: /Users/mraible/dev/jhipster-microservices-example/kubernetes
WARNING! kubectl 1.2 or later is not installed on your computer.
Make sure you have Kubernetes installed. Read http://kubernetes.io/docs/getting-started-guides/binary_release/
Found .yo-rc.json config file...
? Which *type* of application would you like to deploy? Microservice application
? Enter the root directory where your gateway(s) and microservices are located ../
2 applications found at /Users/mraible/dev/jhipster-microservices-example/
? Which applications do you want to include in your configuration? (Press <space> to select, <a> to toggle all, <i> to i
nverse selection)blog, store
JHipster registry detected as the service discovery and configuration provider used by your apps
? Enter the admin password used to secure the JHipster Registry admin
? What should we use for the Kubernetes namespace? default
? What should we use for the base Docker repository name? mraible
? What command should we use for push Docker image to repository? docker push
Checking Docker images in applications' directories...
ls: no such file or directory: /Users/mraible/dev/jhipster-microservices-example/blog/target/docker/blog-*.war
identical blog/blog-deployment.yml
identical blog/blog-service.yml
identical blog/blog-postgresql.yml
identical blog/blog-elasticsearch.yml
identical store/store-deployment.yml
identical store/store-service.yml
identical store/store-mongodb.yml
conflict registry/jhipster-registry.yml
? Overwrite registry/jhipster-registry.yml? overwrite this and all others
force registry/jhipster-registry.yml
force registry/application-configmap.yml
WARNING! Kubernetes configuration generated with missing images!
To generate Docker image, please run:
./mvnw package -Pprod docker:build in /Users/mraible/dev/jhipster-microservices-example/blog
WARNING! You will need to push your image to a registry. If you have not done so, use the following commands to tag and push the images:
docker image tag blog mraible/blog
docker push mraible/blog
docker image tag store mraible/store
docker push mraible/store
You can deploy all your apps by running:
kubectl apply -f registry
kubectl apply -f blog
kubectl apply -f store
Use these commands to find your application's IP addresses:
kubectl get svc blog
See the end of my blog post Develop and Deploy Microservices with JHipster for more information.

sed not finding file when using a pattern through ansible

I have the following role:
---
- name: Replaces a string in a file
command: sed 's/'"{{ target_string }}"'/'"{{ new_string }}"'/g' -i {{ target_file_name }}
chdir="{{ target_file_location }}"
Which is called as follows:
- { role: string_replace_in_file, target_string: "localhost", new_string: "{{ myValue }}", target_file_name: "*.scripts.js", target_file_location: "/path/to/folder" }
The file i want to modify is aea342.scripts.js
I get the following output:
failed: [myMachine] => {"changed": true, "cmd": ["sed", "s/localhost/myValue/g", "-i", ".*.scripts.js"], "delta": "0:00:00.031107", "end": "2016-02-02 14:26:21.715652", "rc": 2, "start": "2016-02-02 14:26:21.684545", "warnings": ["Consider using template or lineinfile module rather than running sed"]}
stderr: sed: can't read .*.scripts.js: No such file or directory
When I run sed 's/localhost/myValue/g' -i *.scripts.js manually on my machine however it works.
Bruce is correct. Use shell to expand globs. I didn't test this. Can you try:
- name: Replaces a string in a file
shell: sed -i "s/<search>/<replace>/g" target_file_location/target_file_name(s)
From the Ansible documentation on the command module:
It will not be processed through the shell, so variables like $HOME and operations like "<", ">", "|", and "&" will not work
This also means that globs like "*.scripts.js" won't be expanded when you use the command module. If you need to use "*" then you should switch to using the shell module. As its name implies, it does run through a command shell so things like "*" will be properly expanded.