Skip to content

Commit

Permalink
fix(ansible_base): add ansible base
Browse files Browse the repository at this point in the history
  • Loading branch information
abolfazl8131 committed Nov 27, 2024
1 parent 8dd8174 commit ba1d48e
Show file tree
Hide file tree
Showing 9 changed files with 251 additions and 4 deletions.
89 changes: 89 additions & 0 deletions app/directory_generators/ansible_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import os

project_name = "app/media/MyAnsible"

# Define directory structure
ansible_dir = project_name
group_vars_dir = os.path.join(ansible_dir, "group_vars")
hosts_file = os.path.join(ansible_dir, "hosts")
host_vars_dir = os.path.join(ansible_dir, "host_vars")
nginx_playbook_file = os.path.join(ansible_dir, "nginx_playbook.yml")
roles_dir = os.path.join(ansible_dir, "roles")
install_nginx_dir = os.path.join(roles_dir, "install_nginx")
defaults_dir = os.path.join(install_nginx_dir, "defaults")
handlers_dir = os.path.join(install_nginx_dir, "handlers")
tasks_dir = os.path.join(install_nginx_dir, "tasks")
templates_dir = os.path.join(install_nginx_dir, "templates")
vars_dir = os.path.join(install_nginx_dir, "vars")

# Create project directories
os.makedirs(group_vars_dir, exist_ok=True)
os.makedirs(host_vars_dir, exist_ok=True)
os.makedirs(tasks_dir, exist_ok=True)
os.makedirs(defaults_dir, exist_ok=True)
os.makedirs(handlers_dir, exist_ok=True)
os.makedirs(templates_dir, exist_ok=True)
os.makedirs(vars_dir, exist_ok=True)
os.makedirs(roles_dir, exist_ok=True)
os.makedirs(install_nginx_dir, exist_ok=True)

# Create ansible.cfg
with open(os.path.join(ansible_dir, "ansible.cfg"), "w") as ansible_config:
ansible_config.write("[defaults]\n")
ansible_config.write("host_key_checking=false\n")

# Create group_vars/nginx_nodes
with open(os.path.join(group_vars_dir, "nginx_nodes"), "w") as nginx_nodes:
nginx_nodes.write("ansible_port: 22\n")
nginx_nodes.write("ansible_user: root\n")

# Create hosts file
with open(hosts_file, "w") as hosts:
hosts.write("[nginx_nodes]\n")
hosts.write("www.examppple.com\n")

# Create nginx_playbook.yml
with open(nginx_playbook_file, "w") as playbook:
playbook.write("- hosts: all\n")
playbook.write(" roles:\n")
playbook.write(" - install_nginx\n")

# Create install_nginx/tasks/main.yml
with open(os.path.join(tasks_dir, "main.yml"), "w") as tasks:
tasks.write("---\n")
tasks.write("- name: Install CA certificates to ensure HTTPS connections work\n")
tasks.write(" apt:\n")
tasks.write(" name: ca-certificates\n")
tasks.write(" state: present\n\n")

tasks.write("- name: Add Nginx signing key\n")
tasks.write(" apt_key:\n")
tasks.write(" url: \"{ nginx_repo_key_url }\"\n")
tasks.write(" state: present\n\n")

tasks.write("- name: Add Nginx repository\n")
tasks.write(" apt_repository:\n")
tasks.write(" repo: \"deb { nginx_repo_url } { ansible_distribution_release } nginx\"\n")
tasks.write(" state: present\n")
tasks.write(" filename: nginx\n\n")

tasks.write("- name: Update apt cache\n")
tasks.write(" apt:\n")
tasks.write(" update_cache: yes\n\n")

tasks.write("- name: Install specific version of Nginx\n")
tasks.write(" apt:\n")
tasks.write(" name: \"nginx={ nginx_version }~{ ansible_distribution_release }\"\n")
tasks.write(" state: present\n\n")

tasks.write("- name: Ensure Nginx service is running and enabled\n")
tasks.write(" service:\n")
tasks.write(" name: nginx\n")
tasks.write(" state: started\n")
tasks.write(" enabled: yes\n")

# Create install_nginx/vars/main.yml
with open(os.path.join(vars_dir, "main.yml"), "w") as vars_file:
vars_file.write("nginx_repo_key_url: \"https://nginx.org/keys/nginx_signing.key\"\n")
vars_file.write("nginx_repo_url: \"http://nginx.org/packages/mainline/ubuntu/\"\n")
vars_file.write("nginx_version: \"1.23.4-1\"\n")
2 changes: 2 additions & 0 deletions app/media/MyAnsible/ansible.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[defaults]
host_key_checking=false
2 changes: 2 additions & 0 deletions app/media/MyAnsible/group_vars/nginx_nodes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ansible_port: 22
ansible_user: root
2 changes: 2 additions & 0 deletions app/media/MyAnsible/hosts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[nginx_nodes]
www.examppple.com
3 changes: 3 additions & 0 deletions app/media/MyAnsible/nginx_playbook.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- hosts: all
roles:
- install_nginx
31 changes: 31 additions & 0 deletions app/media/MyAnsible/roles/install_nginx/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
- name: Install CA certificates to ensure HTTPS connections work
apt:
name: ca-certificates
state: present

