chaos4455 commited on
Commit
af518dd
·
verified ·
1 Parent(s): b04beb4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -45
app.py CHANGED
@@ -34,7 +34,7 @@ def send_message_to_model(message, model_name, temperature, top_p, top_k, max_to
34
  st.error(f"❌ Error communicating with the AI: {e}")
35
  return None
36
 
37
- def generate_ansible_playbook(prompt_base, detail_level, os_type, security_level, model_name, temperature, top_p, top_k, max_tokens, prompt_detail, encoding, add_header, log_level, ansible_user, ansible_ssh_pass, ansible_become, custom_requirements, specific_details, target_hosts):
38
  """Generates an Ansible playbook based on user settings."""
39
  prompt = f"""
40
  You are an expert Ansible automation engineer. Your task is to generate a complete, secure, and efficient Ansible playbook based on the following description:
@@ -47,7 +47,7 @@ def generate_ansible_playbook(prompt_base, detail_level, os_type, security_level
47
  **Security Level:** {security_level}
48
  **Prompt Detail Level**:{prompt_detail}
49
  **Operating System:** {os_type}
50
- **Target Hosts:** {target_hosts if target_hosts else "None"}
51
  **Ansible User:** {ansible_user if ansible_user else "root"}
52
  **Ansible SSH Password:** {ansible_ssh_pass if ansible_ssh_pass else "None"}
53
  **Ansible Become:** {ansible_become}
@@ -60,6 +60,7 @@ def generate_ansible_playbook(prompt_base, detail_level, os_type, security_level
60
  - Do not include comments, explanations, or any other text outside the code block, unless the prompt requires.
61
  - The code must maintain its full vertical formatting, respecting indentation and line breaks.
62
  - The code must be realistic, using real-world examples, data, and situations.
 
63
  - Explore different approaches, techniques, and advanced practices, always prioritizing security and efficiency.
64
  - Use advanced Ansible resources such as modules, roles, variables, functions, conditionals, loops, and handlers, when necessary.
65
  - Unless the user specifies otherwise, use the most current and secure versions of the modules, using ansible and following best practices.
@@ -108,42 +109,6 @@ def parse_and_save_yml(ai_code, short_title, encoding, add_header):
108
 
109
  return file_name, yml_code
110
 
111
- def validate_ip_input(ip_input):
112
- """Validates and parses IP address input."""
113
- try:
114
- if "," in ip_input:
115
- ips = [ip.strip() for ip in ip_input.split(",")]
116
- for ip in ips:
117
- if "/" in ip:
118
- ipaddress.ip_network(ip, strict=False) #verify if is a network
119
- elif "-" in ip:
120
- parts = ip.split("-")
121
- if len(parts) != 2:
122
- return False, "Invalid Range Format"
123
-
124
- ipaddress.ip_address(parts[0])
125
- ipaddress.ip_address(parts[1])
126
- else:
127
- ipaddress.ip_address(ip) #verify if is a valid address
128
- return True, ips
129
- elif "/" in ip_input:
130
- ipaddress.ip_network(ip_input, strict=False) #verify if is a network
131
- return True, [ip_input]
132
- elif "-" in ip_input:
133
- parts = ip_input.split("-")
134
- if len(parts) != 2:
135
- return False, "Invalid Range Format"
136
-
137
- ipaddress.ip_address(parts[0])
138
- ipaddress.ip_address(parts[1])
139
- return True,[ip_input]
140
- else:
141
- ipaddress.ip_address(ip_input)
142
- return True, [ip_input]
143
- except ValueError as e:
144
- return False, str(e)
145
- except Exception as e:
146
- return False, str(e)
147
 
148
  def main():
149
  st.title("⚙️ Gemini2 Ansible Gen Pro by [Elias Andrade](https://github.com/chaos4455)")
@@ -193,7 +158,7 @@ def main():
193
  with col2:
194
  # User's base prompt
195
  prompt_base = st.text_input("Describe the Ansible Playbook:", placeholder="Ex: Install apache2 and configure a default page for all the servers", key="prompt_base")
196
- target_hosts = st.text_input("Target Hosts (IP(s), Range or CIDR, separated by comma):", placeholder="192.168.0.1,192.168.0.2,192.168.0.1/24 or 192.168.0.1-255",key="target_hosts")
197
 
198
  if prompt_presets != "None":
199
  if prompt_base:
@@ -204,11 +169,6 @@ def main():
204
  if not prompt_base:
205
  st.error("⚠️ Please enter a playbook description.")
206
  return
207
-
208
- valid_ip, parsed_ips = validate_ip_input(target_hosts)
209
- if not valid_ip:
210
- st.error(f"⚠️ Invalid IP Address or Range: {parsed_ips}")
211
- return
212
 
213
  with st.spinner("⏳ Generating playbook..."):
214
  ai_code = generate_ansible_playbook(
@@ -230,7 +190,7 @@ def main():
230
  ansible_become,
231
  custom_requirements,
232
  specific_details,
233
- parsed_ips if parsed_ips else None
234
  )
235
 
236
  if ai_code:
 
34
  st.error(f"❌ Error communicating with the AI: {e}")
35
  return None
36
 
37
+ def generate_ansible_playbook(prompt_base, detail_level, os_type, security_level, model_name, temperature, top_p, top_k, max_tokens, prompt_detail, encoding, add_header, log_level, ansible_user, ansible_ssh_pass, ansible_become, custom_requirements, specific_details, target_hosts_description):
38
  """Generates an Ansible playbook based on user settings."""
39
  prompt = f"""
40
  You are an expert Ansible automation engineer. Your task is to generate a complete, secure, and efficient Ansible playbook based on the following description:
 
47
  **Security Level:** {security_level}
48
  **Prompt Detail Level**:{prompt_detail}
49
  **Operating System:** {os_type}
50
+ **Target Hosts:** {target_hosts_description if target_hosts_description else "None"}
51
  **Ansible User:** {ansible_user if ansible_user else "root"}
52
  **Ansible SSH Password:** {ansible_ssh_pass if ansible_ssh_pass else "None"}
53
  **Ansible Become:** {ansible_become}
 
60
  - Do not include comments, explanations, or any other text outside the code block, unless the prompt requires.
61
  - The code must maintain its full vertical formatting, respecting indentation and line breaks.
62
  - The code must be realistic, using real-world examples, data, and situations.
63
+ - The `hosts` section of the playbook, must include the list of the target hosts in the description, if the target hosts are not specified you must create your own hosts list, please avoid using "all" if the target hosts are specified.
64
  - Explore different approaches, techniques, and advanced practices, always prioritizing security and efficiency.
65
  - Use advanced Ansible resources such as modules, roles, variables, functions, conditionals, loops, and handlers, when necessary.
66
  - Unless the user specifies otherwise, use the most current and secure versions of the modules, using ansible and following best practices.
 
109
 
110
  return file_name, yml_code
111
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112
 
113
  def main():
114
  st.title("⚙️ Gemini2 Ansible Gen Pro by [Elias Andrade](https://github.com/chaos4455)")
 
158
  with col2:
159
  # User's base prompt
160
  prompt_base = st.text_input("Describe the Ansible Playbook:", placeholder="Ex: Install apache2 and configure a default page for all the servers", key="prompt_base")
161
+ target_hosts_description = st.text_input("Target Hosts Description (text format, IP(s), Range or CIDR):", placeholder="Ex: all the servers in 192.168.0.0/24 network",key="target_hosts")
162
 
163
  if prompt_presets != "None":
164
  if prompt_base:
 
169
  if not prompt_base:
170
  st.error("⚠️ Please enter a playbook description.")
171
  return
 
 
 
 
 
172
 
173
  with st.spinner("⏳ Generating playbook..."):
174
  ai_code = generate_ansible_playbook(
 
190
  ansible_become,
191
  custom_requirements,
192
  specific_details,
193
+ target_hosts_description # Pass only the description
194
  )
195
 
196
  if ai_code: