I had to install kubectl via ansible and I spent some time to create an Ansible script following the official Kubectl linux installation instruction
This is working for me both using Ansible via command line and using Packer tool to create AWS AMIs:
---
- name: Get latest kubectl version
uri:
url: https://dl.k8s.io/release/stable.txt
return_content: yes
register: version
- name: create kubectl versioned directory
file:
path: /opt/kubectl-{{ version.content }}
state: directory
become: true
- name: Download the latest kubectl release
uri:
url: https://dl.k8s.io/release/{{ version.content }}/bin/linux/amd64/kubectl
dest: /opt/kubectl-{{ version.content }}
register: kubectl
become: true
- name: Download the kubectl checksum
uri:
url: https://dl.k8s.io/release/{{ version.content }}/bin/linux/amd64/kubectl.sha256
dest: /opt/kubectl-{{ version.content }}
register: kubectl
become: true
- name: Get kubectl sha256sum
shell: sha256sum /opt/kubectl-{{ version.content }}/kubectl | cut -d " " -f1
register: file_shasum
- set_fact:
shasum1={{ file_shasum.stdout }}
- debug: var=shasum1
run_once: true
- name: get sha256sum value from file
command: cat /opt/kubectl-{{ version.content }}/kubectl.sha256
register: downloaded_shasum
- set_fact:
shasum2={{ downloaded_shasum.stdout }}
- debug: var=shasum2
run_once: true
- name: Assert that the kubectl binary is OK
assert:
that:
- file_shasum.stdout == downloaded_shasum.stdout
fail_msg: "Shasum does not correspond"
success_msg: "kubectl shasum verified: ok"
- name: Change kubectl file permission
file:
path: "/opt/kubectl-{{ version.content }}/kubectl"
mode: '0755'
become: true
- name: create a symlink to kubectl
file:
src: "/opt/kubectl-{{ version.content }}/kubectl"
dest: "/usr/bin/kubectl"
state: link
become: true
Any comment appreciated.
Answer from Daniele Scarano on Stack OverflowI had to install kubectl via ansible and I spent some time to create an Ansible script following the official Kubectl linux installation instruction
This is working for me both using Ansible via command line and using Packer tool to create AWS AMIs:
---
- name: Get latest kubectl version
uri:
url: https://dl.k8s.io/release/stable.txt
return_content: yes
register: version
- name: create kubectl versioned directory
file:
path: /opt/kubectl-{{ version.content }}
state: directory
become: true
- name: Download the latest kubectl release
uri:
url: https://dl.k8s.io/release/{{ version.content }}/bin/linux/amd64/kubectl
dest: /opt/kubectl-{{ version.content }}
register: kubectl
become: true
- name: Download the kubectl checksum
uri:
url: https://dl.k8s.io/release/{{ version.content }}/bin/linux/amd64/kubectl.sha256
dest: /opt/kubectl-{{ version.content }}
register: kubectl
become: true
- name: Get kubectl sha256sum
shell: sha256sum /opt/kubectl-{{ version.content }}/kubectl | cut -d " " -f1
register: file_shasum
- set_fact:
shasum1={{ file_shasum.stdout }}
- debug: var=shasum1
run_once: true
- name: get sha256sum value from file
command: cat /opt/kubectl-{{ version.content }}/kubectl.sha256
register: downloaded_shasum
- set_fact:
shasum2={{ downloaded_shasum.stdout }}
- debug: var=shasum2
run_once: true
- name: Assert that the kubectl binary is OK
assert:
that:
- file_shasum.stdout == downloaded_shasum.stdout
fail_msg: "Shasum does not correspond"
success_msg: "kubectl shasum verified: ok"
- name: Change kubectl file permission
file:
path: "/opt/kubectl-{{ version.content }}/kubectl"
mode: '0755'
become: true
- name: create a symlink to kubectl
file:
src: "/opt/kubectl-{{ version.content }}/kubectl"
dest: "/usr/bin/kubectl"
state: link
become: true
Any comment appreciated.
You're on the right track. We install Rundeck, which follows similar steps, using the following Ansible code:
- name: add rundeck apt repository key
become: true
apt_key:
url: https://bintray.com/user/downloadSubjectPublicKey?username=bintray
- name: add rundeck apt repository
become: true
apt_repository:
repo: 'deb https://rundeck.bintray.com/rundeck-deb /'
filename: rundeck
- name: install rundeck dependencies
become: true
apt:
name: openjdk-8-jdk
- name: install rundeck and rundeck-cli
become: true
apt:
name: "{{ item }}"
loop:
- rundeck
- rundeck-cli
Use that as an example and you should be good to go. The apt_repository module runs apt-get update automatically when new repositories are added.
kubernetes - how to run cmd kubectl apply using Ansible properly - Stack Overflow
How to setup ansible playbook that is able to execute kubectl (kubernetes) commands - Stack Overflow
Error setting up kubernetes cluster
Ansible and Kubectl Commands
Videos
The best way to do this would be to use ansible kubernetes.core collection
An example with file:
- name: Create a Deployment by reading the definition from a local file
kubernetes.core.k8s:
state: present
src: /testing/deployment.yml
So, you could loop from different folders containing the yaml definitions for your objects with state: present
I don't currently have a running kube cluster to test this against but you should basically be able to run all this in a single task with a loop using the kubernetes.core.k8s module
Here is what I believe should meet your requirement (provided your access to your kube instance is configured and ok in your environment and that you installed the above collection as described in the documentation)
- name: install my kube objects
hosts: localhost
gather_facts: false
vars:
obj_def_path: /path/to/your/obj_def_dir/
obj_def_list:
- food-pv.yaml
- bar-pv.yaml
- foo.yaml
- bar.yaml
- this-service.yaml
- that-nodeport.yaml
- something.yaml
tasks:
- name: Install all objects from def files
k8s:
src: "{{ obj_def_path }}/{{ item }}"
state: present
apply: true
loop: "{{ obj_def_list }}"
I would like to utilise kubectl connection plugin: https://docs.ansible.com/ansible/latest/plugins/connection/kubectl.html but having struggle to figure out how to actually do that.
The fine manual describes how one uses connection plugins, and while it is possible to use in in tasks, that is unlikely to make any sense unless your inventory started with Pods.
The way I have seen that connection used is to start by identifying the Pods against which you might want to take action, and then run a playbook against a unique group for that purpose:
- hosts: all
tasks:
- set_fact:
# this is *just an example for brevity*
# in reality you would use `k8s:` or `kubectl get -o name pods -l my-selector=my-value` to get the pod names
pod_names:
- nginx-12345
- nginx-3456
- add_host:
name: '{{ item }}'
groups:
- my-pods
with_items: '{{ pod_names }}'
- hosts: my-pods
connection: kubectl
tasks:
# and now you are off to the races
- command: ps -ef
# watch out if the Pod doesn't have a working python installed
# as you will have to use raw: instead
# (and, of course, disable "gather_facts: no")
- raw: ps -ef
First install k8s collections
ansible-galaxy collection install community.kubernetes
and here is play-book, it will sort all pods and run a command in every pod
---
-
hosts: localhost
vars_files:
- vars/main.yaml
collections:
- community.kubernetes
tasks:
-
name: Get the pods in the specific namespace
k8s_info:
kubeconfig: '{{ k8s_kubeconfig }}'
kind: Pod
namespace: test
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')}}"
-
k8s_exec:
kubeconfig: '{{ k8s_kubeconfig }}'
namespace: "{{ namespace }}"
pod: "{{ item.metadata.name }}"
command: apt update
with_items: "{{ pod_list.resources }}"
register: exec
loop_control:
label: "{{ item.metadata.name }}"