-1

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)]
E_net4
  • 27,810
  • 13
  • 101
  • 139
João Pacheco
  • 67
  • 2
  • 10

2 Answers2

2

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.

E_net4
  • 27,810
  • 13
  • 101
  • 139
Akin Ozer
  • 1,001
  • 6
  • 14
  • Thanks, it worked!!! Is there any module, right now, that allows doing this in a more "agile "way? I mean, some k8s module from community.kubernetes? I've looked to them all and none of them seemed to have such function as copying files. – João Pacheco Jun 22 '20 at 00:59
  • More agile way would be not use this approach. You should check out configMap as file mount to pod: https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#populate-a-volume-with-data-stored-in-a-configmap – Akin Ozer Jun 23 '20 at 07:05
0

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" .

  • [Welcome to Stack Overflow](https://stackoverflow.com/tour)! This was posted as an answer, but it does not attempt to answer the question. It should possibly be an edit or a comment. – Alessio Jan 23 '21 at 18:13