I am trying to copy a test file (e.g. HelloWorld.txt) from my local machine to a k8s pod on Rancher through Ansible. The thing is that I am getting a cat: no such file or directory. I think I am getting this error because I am using the k8s_exec module to execute the cp command. And since I am using that, I think he is trying to make a copy not from my local machine to the pod but already inside the pod.
Here is the playbook:
---
- hosts: localhost #group of hosts on host file
connection: local
remote_user: root
vars:
ansible_python_interpreter: '{{ ansible_playbook_python }}'
collections:
- community.kubernetes
tasks:
- name: Test Host Connection
ping:
- 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')}}"
- name: Copy Opatch zipfile to the Target Oracle_home
k8s_exec:
kubeconfig: '/etc/ansible/RCCloudConfig'
namespace: redmine
pod: "{{ pod_name | mandatory }}" #pod name
command: cp /home/ansible/ansible/HelloWorld.txt /tmp # copy from the current location to tmp
- name: Show HelloWorld
k8s_exec:
kubeconfig: '/etc/ansible/RCCloudConfig'
namespace: redmine
pod: redminetisl-gitlab-69f7485b5d-52b5s #pod name
command: cat /tmp/HelloWorld.txt
Ansible version:
ansible 2.9.9
config file = /etc/ansible/ansible.cfg
configured module search path = ['/home/ansible/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3.6/site-packages/ansible
executable location = /usr/bin/ansible
python version = 3.6.8 (default, Apr 16 2020, 01:36:27) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)]
I solved this problem by exit the container,and then cp the file into the container. The problem is that I dont have the permission,so I need to quit the container ,and then type "sudo docker cp {file url} {containerID}:/opt/video" .
Yes your guess is true, when you exec
into container your command will be executed "inside" pod. In this case you'll be just copying file inside the pod which is not present.
Check out kubectl cp command. This is similar to docker cp
command, which is copying file from host to container. You can use ansible command module
to execute it.