-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplaybook.yml
111 lines (93 loc) · 3.7 KB
/
playbook.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
- hosts: localhost
become: yes
become_method: sudo
become_user: root
vars:
ansible_python_interpreter: /home/ubuntu/k8s-ansible-venv/bin/python
docker_tag: "latest" # Change this to any tag you want
container_name: "abctechnologies-ansible"
image_name: "basharulsiddike/abctechnologies-ansible"
docker_username: "{{ lookup('env', 'DOCKER_USERNAME') }}"
docker_password: "{{ lookup('env', 'DOCKER_PASSWORD') }}"
kubeconfig_path: "{{ lookup('env', 'KUBECONFIG_PATH') }}"
deployment_file: './k8s_deployments/deployment.yml'
service_file: './k8s_deployments/service.yaml'
namespace: "abc-technologies-ansible" # Add your desired namespace here
docker_host: "unix:///var/run/docker.sock"
tasks:
- name: Debug kubeconfig path
debug:
msg: "Kubeconfig path is {{ kubeconfig_path }}"
- name: Log in to Docker Hub
command: echo "{{ docker_password }}" | docker login -u "{{ docker_username }}" --password-stdin
- name: Use Python from virtual environment
ansible.builtin.command: /home/ubuntu/k8s-ansible-venv/bin/pip install kubernetes packaging
- name: Check Python Kubernetes module
ansible.builtin.command: /home/ubuntu/k8s-ansible-venv/bin/python -c "import kubernetes"
- name: List installed packages
ansible.builtin.command: /home/ubuntu/k8s-ansible-venv/bin/pip list
- name: Check if Docker is already installed
command: docker --version
register: docker_installed
ignore_errors: true
- name: Install Docker
apt:
name: docker.io
state: present
when: docker_installed.rc != 0
- name: Start Docker Service
service:
name: docker
state: started
enabled: true
- name: Build WAR file using Maven (if applicable)
command: mvn clean package
args:
chdir: ./
when: docker_installed.rc == 0 # Run only if Docker is installed
- name: Build Docker Image
command: docker build -t {{ image_name }} .
- name: Stop existing Docker container if running
docker_container:
name: "{{ container_name }}"
state: absent
ignore_errors: true
- name: Remove existing Docker container if exists
docker_container:
name: "{{ container_name }}"
state: absent
ignore_errors: true
- name: Run Docker Container
docker_container:
name: "{{ container_name }}"
image: "{{ image_name }}"
state: started
published_ports:
- "9292:8080"
- name: Log in to Docker Hub
docker_login:
username: "{{ docker_username }}"
password: "{{ docker_password }}"
- name: Tag Docker image for Docker Hub
command: docker tag {{ image_name }} "{{ image_name }}:{{ docker_tag }}"
- name: Push Docker image to Docker Hub
command: docker push "{{ image_name }}:{{ docker_tag }}"
- name: Ensure Kubernetes namespace exists
kubernetes.core.k8s:
name: "{{ namespace }}"
state: present
kubeconfig: "{{ kubeconfig_path }}"
api_version: v1
kind: Namespace
- name: Apply Kubernetes Deployment
kubernetes.core.k8s:
state: present
kubeconfig: "{{ kubeconfig_path }}"
definition: "{{ lookup('file', deployment_file) }}"
namespace: "{{ namespace }}" # Specify the namespace here
- name: Apply Kubernetes Service
kubernetes.core.k8s:
state: present
kubeconfig: "{{ kubeconfig_path }}"
definition: "{{ lookup('file', service_file) }}"
namespace: "{{ namespace }}" # Specify the namespace here