-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcisco_enable_ssh.yml
executable file
·171 lines (156 loc) · 5.76 KB
/
cisco_enable_ssh.yml
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
#This Playbook is enabling SSH on cisco switch and checking the SSH connection once enabled and running by Jenkins job in AWX
---
- name: Get the credentials from Vault
hosts: all
gather_facts: no
vars:
api_url: "http://vault-service/credential/{{ inventory_hostname }}/?<<some parameter>>"
vault_token: !vault |
$ANSIBLE_VAULT;1.1;AES256
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
tasks:
- name: Set a Jenkins variable awx_job_id
debug:
msg: "JENKINS_EXPORT awx_job_id={{ awx_job_id }}"
- name: Identifying the current Cisco user in Vault
uri:
url: "{{ api_url }}"
method: GET
headers:
Vault-Token: "{{ vault_token }}"
Content-Type: "application/json"
status_code: 200
validate_certs: no
delegate_to: localhost
ignore_errors: yes
register: api_output
- name: Setting the output for extracted user's details for Cisco found in Vault
set_fact:
in_json: "{{ api_output }}"
when: api_output is succeeded
no_log: true
- name: Failing then credentials could not be found in Vault
debug:
msg: "Credentials could not be found in Vault for {{ inventory_hostname }}"
when:
- in_json.failed == true
- in_json.status != '200'
- name: Setting the username for Cisco extracted from Vault
set_fact:
vault_user: "{{ api_output.json[0].username }}"
when: api_output is succeeded and api_output.json != []
- name: Setting the password for Cisco extracted from Vault
set_fact:
vault_password: "{{ api_output.json[0].password }}"
when: api_output is succeeded and api_output.json != []
no_log: true
#######################################################
- name: Run Telnet check and SSH enable blocks (first try)
hosts: all
gather_facts: false
connection: local
serial: 1
vars:
ansible_user: "{{ jenkins_cisco_telnet_username }}"
ansible_password_1: "{{ jenkins_cisco_telnet_password }}"
ansible_password_2: "{{ jenkins_cisco_ssh_password }}"
tasks:
- name: SSH enabling on Cisco block
telnet:
user: "{{ ansible_user }}"
password: "{{ ansible_password_1 }}"
login_prompt: "Password: "
prompts:
- "[>|#|:]"
command:
- enable
- "{{ ansible_password_2 }}"
- conf t
- ip domain-name <<xxx>>
- crypto key generate rsa general-keys label ssh modulus 2048
- ip ssh version 2
- aaa new-model
- aaa authentication login default local
- "username {{ ansible_user }} privilege 15 password 0 {{ ansible_password_2 }}"
- line vty 5 15
- transport input ssh
- login authentication default
- exit
- exit
- exit
register: result
ignore_errors: yes
- name: Reading the errors...
debug:
msg: "It seems that ssh is enabled on Cisco...checking additionally"
when: ("'telnet connection closed' in result.msg" and result is failed)
ignore_errors: yes
- name: Run Telnet check and SSH enable blocks (second try)
hosts: all
gather_facts: false
serial: 1
vars:
ansible_user: "{{ jenkins_cisco_telnet_username }}"
ansible_password_1: "{{ jenkins_cisco_telnet_password }}"
ansible_password_2: "{{ jenkins_cisco_ssh_password }}"
tasks:
- name: SSH enabling on Cisco block (second try)
telnet:
user: "{{ ansible_user }}"
password: "{{ ansible_password_1 }}"
login_prompt: "Username: "
prompts:
- "[>|#|:]"
command:
- enable
- "{{ ansible_password_2 }}"
- conf t
- ip domain-name <<xxx>>
- crypto key generate rsa general-keys label ssh modulus 2048
- ip ssh version 2
- aaa new-model
- aaa authentication login default local
- "username {{ ansible_user }} privilege 15 password 0 {{ ansible_password_2 }}"
- line vty 5 15
- transport input ssh
- login authentication default
- exit
- exit
- exit
register: result_second
ignore_errors: yes
when: ("'telnet connection closed' in result.msg" and result is failed)
- name: Reading the errors...
debug:
msg: "It seems that ssh is enabled on Cisco...checking additionally"
when: ("'telnet connection closed' in result_second.msg" and result_second is failed)
ignore_errors: yes
- name: Run SSH check block
hosts: all
connection: network_cli
gather_facts: false
vars:
ansible_user: "{{ jenkins_cisco_telnet_username }}"
ansible_password: "{{ jenkins_cisco_ssh_password }}"
ansible_become: yes
ansible_become_method: enable
ansible_become_pass: "{{ jenkins_cisco_ssh_password }}"
ansible_network_os: ios
tasks:
- name: Run SSH check procedure
ios_command:
commands:
- show ip ssh | inc Enabled
register: ssh_enabled_check
ignore_errors: yes
- name: Failing because unable to connect via telnet...
fail:
msg: "Failing because unable to connect via telnet..."
when: ("'Error reading SSH protocol banner' in ssh_enabled_check.msg" and ssh_enabled_check is failed)
- name: Message out the SSH enable check procedure
debug:
msg: "SSH is enabled on the device"
changed_when: (ssh_enabled_check.stdout is defined and "'SSH Enabled' in ssh_enabled_check.stdout")
ignore_errors: yes