Skip to content

Commit ba1d48e

Browse files
committed
fix(ansible_base): add ansible base
1 parent 8dd8174 commit ba1d48e

9 files changed

Lines changed: 251 additions & 4 deletions

File tree

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import os
2+
3+
project_name = "app/media/MyAnsible"
4+
5+
# Define directory structure
6+
ansible_dir = project_name
7+
group_vars_dir = os.path.join(ansible_dir, "group_vars")
8+
hosts_file = os.path.join(ansible_dir, "hosts")
9+
host_vars_dir = os.path.join(ansible_dir, "host_vars")
10+
nginx_playbook_file = os.path.join(ansible_dir, "nginx_playbook.yml")
11+
roles_dir = os.path.join(ansible_dir, "roles")
12+
install_nginx_dir = os.path.join(roles_dir, "install_nginx")
13+
defaults_dir = os.path.join(install_nginx_dir, "defaults")
14+
handlers_dir = os.path.join(install_nginx_dir, "handlers")
15+
tasks_dir = os.path.join(install_nginx_dir, "tasks")
16+
templates_dir = os.path.join(install_nginx_dir, "templates")
17+
vars_dir = os.path.join(install_nginx_dir, "vars")
18+
19+
# Create project directories
20+
os.makedirs(group_vars_dir, exist_ok=True)
21+
os.makedirs(host_vars_dir, exist_ok=True)
22+
os.makedirs(tasks_dir, exist_ok=True)
23+
os.makedirs(defaults_dir, exist_ok=True)
24+
os.makedirs(handlers_dir, exist_ok=True)
25+
os.makedirs(templates_dir, exist_ok=True)
26+
os.makedirs(vars_dir, exist_ok=True)
27+
os.makedirs(roles_dir, exist_ok=True)
28+
os.makedirs(install_nginx_dir, exist_ok=True)
29+
30+
# Create ansible.cfg
31+
with open(os.path.join(ansible_dir, "ansible.cfg"), "w") as ansible_config:
32+
ansible_config.write("[defaults]\n")
33+
ansible_config.write("host_key_checking=false\n")
34+
35+
# Create group_vars/nginx_nodes
36+
with open(os.path.join(group_vars_dir, "nginx_nodes"), "w") as nginx_nodes:
37+
nginx_nodes.write("ansible_port: 22\n")
38+
nginx_nodes.write("ansible_user: root\n")
39+
40+
# Create hosts file
41+
with open(hosts_file, "w") as hosts:
42+
hosts.write("[nginx_nodes]\n")
43+
hosts.write("www.examppple.com\n")
44+
45+
# Create nginx_playbook.yml
46+
with open(nginx_playbook_file, "w") as playbook:
47+
playbook.write("- hosts: all\n")
48+
playbook.write(" roles:\n")
49+
playbook.write(" - install_nginx\n")
50+
51+
# Create install_nginx/tasks/main.yml
52+
with open(os.path.join(tasks_dir, "main.yml"), "w") as tasks:
53+
tasks.write("---\n")
54+
tasks.write("- name: Install CA certificates to ensure HTTPS connections work\n")
55+
tasks.write(" apt:\n")
56+
tasks.write(" name: ca-certificates\n")
57+
tasks.write(" state: present\n\n")
58+
59+
tasks.write("- name: Add Nginx signing key\n")
60+
tasks.write(" apt_key:\n")
61+
tasks.write(" url: \"{ nginx_repo_key_url }\"\n")
62+
tasks.write(" state: present\n\n")
63+
64+
tasks.write("- name: Add Nginx repository\n")
65+
tasks.write(" apt_repository:\n")
66+
tasks.write(" repo: \"deb { nginx_repo_url } { ansible_distribution_release } nginx\"\n")
67+
tasks.write(" state: present\n")
68+
tasks.write(" filename: nginx\n\n")
69+
70+
tasks.write("- name: Update apt cache\n")
71+
tasks.write(" apt:\n")
72+
tasks.write(" update_cache: yes\n\n")
73+
74+
tasks.write("- name: Install specific version of Nginx\n")
75+
tasks.write(" apt:\n")
76+
tasks.write(" name: \"nginx={ nginx_version }~{ ansible_distribution_release }\"\n")
77+
tasks.write(" state: present\n\n")
78+
79+
tasks.write("- name: Ensure Nginx service is running and enabled\n")
80+
tasks.write(" service:\n")
81+
tasks.write(" name: nginx\n")
82+
tasks.write(" state: started\n")
83+
tasks.write(" enabled: yes\n")
84+
85+
# Create install_nginx/vars/main.yml
86+
with open(os.path.join(vars_dir, "main.yml"), "w") as vars_file:
87+
vars_file.write("nginx_repo_key_url: \"https://nginx.org/keys/nginx_signing.key\"\n")
88+
vars_file.write("nginx_repo_url: \"http://nginx.org/packages/mainline/ubuntu/\"\n")
89+
vars_file.write("nginx_version: \"1.23.4-1\"\n")

app/media/MyAnsible/ansible.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[defaults]
2+
host_key_checking=false
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ansible_port: 22
2+
ansible_user: root

