Skip to content

Commit e05ee3e

Browse files
committed
Fixed ansible conection
1 parent e9a11aa commit e05ee3e

9 files changed

Lines changed: 51 additions & 48 deletions

File tree

WebApi/backend/integrations/ansible.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ class AnsibleSwitchConnector:
55
def __init__(self):
66
self.huaweiHost = 'backend/integrations/communs/huawei/hosts.yml'
77
self.ciscoHost = 'backend/integrations/communs/cisco/hosts.yml'
8-
self.huaweiPlaybook = 'backend/integrations/communs/cisco/playbook.yml'
8+
self.huaweiPlaybook = 'backend/integrations/communs/huawei/playbook.yml'
99
self.ciscoPlaybook = 'backend/integrations/communs/cisco/playbook.yml'
1010
self.ansibleHost = 'backend/integrations/communs/ansible/hosts.yml'
1111
self.ansiblePlaybook = 'backend/integrations/communs/ansible/playbook.yml'
12+
self.ansibleCFG = 'backend/integrations/communs/ansible/ansible.cfg'
1213

1314
def write_ansible_host(self, string, switch, password, username):
1415
user_str = f" ansible_user: {username}\n"
@@ -37,18 +38,23 @@ def write_ansible_host(self, string, switch, password, username):
3738
return False
3839

3940
def write_ansible_playbook(self, string, switch):
40-
try:
41-
command_list = string.split('\n')
42-
command_str = ''
41+
command_list = string.split('\n')
42+
if len(command_list) == 1:
43+
command_str = f' commands: {command_list[0]}\n'
44+
45+
else:
46+
command_str = ' commands: |\n'
4347
for item in command_list:
44-
command_str += ' '+item+'\n'
48+
command_str += ' '+item+'\n'
49+
50+
try:
4551
if switch == 'Huawei':
4652
with open(self.huaweiPlaybook, 'r') as arquivo:
4753
linhas = arquivo.readlines()
4854
if switch == 'Cisco':
4955
with open(self.ciscoPlaybook, 'r') as arquivo:
5056
linhas = arquivo.readlines()
51-
linhas[16] = command_str
57+
linhas[15] = command_str
5258
with open(self.ansiblePlaybook, 'w') as arquivo:
5359
arquivo.writelines(linhas)
5460
return True
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[defaults]
2+
host_key_checking = False
3+
deprecation_warnings = False

WebApi/backend/integrations/communs/cisco/playbook.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
tasks:
1414
- name: Custom Commands
1515
ce_command:
16-
commands: |
17-
16+
commands:
1817
register: display_output
1918

2019
- name: Command Output

WebApi/backend/integrations/communs/huawei/playbook.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
tasks:
1414
- name: Custom Commands
1515
ce_command:
16-
commands: |
17-
16+
commands:
1817
register: display_output
1918

2019
- name: Command Output

WebApi/backend/urls.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
path('', HomeView.as_view(), name='home'),
1010
path('ansible/', AnsibleView.as_view(), name='ansible'),
1111
path('ansible/credential/', CredentialPageView.as_view(), name='credentialList'),
12-
path('ansible/credential/create/', CreateCredentialView.as_view(), name='credentialCreate'),
1312
path('ansible/credential/<int:pk>/edit/', UpdateCredentialView.as_view(), name='credentialEdit'),
1413
path('ansible/credential/<int:pk>/delete/', DeleteCredentialView.as_view(), name='credentialDelete'),
1514
path('log/', LogView.as_view(), name='log'),

WebApi/backend/views/ansible.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def execute_command(self, playbook, host, switch, username, password):
2020
# Adicionar verificações de segurança aqui
2121
try:
2222
self.ansible.write_ansible_playbook(playbook, str(switch))
23-
self.ansible.write_ansible_host(host, str(switch), username, password)
23+
self.ansible.write_ansible_host(host, switch = str(switch), username = username, password = password)
2424
output = self.ansible.run_ansible()
2525
return output
2626
except:

WebApi/backend/views/credential.py

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,14 @@ def get_context_data(self, **kwargs):
1717
context['page'] = "Credentials"
1818
return context
1919

20-
21-
class CreateCredentialView(AdminRequired, TemplateView):
22-
template_name = "credential.html"
23-
24-
def get_context_data(self, **kwargs):
25-
context = super().get_context_data(**kwargs)
26-
context['settings'] = 1
27-
context['page'] = "Create Credentials"
28-
return context
29-
3020
def post(self, request ,*args, **kwargs):
3121
createCredential(
3222
title = request.POST.get('title'),
3323
username = request.POST.get('username'),
3424
password = request.POST.get('password'),
3525
)
3626
result = "The credentials have been successfully upgraded."
37-
return render(request, self.template_name, {'result': result, 'sucess': 1})
27+
return self.render_to_response(self.get_context_data(result = result))
3828

3929

