Skip to content

Commit 483df13

Browse files
Qalthosgundalow
authored andcommitted
Fixing issues with httpapi (ansible#40388)
* I seem to have forgotten the back half of tests * Set http timeout from persistent_command_timeout * Tweak URL generation and provide URL on error * Push var_options to connection process * Don't wait forever if coming from persistent * Don't send the entire contents of variables to ansible-connection
1 parent 231c358 commit 483df13

File tree

61 files changed

+46
-390
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+46
-390
lines changed

bin/ansible-connection

+14-3
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class ConnectionProcess(object):
6969
self.connection = None
7070
self._ansible_playbook_pid = ansible_playbook_pid
7171

72-
def start(self):
72+
def start(self, variables):
7373
try:
7474
messages = list()
7575
result = {}
@@ -83,7 +83,7 @@ class ConnectionProcess(object):
8383
self.play_context.private_key_file = os.path.join(self.original_path, self.play_context.private_key_file)
8484
self.connection = connection_loader.get(self.play_context.connection, self.play_context, '/dev/null',
8585
ansible_playbook_pid=self._ansible_playbook_pid)
86-
self.connection.set_options()
86+
self.connection.set_options(var_options=variables)
8787
self.connection._connect()
8888
self.connection._socket_path = self.socket_path
8989
self.srv.register(self.connection)
@@ -201,10 +201,21 @@ def main():
201201
init_data += cur_line
202202
cur_line = stdin.readline()
203203

204+
cur_line = stdin.readline()
205+
vars_data = b''
206+
207+
while cur_line.strip() != b'#END_VARS#':
208+
if cur_line == b'':
209+
raise Exception("EOF found before vars data was complete")
210+
vars_data += cur_line
211+
cur_line = stdin.readline()
212+
204213
if PY3:
205214
pc_data = cPickle.loads(init_data, encoding='bytes')
215+
variables = cPickle.loads(vars_data, encoding='bytes')
206216
else:
207217
pc_data = cPickle.loads(init_data)
218+
variables = cPickle.loads(vars_data)
208219

209220
play_context = PlayContext()
210221
play_context.deserialize(pc_data)
@@ -242,7 +253,7 @@ def main():
242253
os.close(r)
243254
wfd = os.fdopen(w, 'w')
244255
process = ConnectionProcess(wfd, play_context, socket_path, original_path, ansible_playbook_pid)
245-
process.start()
256+
process.start(variables)
246257
except Exception:
247258
messages.append(traceback.format_exc())
248259
rc = 1

lib/ansible/executor/task_executor.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,9 @@ def _get_connection(self, variables, templar):
813813
self._play_context.timeout = connection.get_option('persistent_command_timeout')
814814
display.vvvv('attempting to start connection', host=self._play_context.remote_addr)
815815
display.vvvv('using connection plugin %s' % connection.transport, host=self._play_context.remote_addr)
816-
socket_path = self._start_connection()
816+
# We don't need to send the entire contents of variables to ansible-connection
817+
filtered_vars = dict((key, value) for key, value in variables.items() if key.startswith('ansible'))
818+
socket_path = self._start_connection(filtered_vars)
817819
display.vvvv('local domain socket path is %s' % socket_path, host=self._play_context.remote_addr)
818820
setattr(connection, '_socket_path', socket_path)
819821

@@ -886,7 +888,7 @@ def _get_action_handler(self, connection, templar):
886888

887889
return handler
888890

889-
def _start_connection(self):
891+
def _start_connection(self, variables):
890892
'''
891893
Starts the persistent connection
892894
'''
@@ -918,8 +920,12 @@ def find_file_in_path(filename):
918920
# that means only protocol=0 will work.
919921
src = cPickle.dumps(self._play_context.serialize(), protocol=0)
920922
stdin.write(src)
921-
922923
stdin.write(b'\n#END_INIT#\n')
924+
925+
src = cPickle.dumps(variables, protocol=0)
926+
stdin.write(src)
927+
stdin.write(b'\n#END_VARS#\n')
928+
923929
stdin.flush()
924930

925931
(stdout, stderr) = p.communicate()

lib/ansible/plugins/connection/httpapi.py

+18-7
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
options:
1717
host:
1818
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)
2020
connection to.
2121
default: inventory_hostname
2222
vars:
@@ -25,14 +25,16 @@
2525
type: int
2626
description:
2727
- 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
2930
ini:
3031
- section: defaults
3132
key: remote_port
3233
env:
3334
- name: ANSIBLE_REMOTE_PORT
3435
vars:
3536
- name: ansible_port
37+
- name: ansible_httpapi_port
3638
network_os:
3739
description:
3840
- Configures the device platform network operating system. This value is
@@ -139,6 +141,7 @@
139141
from ansible.module_utils._text import to_bytes
140142
from ansible.module_utils.six import PY3
141143
from ansible.module_utils.six.moves import cPickle
144+
from ansible.module_utils.six.moves.urllib.error import URLError
142145
from ansible.module_utils.urls import open_url
143146
from ansible.playbook.play_context import PlayContext
144147
from ansible.plugins.loader import cliconf_loader, connection_loader, httpapi_loader
@@ -243,13 +246,13 @@ def _connect(self):
243246
network_os = self._play_context.network_os
244247