app/media/MyAnsible/hosts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[nginx_nodes]
2+
www.examppple.com
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
- hosts: all
2+
roles:
3+
- install_nginx
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
- name: Install CA certificates to ensure HTTPS connections work
3+
apt:
4+
name: ca-certificates
5+
state: present
6+
7+
- name: Add Nginx signing key
8+
apt_key:
9+
url: "{ nginx_repo_key_url }"
10+
state: present
11+
12+
- name: Add Nginx repository
13+
apt_repository:
14+
repo: "deb { nginx_repo_url } { ansible_distribution_release } nginx"
15+
state: present
16+
filename: nginx
17+
18+
- name: Update apt cache
19+
apt:
20+
update_cache: yes
21+
22+
- name: Install specific version of Nginx
23+
apt:
24+
name: "nginx={ nginx_version }~{ ansible_distribution_release }"
25+
state: present
26+
27+
- name: Ensure Nginx service is running and enabled
28+
service:
29+
name: nginx
30+
state: started
31+
enabled: yes
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
nginx_repo_key_url: "https://nginx.org/keys/nginx_signing.key"
2+
nginx_repo_url: "http://nginx.org/packages/mainline/ubuntu/"
3+
nginx_version: "1.23.4-1"

app/models/ansible_models.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
from typing import List, Optional
22
from pydantic import BaseModel, validator, ValidationError
33

4-
4+
class AnsibleBase(BaseModel):
5+
ansible_user:str = 'root'
6+
ansible_port:int = 22
57

6-
class AnsibleInstallNginx(BaseModel):
8+
class AnsibleInstallNginx(AnsibleBase):
79

810
os: str = 'ubuntu'
911
hosts:List[str] = ['www.example.com']
@@ -17,7 +19,7 @@ def validator_os(cls, value):
1719
return value
1820

1921

20-
class AnsibleInstallDocker(BaseModel):
22+
class AnsibleInstallDocker(AnsibleBase):
2123
os: str = 'ubuntu'
2224
hosts:List[str] = ['www.example.com']
2325
version:str = 'latest'

app/template_generators/ansible/install/nginx.py

Lines changed: 114 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,122 @@
11

22
def ansible_nginx_install_ubuntu(input):
3-
prompt = ""
3+
nginx_hosts = input.hosts
4+
inventory = (f"[nginx_nodes]\n" + "\n".join(nginx_hosts))
5+
nginx_version = input.version
6+
7+
prompt = f"""
8+
Generate a Python code to generate an Ansible project (project name is app/media/MyAnsible)
9+
that dynamically provisions Ansible resources ensuring a modular, flexible structure. Only provide
10+
Python code, no explanations or markdown formatting. The project should be organized as follows:
11+
12+
The structure of this project must be as follows:
13+
```
14+
├── ansible.cfg
15+
├── group_vars
16+
│   |── nginx_nodes
17+
│  
18+
├── hosts
19+
├── host_vars
20+
├── nginx_playbook.yml
21+
└── roles
22+
└── install_nginx
23+
├── defaults
24+
│   └── main.yml
25+
├── handlers
26+
│   └── main.yml
27+
├── tasks
28+
│   └── main.yml
29+
├── templates
30+
│   └── main.yml
31+
└── vars
32+
└── main.yml
33+
```
34+
- The content of ansible.cfg must be as follows:
35+
```
36+
[defaults]
37+
host_key_checking=false
38+
```
39+
- group_vars directory includes a single file called "nginx_nodes" and the content of this file must be as follows:
40+
```
41+
ansible_port: 22
42+
ansible_user: root
43+
```
44+
- there is file called "hosts" which its content must be as follows:
45+
```
46+
{inventory}
47+
```
48+
- There is an empty directory called "host_vars" with no files included
49+
- There is a file called "nginx_playbook.yml" which its content must be as follows:
50+
```
51+
- hosts: all
52+
roles:
53+
- install_nginx
54+
```
55+
- There is a directory called "roles" which a sub-directory called "install_nginx" (roles/install_nginx)
56+
"install_nginx" has multiple sub-directories, so let's dive deeper into each its sub-directories:
57+
- (install_nginx/tasks): This path has a file called "main.yml" which its content must be as follows:
58+
```
59+
---
60+
- name: Install CA certificates to ensure HTTPS connections work
61+
apt:
62+
name: ca-certificates
63+
state: present
64+
65+
- name: Add Nginx signing key
66+
apt_key:
67+
url: "{{ nginx_repo_key_url }}"
68+
state: present
69+
70+
- name: Add Nginx repository
71+
apt_repository:
72+
repo: "deb {{ nginx_repo_url }} {{ ansible_distribution_release }} nginx"
73+
state: present
74+
filename: nginx
75+
76+
- name: Update apt cache
77+
apt:
78+
update_cache: yes
79+
80+
- name: Install specific version of Nginx
81+
apt:
82+
name: "nginx={{ nginx_version }}~{{ ansible_distribution_release }}"
83+
state: present
84+
85+
- name: Ensure Nginx service is running and enabled
86+
service:
87+
name: nginx
88+
state: started
89+
enabled: yes
90+
```
91+
- (install_nginx/vars): This path has a file called "main.yml" which its content must be as follows:
92+
```
93+
nginx_repo_key_url: "https://nginx.org/keys/nginx_signing.key"
94+
nginx_repo_url: "http://nginx.org/packages/mainline/ubuntu/"
95+
nginx_version: "1.23.4-1"
96+
```
97+
98+
finally just give me a python code without any note that can generate a project folder with the given
99+
schema without ```python entry. and we dont need any base directory in the python code. the final
100+
terraform template must work very well without any error!
101+
102+
the python code you give me, must have structure like that:
103+
104+
import os
105+
project_name = "app/media/MyAnsible"
106+
foo_dir = os.path.join(project_name, "bar")
107+
x_dir = os.path.join(modules_dir, "y")
108+
109+
# Create project directories
110+
os.makedirs(ansible_dir, exist_ok=True)
111+
112+
# Create main.tf
113+
with open(os.path.join(project_name, "main.tf"), "w") as main_file:
114+
# any thing you need
115+
"""
4116
return prompt
5117

6118

119+
7120
def ansible_nginx_install(input):
8121

9122
if input.os == 'ubuntu':

0 commit comments

Comments
 (0)