4030
class UpdateCredentialView(AdminRequired, UpdateView):
@@ -45,7 +35,7 @@ class UpdateCredentialView(AdminRequired, UpdateView):
4535

4636
def get_context_data(self, **kwargs):
4737
context = super().get_context_data(**kwargs)
48-
context['settings'] = 2
38+
context['settings'] = 1
4939
context['page'] = "Manage Credentials"
5040
return context
5141

@@ -57,6 +47,6 @@ class DeleteCredentialView(AdminRequired, DeleteView):
5747

5848
def get_context_data(self, **kwargs):
5949
context = super().get_context_data(**kwargs)
60-
context['settings'] = 3
50+
context['settings'] = 2
6151
context['page'] = "Delete Credentials"
6252
return context

WebApi/frontend/templates/credential.html

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,31 @@
77
<h1>{{page}}</h1>
88

99
{% if result %}
10-
<h3>{{result}}</h3>
10+
<h3 class="text-success">{{result}}</h3>
1111
{% endif %}
1212

1313
{% if settings == 0 %}
14+
15+
<form method="POST">
16+
{% csrf_token %}
17+
<div class="form-group">
18+
<label>Add new credential</label>
19+
</div>
20+
<div class="form-group">
21+
<label>Credential Title</label>
22+
<input class="form-control" type="text" name="title" placeholder="Title">
23+
</div>
24+
<div class="form-group">
25+
<label>Switch SSH Username</label>
26+
<input class="form-control" type="text" name="username" placeholder="Username">
27+
</div>
28+
<div class="form-group">
29+
<label>Switch SSH Password</label>
30+
<input class="form-control" type="text" name="password" placeholder="Password">
31+
</div>
32+
<button class="btn btn-primary" type="submit" style="margin-top: 5px;">Confirm</button>
33+
</form>
34+
1435
<div class="col-md-12">
1536
<div class="panel panel-default">
1637
<div class="panel-heading">
@@ -64,24 +85,6 @@ <h3>{{result}}</h3>
6485
</div>
6586

6687
{% elif settings == 1 %}
67-
<form method="POST">
68-
{% csrf_token %}
69-
<div class="form-group">
70-
<label>Credential Title</label>
71-
<input class="form-control" type="text" name="title" placeholder="Title">
72-
</div>
73-
<div class="form-group">
74-
<label>Switch SSH Username</label>
75-
<input class="form-control" type="text" name="username" placeholder="Username">
76-
</div>
77-
<div class="form-group">
78-
<label>Switch SSH Password</label>
79-
<input class="form-control" type="password" name="password" placeholder="Password">
80-
</div>
81-
<button class="btn btn-primary" type="submit" style="margin-top: 5px;">Confirm</button>
82-
</form>
83-
84-
{% elif settings == 2 %}
8588
<form method="POST">
8689
{% csrf_token %}
8790
<div class="form-group">
@@ -94,7 +97,7 @@ <h3>{{result}}</h3>
9497
</div>
9598
<div class="form-group">
9699
<label>Switch SSH Password</label>
97-
<input class="form-control" type="password" name="password" placeholder="Password" value="{{ object.password }}">
100+
<input class="form-control" type="text" name="password" placeholder="Password" value="{{ object.password }}">
98101
</div>
99102
<button class="btn btn-primary" type="submit" style="margin-top: 5px;">Confirm</button>
100103
</form>

readme.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@
2424

2525
You can use Docker Image
2626

27-
The current version of the project is 1.3, which is considered the most stable and reliable release available. This version has undergone extensive testing and debugging to ensure a high level of stability and performance.
27+
The current version of the project is 1.4, which is considered the most stable and reliable release available. This version has undergone extensive testing and debugging to ensure a high level of stability and performance.
2828

2929
```
30-
docker pull xandouglas/switchsetup:v1.3
31-
docker run -p 8000:8000 --name switchsetup -d xandouglas/switchsetup:v1.3
30+
docker pull xandouglas/switchsetup:v1.4
31+
docker run -p 8000:8000 --name switchsetup -d xandouglas/switchsetup:v1.4
32+
If dont work:
33+
sudo docker run -p 8000:8000 xandouglas/switchsetup:latest
3234
```
3335

3436
After creating the image, use for login:
@@ -86,6 +88,7 @@ python manage.py migrate
8688
cd ..
8789
docker build -t image_name
8890
docker run -p 8000:8000 image_name
91+
8992
```
9093
## Database
9194

@@ -109,6 +112,7 @@ After this, a new admin user with the username "admin" and password "123" is aut
109112

110113
| Version | | Date |
111114
|----------|:-------------:|------:|
115+
| 1.4 | Fixed ansible conection | 17/07/2023 |
112116
| 1.3 | SSH Save Credentials | 16/07/2023 |
113117
| 1.2 | User Options | 14/07/2023 |
114118
| 1.1 | Fixed login form | 14/07/2023 |

0 commit comments

Comments
 (0)