RedHat Open-shift and Kubernetes Automation - Ansible Approach
Hi Folks,
So, I here is a brief POC that I was doing to automate the open-shift based test cases using Ansible.
We are using kubernetes client in Ansible.
Steps for installation
>yum install python3 -y
>yum install curl -y
>curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
>python get-pip.py
(you can install ansible using the pip3 or subscription-manager on rhel)
(On control node in my case it was the rhel infra node, from which i was
accessing rest of the ocp cluster nodes)
> ssh-copy-id key to self node(your control node or localhost or infra node , from where you are accessing rest of your cluster nodes.)
> ssh root@<controlnode> (one time setup, for accessing and accepting the 'yes' prompt.)
Note: Rest of the ansible warning can be removed by providing appropriate setting in ansible.cfg file which can be created in the working directory manually if it is not created at the time of ansible installation.
Create hosts file in the working directory as below:
[server]
localhost
<hostname of control node>
Example 1:
Now, create playbook1.yaml file and paste following content:
---
- name: Network Getting Started First Playbook
hosts: all
connection: local
become: true
become_user: root
tasks:
- name: Connect to worker pod and fetching cluster definition and pod states.
kubernetes.core.k8s_exec:
namespace: ibm-spectrum-scale
container: gpfs
pod: worker0
command: "{{ item }}"
with_items:
- mmlscluster
- mmgetstate -a
register: command_output
- name: Cluster definition and Pod states output.
debug:
msg: "{{command_output}}"
- name: Validating pod states.
assert:
that:
- "'worker0' and 'active' in command_output.results[1].stdout"
- "'worker1 active' in command_output.results[1].stdout"
- "'worker2 active' in command_output.results[1].stdout"
- "'worker3 active' in command_output.results[1].stdout"
fail_msg: "Testcase Failed."
success_msg: "Testcase Passed."
register: Assert_out
- name: Output Assertion
debug:
msg: "{{Assert_out}}"
...
Run the command using ansible-playbook.
Ansible-playbook playbook1.yaml
--------------------------------------------------------
Kudos! You have setup and ran first ansible playbook with assertions. Similarly you can explore more.
try another example:
Example 2
---
- name: Second Playbook
hosts: all
connection: local
become: true
become_user: root
tasks:
- name: File creation on worker pod.
kubernetes.core.k8s_exec:
namespace: ibm-spectrum-scale
container: gpfs
pod: worker0
command: "{{ item }}"
with_items:
- cd /
- pwd
- mkdir test_demo
- touch /test_demo/test_demo4.txt
- ls
register: command_output
- name: Command output
debug:
msg: "{{command_output}}"
# copy content into a file in the remote pod
- name: Copy content to file inside the pod
kubernetes.core.k8s_cp:
state: to_pod
namespace: ibm-spectrum-scale
container: gpfs
pod: worker0
remote_path: /test_demo/test_demo4.txt
content: "This content will be copied into remote file"
- name: Cleanup for files.
kubernetes.core.k8s_exec:
namespace: ibm-spectrum-scale
container: gpfs
pod: worker0
command: "{{ item }}"
with_items:
- cd /
- pwd
- cd /test_demo
- rm -rf /test_demo/test_demo4.txt
- pwd
- ls
register: cleanup_step
- name: Cleanup output.
debug:
msg: "{{cleanup_step}}"
...
kudos again!!!! you have automated the tasks for accessing and writing inside pod.
Video link
https://youtu.be/C7dU_v_EXTw
will keep posting more content.
References:
https://github.com/ansible-collections/kubernetes.core/blob/main/docs/kubernetes.core.k8s_exec_module.rst
https://github.com/ansible-collections/kubernetes.core/blob/main/docs/kubernetes.core.k8s_module.rst
https://github.com/ansible-collections/kubernetes.core/blob/main/docs/kubernetes.core.k8s_cp_module.rst
Comments
Post a Comment