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

[net_put] - Mark scp deprecated and add check_destination #674

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 38 additions & 2 deletions docs/ansible.netcommon.net_put_module.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,25 @@ Parameters
<th>Choices/<font color="blue">Defaults</font></th>
<th width="100%">Comments</th>
</tr>
<tr>
<td colspan="1">
<div class="ansibleOptionAnchor" id="parameter-"></div>
<b>check_destination</b>
<a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a>
<div style="font-size: small">
<span style="color: purple">boolean</span>
</div>
</td>
<td>
<ul style="margin: 0; padding: 0"><b>Choices:</b>
<li>no</li>
<li><div style="color: blue"><b>yes</b>&nbsp;&larr;</div></li>
</ul>
</td>
<td>
<div>Enabling this check for the file in the destination if the file exists or not and only copy the file if it does not exist.</div>
</td>
</tr>
<tr>
<td colspan="1">
<div class="ansibleOptionAnchor" id="parameter-"></div>
Expand Down Expand Up @@ -91,6 +110,7 @@ Parameters
</td>
<td>
<div>Protocol used to transfer file.</div>
<div>The choice scp is deprecated, use sftp instead. The scp option would be removed after 2028-01-01.</div>
</td>
</tr>
<tr>
Expand Down Expand Up @@ -129,16 +149,32 @@ Examples

.. code-block:: yaml

- name: copy file from ansible controller to a network device
- name: Copy file from ansible controller to a network device (defaults to scp)
ansible.netcommon.net_put:
src: running_cfg_ios1.txt

- name: copy file at root dir of flash in slot 3 of sw1(ios)
# changed: [Appliance] => changed=true
# destination: running_cfg_sw1.txt

- name: Copy file at root dir of flash in slot 3 of sw1(ios)
ansible.netcommon.net_put:
src: running_cfg_sw1.txt
protocol: sftp
dest: flash3:/running_cfg_sw1.txt

# changed: [Appliance] => changed=true
# destination: running_cfg_sw1.txt

- name: Copy file from ansible controller to a network device (does not check destination)
ansible.netcommon.net_put:
src: running_cfg_sw1.txt
protocol: scp
dest: ios_running_cfg_sw1.txt
check_destination: false

# changed: [Appliance] => changed=true
# destination: ios_running_cfg_sw1.txt




Expand Down
30 changes: 19 additions & 11 deletions plugins/action/net_put.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ def run(self, tmp=None, task_vars=None):
if mode is None:
mode = "binary"

# Check if the file is present in destination or not
check_destination = self._task.args.get("check_destination")
if check_destination is None:
check_destination = True

if mode == "text":
try:
self._handle_src_option(convert_data=False)
Expand Down Expand Up @@ -95,17 +100,19 @@ def run(self, tmp=None, task_vars=None):

if dest is None:
dest = src_file_path_name
try:
changed = self._handle_existing_file(conn, output_file, dest, proto, sock_timeout)
if changed is False:
result["changed"] = changed
result["destination"] = dest
if mode == "text":
# Cleanup tmp file expanded wih ansible vars
os.remove(output_file)
return result
except Exception as exc:
result["msg"] = "Warning: %s idempotency check failed. Check dest" % exc

if check_destination:
try:
changed = self._handle_existing_file(conn, output_file, dest, proto, sock_timeout)
if changed is False:
result["changed"] = changed
result["destination"] = dest
if mode == "text":
# Cleanup tmp file expanded wih ansible vars
os.remove(output_file)
return result
except Exception as exc:
result["msg"] = "Warning: %s idempotency check failed. Check dest" % exc

try:
conn.copy_file(
Expand All @@ -114,6 +121,7 @@ def run(self, tmp=None, task_vars=None):
proto=proto,
timeout=sock_timeout,
)
changed = True
except Exception as exc:
if to_text(exc) == "No response from server":
if network_os == "iosxr":
Expand Down
29 changes: 27 additions & 2 deletions plugins/modules/net_put.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,19 @@
protocol:
description:
- Protocol used to transfer file.
- The choice scp is deprecated, use sftp instead. The scp option would be removed
after 2028-01-01.
default: scp
choices:
- scp
- sftp
check_destination:
description:
- Enabling this check for the file in the destination if the file exists or not and
only copy the file if it does not exist.
type: bool
default: true
required: false
dest:
description:
- Specifies the destination file. The path to destination file can either be the
Expand Down Expand Up @@ -64,15 +73,31 @@
"""

EXAMPLES = """
- name: copy file from ansible controller to a network device
- name: Copy file from ansible controller to a network device (defaults to scp)
ansible.netcommon.net_put:
src: running_cfg_ios1.txt

- name: copy file at root dir of flash in slot 3 of sw1(ios)
# changed: [Appliance] => changed=true
# destination: running_cfg_sw1.txt

- name: Copy file at root dir of flash in slot 3 of sw1(ios)
ansible.netcommon.net_put:
src: running_cfg_sw1.txt
protocol: sftp
dest: flash3:/running_cfg_sw1.txt

# changed: [Appliance] => changed=true
# destination: running_cfg_sw1.txt

- name: Copy file from ansible controller to a network device (does not check destination)
ansible.netcommon.net_put:
src: running_cfg_sw1.txt
protocol: scp
dest: ios_running_cfg_sw1.txt
check_destination: false

# changed: [Appliance] => changed=true
# destination: ios_running_cfg_sw1.txt
"""

RETURN = """
Expand Down
Loading