Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ios_file: Don't leave leftover files behind #42622

Merged
merged 3 commits into from
Jul 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 7 additions & 3 deletions lib/ansible/plugins/action/net_put.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,13 @@ def run(self, tmp=None, task_vars=None):
src = self._task.args.get('src')
filename = str(uuid.uuid4())
cwd = self._loader.get_basedir()
output_file = cwd + '/' + filename
with open(output_file, 'w') as f:
f.write(src)
output_file = os.path.join(cwd, filename)
try:
with open(output_file, 'wb') as f:
f.write(to_bytes(src, encoding='utf-8'))
except Exception:
os.remove(output_file)
raise
else:
try:
output_file = self._get_binary_src_file(src)
Expand Down
Binary file modified test/integration/targets/ios_file/nonascii.bin
Binary file not shown.
12 changes: 9 additions & 3 deletions test/integration/targets/ios_file/tests/cli/net_get.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@

- name: setup (remove file from localhost if present)
file:
path: ios_{{ ansible_host }}.cfg
path: ios_{{ inventory_hostname }}.cfg
state: absent
delegate_to: localhost

- name: get the file from device with relative destination
net_get:
src: ios1.cfg
dest: 'ios_{{ ansible_host }}.cfg'
dest: 'ios_{{ inventory_hostname }}.cfg'
register: result

- assert:
Expand All @@ -36,11 +36,17 @@
- name: Idempotency check
net_get:
src: ios1.cfg
dest: 'ios_{{ ansible_host }}.cfg'
dest: 'ios_{{ inventory_hostname }}.cfg'
register: result

- assert:
that:
- result.changed == false

- name: setup (remove file from localhost if present)
file:
path: ios_{{ inventory_hostname }}.cfg
state: absent
delegate_to: localhost

- debug: msg="END ios cli/net_get.yaml on connection={{ ansible_connection }}"
28 changes: 12 additions & 16 deletions test/integration/targets/ios_file/tests/cli/net_put.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
- debug: msg="START ios cli/net_put.yaml on connection={{ ansible_connection }}"
- debug:
msg: "START ios cli/net_put.yaml on connection={{ ansible_connection }}"

# Add minimal testcase to check args are passed correctly to
# implementation module and module run is successful.
Expand All @@ -12,22 +13,13 @@
- username {{ ansible_ssh_user }} privilege 15
match: none

- name: Delete existing file ios1.cfg if presen on remote host
- name: Delete existing files if present on remote host
ios_command:
commands:
- command: 'delete /force ios1.cfg'
ignore_errors: true

- name: Delete existing file ios.cfg if presen on remote host
ios_command:
commands:
- command: 'delete /force ios.cfg'
ignore_errors: true

- name: Delete existing file nonascii.bin if presen on remote host
ios_command:
commands:
- command: 'delete /force nonascii.bin'
commands: "{{ item }}"
loop:
- delete /force ios1.cfg
- delete /force ios.cfg
- delete /force nonascii.bin
ignore_errors: true

- name: copy file from controller to ios + scp (Default)
Expand Down Expand Up @@ -65,6 +57,10 @@
register: result
ignore_errors: true

- assert:
that:
- result.failed == true

- name: copy file with non-ascii characters to ios in default mode(binary)
net_put:
src: nonascii.bin
Expand Down