245248
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)
248251
self._url = '%s://%s:%s' % (protocol, host, port)
249252

250253
self._cliconf = cliconf_loader.get(network_os, self)
251254
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)
253256
else:
254257
display.vvvv('unable to load cliconf for network_os %s' % network_os)
255258

@@ -295,9 +298,17 @@ def send(self, path, data, **kwargs):
295298
'''
296299
Sends the command to the device over api
297300
'''
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+
)
299305
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+
301312
self._auth = response.info().get('Set-Cookie')
302313

303314
return response

lib/ansible/plugins/connection/persistent.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,12 @@ def find_file_in_path(filename):
117117
# that means only protocol=0 will work.
118118
src = cPickle.dumps(self._play_context.serialize(), protocol=0)
119119
stdin.write(src)
120-
121120
stdin.write(b'\n#END_INIT#\n')
121+
122+
src = cPickle.dumps({}, protocol=0)
123+
stdin.write(src)
124+
stdin.write(b'\n#END_VARS#\n')
125+
122126
stdin.flush()
123127

124128
(stdout, stderr) = p.communicate()

test/integration/targets/nxos_ntp/tests/common/sanity.yaml

-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
vrf_name: management
1212
source_addr: 5.5.5.5
1313
state: absent
14-
provider: "{{ connection }}"
1514
ignore_errors: yes
1615

1716
- block:
@@ -24,7 +23,6 @@
2423
vrf_name: management
2524
source_addr: 5.5.5.5
2625
state: present
27-
provider: "{{ connection }}"
2826
register: result
2927

3028
- assert: &true
@@ -47,7 +45,6 @@
4745
vrf_name: default
4846
source_addr: default
4947
state: present
50-
provider: "{{ connection }}"
5148
register: result
5249

5350
- assert: *true
@@ -76,7 +73,6 @@
7673
peer: 1.2.3.4
7774
prefer: enabled
7875
state: present
79-
provider: "{{ connection }}"
8076
register: result
8177

8278
- assert: *true
@@ -91,7 +87,6 @@
9187
nxos_ntp: &config3
9288
source_int: default
9389
state: present
94-
provider: "{{ connection }}"
9590
register: result
9691

9792
- assert: *true

test/integration/targets/nxos_ntp_auth/tests/common/sanity.yaml

-9
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
key_id: 32
99
md5string: hello
1010
state: absent
11-
provider: "{{ connection }}"
1211
ignore_errors: yes
1312

1413
- block:
@@ -19,7 +18,6 @@
1918
md5string: hello
2019
authentication: off
2120
state: present
22-
provider: "{{ connection }}"
2321
register: result
2422

2523
- assert: &true
@@ -32,7 +30,6 @@
3230
md5string: hello
3331
authentication: off
3432
state: absent
35-
provider: "{{ connection }}"
3633
register: result
3734

