name: Activate Master node
hosts: all
tasks:
- name: Export2
shell: kubectl version | base64 | tr -d '\n'
register: output
#become: yes
- name: Exec all
shell: kubectl apply -f "https://cloud.weave.works/k8s/net?k8s-version={{ output.stdout }}"
~
I will execute this as root. but the last command will not execute with an error like this:
fatal: [185.255.91.121]: FAILED! => {"changed": true, "cmd": "kubectl apply -f \"https://cloud.weave.works/k8s/net?k8s-version=Q2xpZW50IFZlcnNpb246IHZlcnNpb24uSW5mb3tNYWpvcjoiMSIsIE1pbm9yOiIyMiIsIEdpdFZlcnNpb246InYxLjIyLjAiLCBHaXRDb21taXQ6ImMyYjUyMzdjY2Q5YzBmMWQ2MDBkMzA3MjYzNGNhNjZjZWZkZjI3MmYiLCBHaXRUcmVlU3RhdGU6ImNsZWFuIiwgQnVpbGREYXRlOiIyMDIxLTA4LTA0VDE4OjAzOjIwWiIsIEdvVmVyc2lvbjoiZ28xLjE2LjYiLCBDb21waWxlcjoiZ2MiLCBQbGF0Zm9ybToibGludXgvYW1kNjQifQo=\"", "delta": "0:00:00.077468", "end": "2021-08-08 07:17:20.846559", "msg": "non-zero return code", "rc": 1, "start": "2021-08-08 07:17:20.769091", "stderr": "The connection to the server localhost:8080 was refused - did you specify the right host or port?", "stderr_lines": ["The connection to the server localhost:8080 was refused - did you specify the right host or port?"], "stdout": "", "stdout_lines": []}
But I can execute it manually on the target server. what can be the reason?
Read the error message
I've read that error message, but the same command will execute manually What should be the reason?
Using which user when it works?
and I run playbook using -i root@myserver
Is there any clue?
Yes, your kubeconfig is not being picked up. Hence the localhost:8080 (the unconfigured default)
So why that command executes on target server manually?
Check your environment variables. Probably kubeconfig is setup for login shell but not for your ansible
Same thing applies. When the command runs as the Ansible user it is not using the kubeconfig. Fix that
Thats it, my playbook can not recognize KUBECONFIG inside .bashrc
That's because ansible isn't really doing things through the shell, it's transferring modules and executing scripts that it transfers, not using a login shell.
What should I do?
define KUBECONFIG environment variable in your playbook.
unreadable
Обсуждают сегодня