|
16 | 16 | options:
|
17 | 17 | host:
|
18 | 18 | description:
|
19 |
| - - Specifies the remote device FQDN or IP address to establish the SSH |
| 19 | + - Specifies the remote device FQDN or IP address to establish the HTTP(S) |
20 | 20 | connection to.
|
21 | 21 | default: inventory_hostname
|
22 | 22 | vars:
|
|
25 | 25 | type: int
|
26 | 26 | description:
|
27 | 27 | - Specifies the port on the remote device to listening for connections
|
28 |
| - when establishing the SSH connection. |
| 28 | + when establishing the HTTP(S) connection. |
| 29 | + When unspecified, will pick 80 or 443 based on the value of use_ssl |
29 | 30 | ini:
|
30 | 31 | - section: defaults
|
31 | 32 | key: remote_port
|
32 | 33 | env:
|
33 | 34 | - name: ANSIBLE_REMOTE_PORT
|
34 | 35 | vars:
|
35 | 36 | - name: ansible_port
|
| 37 | + - name: ansible_httpapi_port |
36 | 38 | network_os:
|
37 | 39 | description:
|
38 | 40 | - Configures the device platform network operating system. This value is
|
|
139 | 141 | from ansible.module_utils._text import to_bytes
|
140 | 142 | from ansible.module_utils.six import PY3
|
141 | 143 | from ansible.module_utils.six.moves import cPickle
|
| 144 | +from ansible.module_utils.six.moves.urllib.error import URLError |
142 | 145 | from ansible.module_utils.urls import open_url
|
143 | 146 | from ansible.playbook.play_context import PlayContext
|
144 | 147 | from ansible.plugins.loader import cliconf_loader, connection_loader, httpapi_loader
|
@@ -243,13 +246,13 @@ def _connect(self):
|
243 | 246 | network_os = self._play_context.network_os
|
244 | 247 |
|
245 | 248 | protocol = 'https' if self.get_option('use_ssl') else 'http'
|
246 |
| - host = self._play_context.remote_addr |
247 |
| - port = self._play_context.port or 443 if protocol == 'https' else 80 |
| 249 | + host = self.get_option('host') |
| 250 | + port = self.get_option('port') or (443 if protocol == 'https' else 80) |
248 | 251 | self._url = '%s://%s:%s' % (protocol, host, port)
|
249 | 252 |
|
250 | 253 | self._cliconf = cliconf_loader.get(network_os, self)
|
251 | 254 | if self._cliconf:
|
252 |
| - display.vvvv('loaded cliconf plugin for network_os %s' % network_os, host=self._play_context.remote_addr) |
| 255 | + display.vvvv('loaded cliconf plugin for network_os %s' % network_os, host=host) |
253 | 256 | else:
|
254 | 257 | display.vvvv('unable to load cliconf for network_os %s' % network_os)
|
255 | 258 |
|
@@ -295,9 +298,17 @@ def send(self, path, data, **kwargs):
|
295 | 298 | '''
|
296 | 299 | Sends the command to the device over api
|
297 | 300 | '''
|
298 |
| - url_kwargs = dict(url_username=self._play_context.remote_user, url_password=self._play_context.password) |
| 301 | + url_kwargs = dict( |
| 302 | + url_username=self.get_option('remote_user'), url_password=self.get_option('password'), |
| 303 | + timeout=self.get_option('timeout'), |
| 304 | + ) |
299 | 305 | url_kwargs.update(kwargs)
|
300 |
| - response = open_url(self._url + path, data=data, **url_kwargs) |
| 306 | + |
| 307 | + try: |
| 308 | + response = open_url(self._url + path, data=data, **url_kwargs) |
| 309 | + except URLError as exc: |
| 310 | + raise AnsibleConnectionFailure('Could not connect to {0}: {1}'.format(self._url, exc.reason)) |
| 311 | + |
301 | 312 | self._auth = response.info().get('Set-Cookie')
|
302 | 313 |
|
303 | 314 | return response
|
0 commit comments