mirror of
https://github.com/ansible-collections/kubernetes.core.git
synced 2026-03-27 13:53:03 +00:00
This is a backport of PR #971 as merged into main (027700c).
SUMMARY
Was going trough the list with issues and found 958; which seemed a quick fix.
What I fixed with with this PR:
Added support for copying files to init containers.
Fixed the format message when an exec is failing for a pod (the order was wrong).
Added a check if the container that you try to run copy for is started.
ISSUE TYPE
Bugfix Pull Request
COMPONENT NAME
copy.py module
ADDITIONAL INFORMATION
Some testing.
Verify that the pod does not exist:
kubectl -n default get pod/yorick
Output:
Error from server (NotFound): pods "yorick" not found
Run the playbook to create the file, create the deployment, wait for the init container to be ready, copy the created file to the init container, cat the copied file (using kubernetes.core.k8s_exec) that is now in the init container and try to copy the created file to the (not started) container (which fails - to see the new error message for it):
cat << EOF | ansible-playbook /dev/stdin
- hosts: localhost
gather_facts: False
tasks:
- ansible.builtin.copy:
content: |
Hi there
dest: /tmp/yorick.txt
- name: Deploy pod with initContainer with an unlimited while loop
kubernetes.core.k8s:
kubeconfig: "~/.kube/config"
definition:
apiVersion: v1
kind: Pod
metadata:
name: "yorick"
namespace: "default"
spec:
initContainers:
- name: "yorick-init"
image: busybox:latest
command: ["/bin/sh"]
args:
- "-c"
- |
echo "Init container started, waiting for file..."
# Wait for the file to be copied
while :;do
echo "Waiting for file"
sleep 5
done
echo "File received! Init container completing..."
containers:
- name: "yorick-container"
image: busybox:latest
command: ["/bin/sh"]
args:
- "-c"
- |
# Keep container running for testing
sleep 300
- kubernetes.core.k8s_info:
kubeconfig: "~/.kube/config"
api_version: v1
kind: Pod
name: "yorick"
namespace: "default"
register: pod_status
until: >-
pod_status.resources|length > 0
and 'initContainerStatuses' in pod_status.resources.0.status
and pod_status.resources.0.status.initContainerStatuses|length > 0
and pod_status.resources.0.status.initContainerStatuses.0.started|bool
- name: Copy /tmp/yorick.txt to the yorick-init init container
kubernetes.core.k8s_cp:
kubeconfig: "~/.kube/config"
namespace: default
pod: yorick
remote_path: /tmp/yorick.txt
local_path: /tmp/yorick.txt
container: yorick-init
- name: Execute a command
kubernetes.core.k8s_exec:
kubeconfig: "~/.kube/config"
namespace: default
pod: yorick
container: yorick-init
command: cat /tmp/yorick.txt
register: exec_out
- ansible.builtin.debug:
var: exec_out.stdout
- name: Try to copy /tmp/yorick.txt to the yorick-container container
kubernetes.core.k8s_cp:
kubeconfig: "~/.kube/config"
namespace: default
pod: yorick
remote_path: /tmp/yorick.txt
local_path: /tmp/yorick.txt
container: yorick-container
EOF
Output:
PLAY [localhost] ********************************************************************************************************************************************************************
TASK [ansible.builtin.copy] *********************************************************************************************************************************************************
Thursday 31 July 2025 02:01:21 +0200 (0:00:00.016) 0:00:00.016 *********
ok: [localhost]
TASK [Deploy pod with initContainer with an unlimited while loop] *******************************************************************************************************************
Thursday 31 July 2025 02:01:21 +0200 (0:00:00.788) 0:00:00.804 *********
changed: [localhost]
TASK [kubernetes.core.k8s_info] *****************************************************************************************************************************************************
Thursday 31 July 2025 02:01:25 +0200 (0:00:03.963) 0:00:04.768 *********
FAILED - RETRYING: [localhost]: kubernetes.core.k8s_info (3 retries left).
ok: [localhost]
TASK [Copy /tmp/yorick.txt to the yorick-init init container] ***********************************************************************************************************************
Thursday 31 July 2025 02:01:32 +0200 (0:00:06.598) 0:00:11.366 *********
changed: [localhost]
TASK [Execute a command] ************************************************************************************************************************************************************
Thursday 31 July 2025 02:01:39 +0200 (0:00:07.017) 0:00:18.383 *********
changed: [localhost]
TASK [ansible.builtin.debug] ********************************************************************************************************************************************************
Thursday 31 July 2025 02:01:40 +0200 (0:00:00.644) 0:00:19.028 *********
ok: [localhost] => {
"exec_out.stdout": "Hi there\n"
}
TASK [Try to copy /tmp/yorick.txt to the yorick-container container] ****************************************************************************************************************
Thursday 31 July 2025 02:01:40 +0200 (0:00:00.021) 0:00:19.050 *********
fatal: [localhost]: FAILED! => {
"changed": false
}
MSG:
Pod container yorick-container is not started
PLAY RECAP **************************************************************************************************************************************************************************
localhost : ok=6 changed=3 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
Playbook run took 0 days, 0 hours, 0 minutes, 21 seconds
Reviewed-by: Bianca Henderson <beeankha@gmail.com>