ios_file: Don't leave leftover files behind (#42622)

* Don't leave leftover files behind

* Fix writing files in python3

* Replace nonascii.bin with one that will not pass for unicode
This commit is contained in:
Nathaniel Case 2018-07-12 02:56:14 -04:00 committed by Deepak Agrawal
parent 6a94090e7f
commit 6b162142a7
4 changed files with 28 additions and 22 deletions

View file

@ -83,9 +83,13 @@ class ActionModule(ActionBase):
src = self._task.args.get('src') src = self._task.args.get('src')
filename = str(uuid.uuid4()) filename = str(uuid.uuid4())
cwd = self._loader.get_basedir() cwd = self._loader.get_basedir()
output_file = cwd + '/' + filename output_file = os.path.join(cwd, filename)
with open(output_file, 'w') as f: try:
f.write(src) with open(output_file, 'wb') as f:
f.write(to_bytes(src, encoding='utf-8'))
except Exception:
os.remove(output_file)
raise
else: else:
try: try:
output_file = self._get_binary_src_file(src) output_file = self._get_binary_src_file(src)

View file

@ -19,14 +19,14 @@
- name: setup (remove file from localhost if present) - name: setup (remove file from localhost if present)
file: file:
path: ios_{{ ansible_host }}.cfg path: ios_{{ inventory_hostname }}.cfg
state: absent state: absent
delegate_to: localhost delegate_to: localhost
- name: get the file from device with relative destination - name: get the file from device with relative destination
net_get: net_get:
src: ios1.cfg src: ios1.cfg
dest: 'ios_{{ ansible_host }}.cfg' dest: 'ios_{{ inventory_hostname }}.cfg'
register: result register: result
- assert: - assert:
@ -36,11 +36,17 @@
- name: Idempotency check - name: Idempotency check
net_get: net_get:
src: ios1.cfg src: ios1.cfg
dest: 'ios_{{ ansible_host }}.cfg' dest: 'ios_{{ inventory_hostname }}.cfg'
register: result register: result
- assert: - assert:
that: that:
- result.changed == false - 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 }}" - debug: msg="END ios cli/net_get.yaml on connection={{ ansible_connection }}"

View file

@ -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 # Add minimal testcase to check args are passed correctly to
# implementation module and module run is successful. # implementation module and module run is successful.
@ -12,22 +13,13 @@
- username {{ ansible_ssh_user }} privilege 15 - username {{ ansible_ssh_user }} privilege 15
match: none match: none
- name: Delete existing file ios1.cfg if presen on remote host - name: Delete existing files if present on remote host
ios_command: ios_command:
commands: commands: "{{ item }}"
- command: 'delete /force ios1.cfg' loop:
ignore_errors: true - delete /force ios1.cfg
- delete /force ios.cfg
- name: Delete existing file ios.cfg if presen on remote host - delete /force nonascii.bin
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'
ignore_errors: true ignore_errors: true
- name: copy file from controller to ios + scp (Default) - name: copy file from controller to ios + scp (Default)
@ -65,6 +57,10 @@
register: result register: result
ignore_errors: true ignore_errors: true
- assert:
that:
- result.failed == true
- name: copy file with non-ascii characters to ios in default mode(binary) - name: copy file with non-ascii characters to ios in default mode(binary)
net_put: net_put:
src: nonascii.bin src: nonascii.bin