3835
- assert: *true
@@ -43,7 +40,6 @@
4340
md5string: hello
4441
auth_type: encrypt
4542
state: present
46-
provider: "{{ connection }}"
4743
register: result
4844

4945
- assert: *true
@@ -60,7 +56,6 @@
6056
nxos_ntp_auth: &authon
6157
authentication: on
6258
state: present
63-
provider: "{{ connection }}"
6459
register: result
6560

6661
- assert: *true
@@ -75,7 +70,6 @@
7570
nxos_ntp_auth: &authoff
7671
authentication: off
7772
state: present
78-
provider: "{{ connection }}"
7973
register: result
8074

8175
- assert: *true
@@ -91,7 +85,6 @@
9185
key_id: 32
9286
trusted_key: true
9387
state: present
94-
provider: "{{ connection }}"
9588
register: result
9689

9790
- assert: *true
@@ -107,7 +100,6 @@
107100
key_id: 32
108101
trusted_key: false
109102
state: present
110-
provider: "{{ connection }}"
111103
register: result
112104

113105
- assert: *true
@@ -125,7 +117,6 @@
125117
auth_type: encrypt
126118
authentication: on
127119
state: absent
128-
provider: "{{ connection }}"
129120
register: result
130121

131122
- assert: *true

test/integration/targets/nxos_ntp_options/tests/common/sanity.yaml

-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
- name: "Apply default ntp config"
77
nxos_ntp_options: &default
88
state: absent
9-
provider: "{{ connection }}"
109
ignore_errors: yes
1110

1211
- block:
@@ -16,7 +15,6 @@
1615
master: true
1716
logging: true
1817
state: present
19-
provider: "{{ connection }}"
2018
register: result
2119

2220
- assert: &true
@@ -36,7 +34,6 @@
3634
master: true
3735
stratum: 10
3836
state: present
39-
provider: "{{ connection }}"
4037
register: result
4138

4239
- assert: *true
@@ -53,7 +50,6 @@
5350
stratum: 10
5451
logging: false
5552
state: present
56-
provider: "{{ connection }}"
5753
register: result
5854

5955
- assert: *true
@@ -69,7 +65,6 @@
6965
master: false
7066
logging: true
7167
state: present
72-
provider: "{{ connection }}"
7368
register: result
7469

7570
- assert: *true

test/integration/targets/nxos_nxapi/tests/cli/configure.yaml

-4
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,18 @@
77
- name: Setup - put NXAPI in stopped state
88
nxos_nxapi:
99
state: absent
10-
provider: "{{ cli }}"
1110

1211
- name: Configure NXAPI
1312
nxos_nxapi:
1413
enable_http: no
1514
enable_sandbox: "{{nxapi_sandbox_option|default(omit)}}"
1615
enable_https: yes
1716
https_port: 9443
18-
provider: "{{ cli }}"
1917
register: result
2018

2119
- nxos_command:
2220
commands:
2321
- show nxapi | json
24-
provider: "{{ cli }}"
2522
register: result
2623

2724
- include: targets/nxos_nxapi/tasks/platform/n7k/assert_changes.yaml
@@ -39,7 +36,6 @@
3936
enable_sandbox: "{{nxapi_sandbox_option|default(omit)}}"
4037
enable_https: yes
4138
https_port: 9443
42-
provider: "{{ cli }}"
4339
register: result
4440

4541
- name: Assert configuration is idempotent

test/integration/targets/nxos_nxapi/tests/cli/disable.yaml

-3
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@
44
- name: Disable NXAPI
55
nxos_nxapi:
66
state: absent
7-
provider: "{{ cli }}"
87
register: result
98

109
- name: Check NXAPI state
1110
nxos_command:
1211
commands:
1312
- show feature | grep nxapi
14-
provider: "{{ cli }}"
1513
register: result
1614

1715
- name: Assert NXAPI is disabled
@@ -23,7 +21,6 @@
2321
nxos_nxapi:
2422
state:
2523
absent
26-
provider: "{{ cli }}"
2724
register: result
2825

2926
- name: Assert idempotence

0 commit comments

Comments
 (0)