- name: Add Nginx signing key
apt_key:
url: "{ nginx_repo_key_url }"
state: present

- name: Add Nginx repository
apt_repository:
repo: "deb { nginx_repo_url } { ansible_distribution_release } nginx"
state: present
filename: nginx

- name: Update apt cache
apt:
update_cache: yes

- name: Install specific version of Nginx
apt:
name: "nginx={ nginx_version }~{ ansible_distribution_release }"
state: present

- name: Ensure Nginx service is running and enabled
service:
name: nginx
state: started
enabled: yes
3 changes: 3 additions & 0 deletions app/media/MyAnsible/roles/install_nginx/vars/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
nginx_repo_key_url: "https://nginx.org/keys/nginx_signing.key"
nginx_repo_url: "http://nginx.org/packages/mainline/ubuntu/"
nginx_version: "1.23.4-1"
8 changes: 5 additions & 3 deletions app/models/ansible_models.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from typing import List, Optional
from pydantic import BaseModel, validator, ValidationError


class AnsibleBase(BaseModel):
ansible_user:str = 'root'
ansible_port:int = 22

class AnsibleInstallNginx(BaseModel):
class AnsibleInstallNginx(AnsibleBase):

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


class AnsibleInstallDocker(BaseModel):
class AnsibleInstallDocker(AnsibleBase):
os: str = 'ubuntu'
hosts:List[str] = ['www.example.com']
version:str = 'latest'
Expand Down
115 changes: 114 additions & 1 deletion app/template_generators/ansible/install/nginx.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,122 @@

def ansible_nginx_install_ubuntu(input):
prompt = ""
nginx_hosts = input.hosts
inventory = (f"[nginx_nodes]\n" + "\n".join(nginx_hosts))
nginx_version = input.version

prompt = f"""
Generate a Python code to generate an Ansible project (project name is app/media/MyAnsible)
that dynamically provisions Ansible resources ensuring a modular, flexible structure. Only provide
Python code, no explanations or markdown formatting. The project should be organized as follows:
The structure of this project must be as follows:
```
├── ansible.cfg
├── group_vars
│   |── nginx_nodes
│  
├── hosts
├── host_vars
├── nginx_playbook.yml
└── roles
└── install_nginx
├── defaults
│   └── main.yml
├── handlers
│   └── main.yml
├── tasks
│   └── main.yml
├── templates
│   └── main.yml
└── vars
└── main.yml
```
- The content of ansible.cfg must be as follows:
```
[defaults]
host_key_checking=false
```
- group_vars directory includes a single file called "nginx_nodes" and the content of this file must be as follows:
```
ansible_port: 22
ansible_user: root
```
- there is file called "hosts" which its content must be as follows:
```
{inventory}
```
- There is an empty directory called "host_vars" with no files included
- There is a file called "nginx_playbook.yml" which its content must be as follows:
```
- hosts: all
roles:
- install_nginx
```
- There is a directory called "roles" which a sub-directory called "install_nginx" (roles/install_nginx)
"install_nginx" has multiple sub-directories, so let's dive deeper into each its sub-directories:
- (install_nginx/tasks): This path has a file called "main.yml" which its content must be as follows:
```
---
- name: Install CA certificates to ensure HTTPS connections work
apt:
name: ca-certificates
state: present
- name: Add Nginx signing key
apt_key:
url: "{{ nginx_repo_key_url }}"
state: present
- name: Add Nginx repository
apt_repository:
repo: "deb {{ nginx_repo_url }} {{ ansible_distribution_release }} nginx"
state: present
filename: nginx
- name: Update apt cache
apt:
update_cache: yes
- name: Install specific version of Nginx
apt:
name: "nginx={{ nginx_version }}~{{ ansible_distribution_release }}"
state: present
- name: Ensure Nginx service is running and enabled
service:
name: nginx
state: started
enabled: yes
```
- (install_nginx/vars): This path has a file called "main.yml" which its content must be as follows:
```
nginx_repo_key_url: "https://nginx.org/keys/nginx_signing.key"
nginx_repo_url: "http://nginx.org/packages/mainline/ubuntu/"
nginx_version: "1.23.4-1"
```
finally just give me a python code without any note that can generate a project folder with the given
schema without ```python entry. and we dont need any base directory in the python code. the final
terraform template must work very well without any error!
the python code you give me, must have structure like that:
import os
project_name = "app/media/MyAnsible"
foo_dir = os.path.join(project_name, "bar")
x_dir = os.path.join(modules_dir, "y")
# Create project directories
os.makedirs(ansible_dir, exist_ok=True)
# Create main.tf
with open(os.path.join(project_name, "main.tf"), "w") as main_file:
# any thing you need
"""
return prompt



def ansible_nginx_install(input):

if input.os == 'ubuntu':
Expand Down

0 comments on commit ba1d48e

Please sign in to comment.