From ba1d48e1f1f2f71996dddec216c71d12a05c5024 Mon Sep 17 00:00:00 2001 From: abolfazl1381 Date: Wed, 27 Nov 2024 22:58:07 +0330 Subject: [PATCH] fix(ansible_base): add ansible base --- app/directory_generators/ansible_generator.py | 89 ++++++++++++++ app/media/MyAnsible/ansible.cfg | 2 + app/media/MyAnsible/group_vars/nginx_nodes | 2 + app/media/MyAnsible/hosts | 2 + app/media/MyAnsible/nginx_playbook.yml | 3 + .../roles/install_nginx/tasks/main.yml | 31 +++++ .../roles/install_nginx/vars/main.yml | 3 + app/models/ansible_models.py | 8 +- .../ansible/install/nginx.py | 115 +++++++++++++++++- 9 files changed, 251 insertions(+), 4 deletions(-) create mode 100644 app/media/MyAnsible/ansible.cfg create mode 100644 app/media/MyAnsible/group_vars/nginx_nodes create mode 100644 app/media/MyAnsible/hosts create mode 100644 app/media/MyAnsible/nginx_playbook.yml create mode 100644 app/media/MyAnsible/roles/install_nginx/tasks/main.yml create mode 100644 app/media/MyAnsible/roles/install_nginx/vars/main.yml diff --git a/app/directory_generators/ansible_generator.py b/app/directory_generators/ansible_generator.py index e69de29b..afa94917 100644 --- a/app/directory_generators/ansible_generator.py +++ b/app/directory_generators/ansible_generator.py @@ -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") \ No newline at end of file diff --git a/app/media/MyAnsible/ansible.cfg b/app/media/MyAnsible/ansible.cfg new file mode 100644 index 00000000..1677c639 --- /dev/null +++ b/app/media/MyAnsible/ansible.cfg @@ -0,0 +1,2 @@ +[defaults] +host_key_checking=false diff --git a/app/media/MyAnsible/group_vars/nginx_nodes b/app/media/MyAnsible/group_vars/nginx_nodes new file mode 100644 index 00000000..0d1345c2 --- /dev/null +++ b/app/media/MyAnsible/group_vars/nginx_nodes @@ -0,0 +1,2 @@ +ansible_port: 22 +ansible_user: root diff --git a/app/media/MyAnsible/hosts b/app/media/MyAnsible/hosts new file mode 100644 index 00000000..39f58623 --- /dev/null +++ b/app/media/MyAnsible/hosts @@ -0,0 +1,2 @@ +[nginx_nodes] +www.examppple.com diff --git a/app/media/MyAnsible/nginx_playbook.yml b/app/media/MyAnsible/nginx_playbook.yml new file mode 100644 index 00000000..475800e3 --- /dev/null +++ b/app/media/MyAnsible/nginx_playbook.yml @@ -0,0 +1,3 @@ +- hosts: all + roles: + - install_nginx diff --git a/app/media/MyAnsible/roles/install_nginx/tasks/main.yml b/app/media/MyAnsible/roles/install_nginx/tasks/main.yml new file mode 100644 index 00000000..b87d89dd --- /dev/null +++ b/app/media/MyAnsible/roles/install_nginx/tasks/main.yml @@ -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 diff --git a/app/media/MyAnsible/roles/install_nginx/vars/main.yml b/app/media/MyAnsible/roles/install_nginx/vars/main.yml new file mode 100644 index 00000000..7c28ea13 --- /dev/null +++ b/app/media/MyAnsible/roles/install_nginx/vars/main.yml @@ -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" diff --git a/app/models/ansible_models.py b/app/models/ansible_models.py index d30c3cba..8ff0f9aa 100644 --- a/app/models/ansible_models.py +++ b/app/models/ansible_models.py @@ -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'] @@ -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' diff --git a/app/template_generators/ansible/install/nginx.py b/app/template_generators/ansible/install/nginx.py index 03780a67..9fae7daa 100644 --- a/app/template_generators/ansible/install/nginx.py +++ b/app/template_generators/ansible/install/nginx.py @@ -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':