There weren’t a lot of resources out there to share how to execute commands or copy files using Ansible and the
kubectl Connection plugin. To help, I decided to document how to use the plugin. At least an example.
Usage is fairly easy. Top copy a file from your Ansible machine to the K8s pod you’ll need to have your
~/.kube/config file setup. Follow
kubectl instructions for that part (it can differ depending on your cloud).
In this example, I add the K8s pod to inventory, then copy a file to the K8s pod. I added a step that allows you to see what the file contains if you use
-vvv in the Ansible command
- hosts: localhost
- name: Token | Add k8s pod to inventory
- name: Token | Deploy testfile to K8s Pod
- name: Token | Get testfile from K8s Pod
command: cat /tmp/testfile.txt
There are additional parameters, which are listed here: https://docs.ansible.com/ansible/latest/plugins/connection/kubectl.html