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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
--- - hosts: localhost tasks: - name: Token | Add k8s pod to inventory add_host: name: node-0-0 ansible_connection: kubectl ansible_kubectl_namespace: 2071296ed6284f5f801f8dd52c9c7779 - name: Token | Deploy testfile to K8s Pod copy: src: /home/eanderson/testfile.txt dest: /tmp/testfile.txt delegate_to: node-0-0 - name: Token | Get testfile from K8s Pod command: cat /tmp/testfile.txt delegate_to: node-0-0 |
There are additional parameters, which are listed here: https://docs.ansible.com/ansible/latest/plugins/connection/kubectl.html