Skip to content

Commit 12d3657

Browse files
committed
feat(ansible_kubernetes_installation): add preinstall role
1 parent 4aa2e1a commit 12d3657

1 file changed

Lines changed: 231 additions & 2 deletions

File tree

  • app/template_generators/ansible/install
Lines changed: 231 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,232 @@
1-
21
def ansible_kuber_install(input):
3-
pass
2+
3+
4+
kubernetes_ansible_port = input.ansible_port
5+
kubernetes_ansible_user = input.ansible_user
6+
k8s_master_nodes = input.k8s_master_nodes
7+
k8s_worker_nodes = input.k8s_worker_nodes
8+
lb_nodes = input.lb_nodes
9+
k8s_version = input.version
10+
sections = {
11+
"[all]": [f"{name} private_ip=x.x.x.x" for name in k8s_master_nodes + k8s_worker_nodes + lb_nodes],
12+
"[k8s]": k8s_master_nodes + k8s_worker_nodes,
13+
"[k8s_masters]": k8s_master_nodes,
14+
"[k8s_workers]": k8s_worker_nodes,
15+
"[lb]": lb_nodes,
16+
}
17+
kubernetes_inventory = "\n\n".join(f"{section}\n" + "\n".join(entries) for section, entries in sections.items())
18+
19+
inventory_hostname = "{{ inventory_hostname }}"
20+
21+
22+
23+
prompt = f"""
24+
Generate a Python code to generate an Ansible project (project name is app/media/MyAnsible)
25+
that dynamically provisions Ansible resources ensuring a modular, flexible structure. Only provide
26+
Python code, no explanations or markdown formatting, without ```python entry.
27+
The project should be organized as follows:
28+
29+
The structure of this project must be as follows:
30+
```
31+
├── ansible.cfg
32+
├── group_vars
33+
│   |── all
34+
│  
35+
├── hosts
36+
├── host_vars
37+
├── kubernetes_playbook.yml
38+
└── roles
39+
└── preinstall
40+
├── defaults
41+
│   └── main.yml
42+
├── files
43+
│   └── sample.sh
44+
├── handlers
45+
│   └── main.yml
46+
├── tasks
47+
│   └── basic.yml
48+
│   └── main.yml
49+
├── templates
50+
│   └── resolv.conf.j2
51+
└── vars
52+
└── main.yml
53+
```
54+
- The content of ansible.cfg must be as follows:
55+
```
56+
[defaults]
57+
host_key_checking=false
58+
```
59+
- group_vars directory includes a single file called "all" and the content of this file must be as follows:
60+
```
61+
# General
62+
install_ansible_modules: "true"
63+
disable_transparent_huge_pages: "true"
64+
setup_interface: "false"
65+
66+
# Network Calico see here for more details https://github.com/projectcalico/calico/releases
67+
calico_operator_url: "https://raw.githubusercontent.com/projectcalico/calico/v3.29.0/manifests/tigera-operator.yaml"
68+
calico_crd_url: "https://raw.githubusercontent.com/projectcalico/calico/v3.29.0/manifests/custom-resources.yaml"
69+
pod_network_cidr: "192.168.0.0/16"
70+
71+
# DNS
72+
resolv_nameservers: [8.8.8.8, 4.2.2.4] # 403.online
73+
74+
# Sanction shekan
75+
use_iran: "true" # change it to "false" if you are outside of iran
76+
77+
# Docker
78+
docker_gpg_key_url: "https://download.docker.com/linux/ubuntu/gpg"
79+
docker_gpg_key_path: "/etc/apt/keyrings/docker.gpg"
80+
docker_apt_repo: "https://download.docker.com/linux/ubuntu"
81+
82+
# Kubernetes
83+
kubernetes_gpg_keyring_path: "/etc/apt/keyrings/kubernetes-apt-keyring.gpg"
84+
kubernetes_gpg_key_url: "https://pkgs.k8s.io/core:/stable:/v{k8s_version}/deb/Release.key"
85+
kubernetes_apt_repo: "https://pkgs.k8s.io/core:/stable:/v{k8s_version}/deb/"
86+
k8s_version: "{k8s_version}.2" # see here https://kubernetes.io/releases/patch-releases/ and https://github.com/kubernetes/kubernetes/releases
87+
88+
# CRI
89+
cri_socket: unix:///var/run/containerd/containerd.sock
90+
91+
# VRRP and HAProxy
92+
interface_name: "enp0s8"
93+
virtual_ip: "192.168.178.100"
94+
haproxy_frontend_password: "password"
95+
96+
# Ansible Connection
97+
98+
ansible_user: {kubernetes_ansible_user}
99+
ansible_port: {kubernetes_ansible_port}
100+
ansible_python_interpreter: "/usr/bin/python3"
101+
domain="devopsgpt.com"
102+
apiserver_url="devopsgpt.com"
103+
```
104+
- there is file called "hosts" which its content must be as follows:
105+
```
106+
{kubernetes_inventory}
107+
```
108+
- There is an empty directory called "host_vars" with no files included
109+
- There is a file called "kubernetes_playbook.yml" which its content must be as follows:
110+
```
111+
- hosts: all
112+
roles:
113+
- role: preinstall
114+
gather_facts: yes
115+
any_errors_fatal: true
116+
tags: [preinstall]
117+
```
118+
- There is a directory called "roles" which a sub-directory called "preinstall" (roles/preinstall):
119+
"preinstall" has multiple sub-directories, so let's dive deeper into each its sub-directories:
120+
- (preinstall/tasks): This path has two files called "basic.yml" and "main.yml".
121+
122+
"(preinstall/tasks/basic.yml) must be as follows:"
123+
```
124+
- name: Set timezone to UTC
125+
timezone:
126+
name: Etc/UTC
127+
128+
- name: Set hostname
129+
command: hostnamectl set-hostname {inventory_hostname}
130+
131+
- name: Remove symlink resolve.conf
132+
file:
133+
path: "/etc/resolv.conf"
134+
state: absent
135+
ignore_errors: true
136+
when: use_iran == "true"
137+
138+
- name: Configure resolv.conf
139+
template:
140+
src: "resolv.conf.j2"
141+
dest: "/etc/resolv.conf"
142+
mode: "0644"
143+
when: use_iran == "true"
144+
145+
- name: Add hostname
146+
lineinfile:
147+
path: /etc/hosts
148+
regexp: '^127\.0\.0\.1'
149+
line: "127.0.0.1 {inventory_hostname} localhost"
150+
owner: root
151+
group: root
152+
mode: 0644
153+
154+
- name: Install necessary tools
155+
apt:
156+
update_cache: true
157+
name:
158+
- vim
159+
- sudo
160+
- wget
161+
- curl
162+
- telnet
163+
- nload
164+
- s3cmd
165+
- cron
166+
- ipset
167+
- lvm2
168+
- python3
169+
- python3-setuptools
170+
- python3-pip
171+
- python3-apt
172+
- intel-microcode
173+
- htop
174+
- tcpdump
175+
- net-tools
176+
- screen
177+
- tmux
178+
- byobu
179+
- iftop
180+
- bmon
181+
- iperf
182+
- sysstat
183+
- ethtool
184+
- plocate
185+
- thin-provisioning-tools
186+
- conntrack
187+
- stress
188+
- cpufrequtils
189+
- rsync
190+
- xz-utils
191+
- build-essential
192+
- apt-transport-https
193+
- ca-certificates
194+
- software-properties-common
195+
- gnupg-agent
196+
- iptables-persistent
197+
- open-iscsi
198+
- nfs-common
199+
- tzdata
200+
- tree
201+
state: latest
202+
203+
- name: Fix broken packages
204+
apt:
205+
state: fixed
206+
```
207+
208+
"(preinstall/tasks/main.yml) must be as follows:"
209+
```
210+
---
211+
- name: basic setup
212+
include_tasks: basic.yml
213+
```
214+
finally just give me a python code without any note that can generate a project folder with the
215+
given schema without ```python entry. and we dont need any base directory in the python code.
216+
the final ansible template must work very well without any error!
217+
218+
the python code you give me, must have structure like that:
219+
220+
import os
221+
project_name = "app/media/MyAnsible"
222+
foo_dir = os.path.join(project_name, "bar")
223+
x_dir = os.path.join(modules_dir, "y")
224+
225+
# Create project directories
226+
os.makedirs(ansible_dir, exist_ok=True)
227+
228+
# Create main.tf
229+
with open(os.path.join(project_name, "main.tf"), "w") as main_file:
230+
# any thing you need
231+
"""
232+
return prompt

0 commit comments

Comments
 (0)