-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
244 lines (215 loc) · 8.4 KB
/
main.py
File metadata and controls
244 lines (215 loc) · 8.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
from typing import Any, Dict, List, Optional
from fastmcp import FastMCP
import yaml
# Initialize FastMCP server
mcp = FastMCP(
name="ansible-info",
description="Ansible information service",
version="1.0.0"
)
# Types as Python dataclasses/type hints
AnsibleInfo = Dict[str, Any]
PlaybookTask = Dict[str, Any]
Playbook = Dict[str, Any]
async def get_ansible_info() -> AnsibleInfo:
"""Helper function to get Ansible info."""
return {
"version": "2.15.5",
"installation": {
"rhel": [
"sudo subscription-manager repos --enable ansible-2.9-for-rhel-8-x86_64-rpms",
"sudo dnf install ansible",
"# Alternative method using EPEL",
"sudo dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm",
"sudo dnf install ansible",
],
"ubuntu": [
"sudo apt update",
"sudo apt install software-properties-common",
"sudo apt-add-repository --yes --update ppa:ansible/ansible",
"sudo apt install ansible",
],
"pip": [
"sudo dnf install python3-pip # For RHEL/CentOS",
"sudo apt install python3-pip # For Ubuntu",
"pip3 install --user ansible",
],
},
"documentation": {
"main": "https://docs.ansible.com",
"installation": "https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html",
"playbooks": "https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_intro.html",
},
"modules": [
"command",
"shell",
"copy",
"file",
"yum",
"apt",
"dnf",
"service",
"template",
"git",
"user",
"group",
"cron",
"mount",
"systemd",
"firewalld",
],
}
@mcp.tool()
async def get_ansible_version() -> str:
"""Get the current Ansible version information."""
try:
info = await get_ansible_info()
return f"""Ansible Version: {info['version']}
Documentation Links:
- Main: {info['documentation']['main']}
- Installation Guide: {info['documentation']['installation']}
- Playbook Guide: {info['documentation']['playbooks']}"""
except Exception as e:
return f"Error fetching Ansible version: {str(e)}"
@mcp.tool()
async def get_ansible_modules() -> str:
"""Get list of available Ansible modules."""
try:
info = await get_ansible_info()
modules_list = "\n".join(f"- {m}" for m in info["modules"])
return f"""Available Ansible Modules:
{modules_list}
For a complete list, visit: https://docs.ansible.com/ansible/latest/collections/index_module.html"""
except Exception as e:
return f"Error fetching Ansible modules: {str(e)}"
@mcp.tool()
async def get_installation_guide() -> str:
"""Get Ansible installation and documentation information."""
try:
info = await get_ansible_info()
return f"""Installation Guide for Different Systems:
1. RHEL/CentOS Installation:
{chr(10).join(info['installation']['rhel'])}
2. Ubuntu Installation:
{chr(10).join(info['installation']['ubuntu'])}
3. Python pip Installation (Cross-platform):
{chr(10).join(info['installation']['pip'])}
After installation, verify with:
ansible --version
Documentation Links:
- Main Documentation: {info['documentation']['main']}
- Detailed Installation Guide: {info['documentation']['installation']}
- Playbook Guide: {info['documentation']['playbooks']}
Note: For RHEL, make sure your system is registered with Red Hat Subscription Management before installation."""
except Exception as e:
return f"Error fetching installation guide: {str(e)}"
@mcp.tool()
async def generate_playbook(template_name: str, variables: Optional[Dict[str, Any]] = None) -> str:
"""Generate an Ansible playbook from template.
Args:
template_name: Name of the template to use
variables: Optional variables to use in the template
"""
try:
templates = {
"install-package": {
"name": "Install Package Playbook",
"hosts": ["all"],
"tasks": [
{
"name": "Install package",
"module": "package",
"args": {
"name": "{{ package_name }}",
"state": "present",
},
},
],
"vars": {
"package_name": variables.get("package_name", "httpd") if variables else "httpd",
},
},
"configure-service": {
"name": "Configure Service Playbook",
"hosts": ["all"],
"tasks": [
{
"name": "Ensure service is installed",
"module": "package",
"args": {
"name": "{{ service_name }}",
"state": "present",
},
},
{
"name": "Configure service",
"module": "template",
"args": {
"src": "{{ config_template }}",
"dest": "{{ config_path }}",
},
},
{
"name": "Start and enable service",
"module": "systemd",
"args": {
"name": "{{ service_name }}",
"state": "started",
"enabled": True,
},
},
],
"vars": {
"service_name": variables.get("service_name", "httpd") if variables else "httpd",
"config_template": variables.get("config_template", "templates/service.conf.j2") if variables else "templates/service.conf.j2",
"config_path": variables.get("config_path", "/etc/httpd/conf/httpd.conf") if variables else "/etc/httpd/conf/httpd.conf",
},
},
"setup-user": {
"name": "User Setup Playbook",
"hosts": ["all"],
"tasks": [
{
"name": "Create user group",
"module": "group",
"args": {
"name": "{{ group_name }}",
"state": "present",
},
},
{
"name": "Create user",
"module": "user",
"args": {
"name": "{{ username }}",
"group": "{{ group_name }}",
"shell": "{{ user_shell }}",
"createhome": True,
},
},
],
"vars": {
"username": variables.get("username", "ansible-user") if variables else "ansible-user",
"group_name": variables.get("group_name", "ansible-group") if variables else "ansible-group",
"user_shell": variables.get("user_shell", "/bin/bash") if variables else "/bin/bash",
},
},
}
if template_name not in templates:
available_templates = ", ".join(templates.keys())
raise ValueError(
f"Template '{template_name}' not found. Available templates: {available_templates}")
# Deep copy the template
playbook = templates[template_name].copy()
# Merge variables if provided
if variables:
playbook["vars"].update(variables)
# Convert to YAML
yaml_content = yaml.safe_dump(
[playbook], default_flow_style=False, sort_keys=False)
return f"# Generated Ansible Playbook for template: {template_name}\n{yaml_content}"
except Exception as e:
return f"Error generating playbook: {str(e)}"
if __name__ == "__main__":
# Initialize and run the server
mcp.run()