1- import os
2-
3- project_name = "app/media/MyAnsible"
4- roles_dir = os .path .join (project_name , "roles" )
5- nginx_dir = os .path .join (roles_dir , "nginx" )
6- tasks_dir = os .path .join (nginx_dir , "tasks" )
7- handlers_dir = os .path .join (nginx_dir , "handlers" )
8- var_dir = os .path .join (nginx_dir , "var" )
9- meta_dir = os .path .join (nginx_dir , "meta" )
10-
11- # Create project directories
12- os .makedirs (nginx_dir , exist_ok = True )
13- os .makedirs (tasks_dir , exist_ok = True )
14- os .makedirs (handlers_dir , exist_ok = True )
15- os .makedirs (var_dir , exist_ok = True )
16- os .makedirs (meta_dir , exist_ok = True )
17-
18- # Create install_nginx.yaml
19- with open (os .path .join (project_name , "install_nginx.yaml" ), "w" ) as main_file :
20- main_file .write ('''---
21- - name: install nginx service
22- hosts:
23- - dest
24- roles:
25- - nginx
26- ''' )
27-
28- # Create handlers/main.yaml
29- with open (os .path .join (handlers_dir , "main.yaml" ), "w" ) as handlers_file :
30- handlers_file .write ('''---
31- - name: restart nginx
32- service:
33- name: "{ service }"
34- state: restarted
35-
36- - name: restart firewall
37- service:
38- name: firewalld
39- state: restarted
40- ''' )
41-
42- # Create tasks/main.yaml
43- with open (os .path .join (tasks_dir , "main.yaml" ), "w" ) as tasks_file :
44- tasks_file .write ('''---
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- # Create tasks/unzip.yaml
77- with open (os .path .join (tasks_dir , "unzip.yaml" ), "w" ) as unzip_file :
78- unzip_file .write ('''---
79- - name: unarchive zip file
80- ansible.builtin.unarchive:
81- src: /home/ansible/ninom.zip
82- dest: /home/ansible
83- remote_src: yes
84- register: unzip
85-
86- - debug:
87- var: unzip
88- ''' )
89-
90- # Create tasks/download_webpage.yaml
91- with open (os .path .join (tasks_dir , "download_webpage.yaml" ), "w" ) as download_file :
92- download_file .write ('''---
93- - name: download webpage
94- ansible.builtin.get_url:
95- url: "{ url_path }"
96- dest: /home/ansible
97- timeout: 20
98- validate_certs: no
99- register: download
100- ignore_errors: True
101-
102- - debug:
103- var: download
104- ''' )
105-
106- # Create tasks/install_service.yaml
107- with open (os .path .join (tasks_dir , "install_service.yaml" ), "w" ) as install_file :
108- install_file .write ('''---
109- - name: install { service }
110- register: install
111- ansible.builtin.package:
112- name: "{ item }"
113- state: latest
114- loop:
115- - "{ service }"
116-
117- - debug:
118- var: install
119- ''' )
120-
121- # Create tasks/copy_files.yaml
122- with open (os .path .join (tasks_dir , "copy_files.yaml" ), "w" ) as copy_file :
123- copy_file .write ('''---
124- - name: delete before context
125- shell: "rm -rf /usr/share/nginx/html/*"
126- register: delete
127-
128- - debug:
129- var: delete
130-
131- - name: copy html file on the html main dir
132- register: copy_file
133- copy:
134- src: /home/ansible/ninom-html/
135- dest: /usr/share/nginx/html/
136- remote_src: yes
137- directory_mode: yes
138- notify:
139- - restart nginx
140-
141- - debug:
142- var: copy_file
143- ''' )
144-
145- # Create var/main.yaml
146- with open (os .path .join (var_dir , "main.yaml" ), "w" ) as var_file :
147- var_file .write ('''---
148- # vars file for nginx
149- #url_path : https://www.free-css.com/assets/files/free-css-templates/download/page261/avalon.zip
150- url_path : https://www.free-css.com/assets/files/free-css-templates/download/page283/ninom.zip
151- service: nginx
152- ''' )
153-
154- # Create meta/main.yaml
155- with open (os .path .join (meta_dir , "main.yaml" ), "w" ) as meta_file :
156- meta_file .write ('''---
157- allow_duplicates: yes
158- dependencies:
159- - { role: pre-install }
160- ''' )
0 commit comments