Skip to content

Commit 641342d

Browse files
committed
refactor(ansible): refactor ansible function for more flexibility
1 parent cf4be88 commit 641342d

1 file changed

Lines changed: 8 additions & 218 deletions

File tree

  • app/template_generators/ansible/install
Lines changed: 8 additions & 218 deletions
Original file line numberDiff line numberDiff line change
@@ -1,222 +1,12 @@
11

2-
2+
def ansible_nginx_install_ubuntu(input):
3+
prompt = ""
4+
return prompt
5+
6+
37
def ansible_nginx_install(input):
48

5-
prompt = f"""
9+
if input.os == 'ubuntu':
10+
return ansible_nginx_install_ubuntu(input)
611

7-
Generate a Python code to generate a Ansible project (project name is app/media/MyAnsible)
8-
with installs Nginx leatest version on the {input.os}.
9-
finally just give me a python code without any note that can generate a project folder with the given
10-
schema without ```python entry.(important)
11-
project's structure must be same as the structure which I write below:
12-
13-
1. at location app/media/MyAnsible/install_nginx.yaml set:
14-
```
15-
---
16-
- name: install nginx service
17-
hosts:
18-
- dest
19-
roles:
20-
- nginx
21-
22-
```
23-
24-
2. create a file named app/media/MyAnsible/roles/nginx/handlers/main.yaml and put these contents on them:
25-
```
26-
27-
---
28-
29-
- name: restart nginx
30-
service:
31-
name: "{{ service }}"
32-
state: restarted
33-
34-
- name: restart firewall
35-
service:
36-
name: firewalld
37-
state: restarted
38-
39-
```
40-
41-
3. create a file named app/media/MyAnsible/roles/nginx/tasks/main.yaml and put these contents on them:
42-
```
43-
44-
---
45-
# tasks file for nginx
46-
- name: download page
47-
include: download_webpage.yml
48-
49-
- name: unzip
50-
include: unzip.yml
51-
52-
- name: install service
53-
include: install_service.yml
54-
55-
- name: open website port https
56-
firewalld:
57-
service: https
58-
permanent: yes
59-
state: enabled
60-
notify:
61-
- restart firewall
62-
63-
- name: open website port http
64-
firewalld:
65-
zone: public
66-
service: http
67-
permanent: yes
68-
state: enabled
69-
notify:
70-
- restart firewall
71-
72-
- name: webpage
73-
include: copy_files.yml
74-
75-
```
76-
77-
4. create a file named app/media/MyAnsible/roles/nginx/tasks/unzip.yaml and put these contents on them:
78-
79-
```
80-
---
81-
- name: unarchive zip file
82-
ansible.builtin.unarchive:
83-
src: /home/ansible/ninom.zip
84-
dest: /home/ansible
85-
remote_src: yes
86-
register: unzip
87-
88-
- debug:
89-
var: unzip
90-
91-
92-
93-
```
94-
95-
5. create a file named app/media/MyAnsible/roles/nginx/tasks/download_webpage.yaml and put these contents on them:
96-
97-
```
98-
---
99-
- name: download webpage
100-
ansible.builtin.get_url:
101-
url: "{{ url_path }}"
102-
dest: /home/ansible
103-
timeout: 20
104-
validate_certs: no
105-
register: download
106-
ignore_errors: True
107-
108-
- debug:
109-
var: download
110-
111-
112-
```
113-
114-
6. create a file named app/media/MyAnsible/roles/nginx/tasks/install_service.yaml and put these contents on them:
115-
116-
```
117-
---
118-
- name: install {{ service }}
119-
register: install
120-
ansible.builtin.package:
121-
name: "{{ item }}"
122-
state: latest
123-
loop:
124-
- "{{ service }}"
125-
126-
- debug:
127-
var: install
128-
129-
130-
```
131-
132-
7. create a file named app/media/MyAnsible/roles/nginx/tasks/copy_files.yaml and put these contents on them:
133-
134-
```
135-
---
136-
- name: delete befor context
137-
shell: "rm -rf /usr/share/nginx/html/*"
138-
register: delete
139-
140-
- debug:
141-
var: delete
142-
143-
- name: copy html file on the html main dir
144-
register: copy_file
145-
copy:
146-
src: /home/ansible/ninom-html/
147-
dest: /usr/share/nginx/html/
148-
remote_src: yes
149-
directory_mode: yes
150-
notify:
151-
- restart nginx
152-
153-
- debug:
154-
var: copy_file
155-
156-
157-
```
158-
159-
8. create a file named app/media/MyAnsible/roles/nginx/var/main.yaml and put these contents on them:
160-
161-
```
162-
---
163-
# vars file for nginx
164-
#url_path : https://www.free-css.com/assets/files/free-css-templates/download/page261/avalon.zip
165-
url_path : https://www.free-css.com/assets/files/free-css-templates/download/page283/ninom.zip
166-
service: nginx
167-
168-
169-
```
170-
171-
9. create a file named app/media/MyAnsible/roles/nginx/meta/main.yaml and put these contents on them:
172-
173-
```
174-
---
175-
allow_duplicates: yes
176-
dependencies:
177-
- {{ role: pre-install }}
178-
179-
180-
```
181-
182-
10. create a file named app/media/MyAnsible/roles/nginx/handlers/main.yaml and put these contents on them:
183-
184-
```
185-
---
186-
# handlers file for nginx
187-
- name: restart nginx
188-
service:
189-
name: "{{ service }}"
190-
state: restarted
191-
192-
- name: restart firewall
193-
service:
194-
name: firewalld
195-
state: restarted
196-
197-
198-
```
199-
200-
201-
finally just give me a python code without any note that can generate a project folder with the given
202-
schema without ```python entry.(important) and we dont need any base directory in the python code. the final
203-
ansible template must work very well without any error!
204-
205-
Python code you give me, must have structure like that:
206-
207-
import os
208-
project_name = "app/media/MyAnsible"
209-
roles_dir = os.path.join(project_name, "roles")
210-
docker_container_dir = os.path.join(modules_dir, "docker_container")
211-
212-
# Create project directories
213-
os.makedirs(docker_container_dir, exist_ok=True)
214-
215-
# Create main.tf
216-
with open(os.path.join(project_name, "install_nginx.yaml"), "w") as main_file:
217-
# any thing you need
218-
219-
220-
221-
"""
222-
return prompt
12+

0 commit comments

Comments
 (0)