Skip to content

Commit

Permalink
Merge pull request #1 from ansible-collections/main
Browse files Browse the repository at this point in the history
update code
  • Loading branch information
gemingqi committed Dec 5, 2021
2 parents 64a3348 + 745d91e commit 018d8b8
Show file tree
Hide file tree
Showing 13 changed files with 111 additions and 58 deletions.
1 change: 1 addition & 0 deletions .github/BOTMETA.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
automerge: false
notifications: true
files:
$actions/aireos.py:
labels: aireos cisco networking
Expand Down
2 changes: 2 additions & 0 deletions 103-nclu-check-diff.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- nclu - add support for ``check_mode`` and ``--diff`` (https://github.com/ansible-collections/community.network/pull/103).
42 changes: 30 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,32 +84,50 @@ See [Ansible Using collections](https://docs.ansible.com/ansible/latest/user_gui

## Using this collection

You can call modules by their Fully Qualified Collection Name (FQCN), such as `community.network.routeros_command`.
The following example task replaces configuration changes in the existing configuration on a network device, using the FQCN:
You can call modules by their Fully Qualified Collection Name (FQCN), such as `community.network.a10_server`.
The following example task creates a new server load balancer object on an A10 Networks device, using the FQCN:

```yaml
---
- name: run command on remote devices
community.network.routeros_command:
commands: /system routerboard print

- name: Create a new server
community.network.a10_server:
host: a10.mydomain.com
username: myadmin
password: mypassword
partition: mypartition
server: test
server_ip: 192.0.2.100
server_ports:
- port_num: 8080
protocol: tcp
- port_num: 8443
protocol: TCP
```
Alternately, you can call modules by their short name if you list the `community.network` collection in the playbook's `collections`, as follows:

```yaml
---
- hosts: routeros01
gather_facts: false
connection: network_cli
- hosts: "{{desired_inventory_group}}"
connection: local
collections:
- community.network
tasks:
- name: Gather facts from the device.
routeros_facts:
gather_subset: all
- name: Create a new server
a10_server:
host: a10.mydomain.com
username: myadmin
password: mypassword
partition: mypartition
server: test
server_ip: 192.0.2.100
server_ports:
- port_num: 8080
protocol: tcp
- port_num: 8443
protocol: TCP
```

### See Also:
Expand Down
3 changes: 3 additions & 0 deletions changelogs/fragments/189-append-save-command-into-result.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
minor_changes:
- community.network.edgeos_config - append save command into result (https://github.com/ansible-collections/community.network/pull/189)
4 changes: 4 additions & 0 deletions changelogs/fragments/313_fix_broken_ce_switchport_module.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
bugfixes:
- community.network.ce_switchport - fix error causing by ``KeyError:`` ``host`` due to properties aren't
used anywhere (https://github.com/ansible-collections/community.network/issues/313)
1 change: 1 addition & 0 deletions plugins/modules/network/avi/avi_useraccount.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ def main():
first_pwd = api_creds.password
second_pwd = old_password
password_changed = False
rsp = None
try:
api = ApiSession.get_session(
api_creds.controller, api_creds.username,
Expand Down
24 changes: 5 additions & 19 deletions plugins/modules/network/cloudengine/ce_switchport.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@
in the C(port trunk allow-pass vlan) command. Use verbose mode to see commands sent.
- When C(state=unconfigured), the interface will result with having a default Layer 2 interface, i.e. vlan 1 in access mode.
- This module requires the netconf system service be enabled on the remote device being managed.
- Recommended connection is C(netconf).
- This module also works with C(local) connections for legacy playbooks.
- Recommended connection is C(ansible.netcommon.netconf).
options:
interface:
description:
Expand Down Expand Up @@ -72,53 +71,45 @@
EXAMPLES = '''
- name: Switchport module test
hosts: cloudengine
connection: local
gather_facts: no
vars:
cli:
host: "{{ inventory_hostname }}"
port: "{{ ansible_ssh_port }}"
username: "{{ username }}"
password: "{{ password }}"
transport: cli
ansible_user: root
ansible_password: PASSWORD
ansible_connection: ansible.netcommon.netconf
ansible_network_os: community.network.ce
tasks:
- name: Ensure 10GE1/0/22 is in its default switchport state
community.network.ce_switchport:
interface: 10GE1/0/22
state: unconfigured
provider: '{{ cli }}'
- name: Ensure 10GE1/0/22 is configured for access vlan 20
community.network.ce_switchport:
interface: 10GE1/0/22
mode: access
default_vlan: 20
provider: '{{ cli }}'
- name: Ensure 10GE1/0/22 only has vlans 5-10 as trunk vlans
community.network.ce_switchport:
interface: 10GE1/0/22
mode: trunk
pvid_vlan: 10
trunk_vlans: 5-10
provider: '{{ cli }}'
- name: Ensure 10GE1/0/22 is a trunk port and ensure 2-50 are being tagged (doesn't mean others aren't also being tagged)
community.network.ce_switchport:
interface: 10GE1/0/22
mode: trunk
pvid_vlan: 10
trunk_vlans: 2-50
provider: '{{ cli }}'
- name: Ensure these VLANs are not being tagged on the trunk
community.network.ce_switchport:
interface: 10GE1/0/22
mode: trunk
trunk_vlans: 51-4000
state: absent
provider: '{{ cli }}'
'''

RETURN = '''
Expand Down Expand Up @@ -334,11 +325,6 @@ def __init__(self, argument_spec):
self.untagged_vlans = self.module.params['untagged_vlans']
self.tagged_vlans = self.module.params['tagged_vlans']

# host info
self.host = self.module.params['host']
self.username = self.module.params['username']
self.port = self.module.params['port']

# state
self.changed = False
self.updates_cmd = list()
Expand Down
25 changes: 21 additions & 4 deletions plugins/modules/network/cumulus/nclu.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
- Commit description that will be recorded to the commit log if
I(commit) or I(atomic) are true.
default: "Ansible-originated commit"
notes:
- Supports check_mode. Note that when using check_mode, I(abort) is always true.
'''

EXAMPLES = '''
Expand Down Expand Up @@ -203,14 +205,19 @@ def run_nclu(module, command_list, command_string, commit, atomic, abort, descri
output = "\n".join(output_lines)

# If pending changes changed, report a change.
diff = {}
after = check_pending(module)
if before == after:
_changed = False
else:
_changed = True
diff = {"prepared": after}

if module.check_mode:
command_helper(module, "abort")

# Do the commit.
if (do_commit and _changed):
if (do_commit and _changed and not module.check_mode):
result = command_helper(module, "commit description '%s'" % description)
if "commit ignored" in result:
_changed = False
Expand All @@ -220,7 +227,7 @@ def run_nclu(module, command_list, command_string, commit, atomic, abort, descri
elif do_abort:
command_helper(module, "abort")

return _changed, output
return _changed, output, diff


def main(testing=False):
Expand All @@ -231,6 +238,7 @@ def main(testing=False):
abort=dict(required=False, type='bool', default=False),
commit=dict(required=False, type='bool', default=False),
atomic=dict(required=False, type='bool', default=False)),
supports_check_mode=True,
mutually_exclusive=[('commands', 'template'),
('commit', 'atomic'),
('abort', 'atomic')]
Expand All @@ -242,9 +250,18 @@ def main(testing=False):
abort = module.params.get('abort')
description = module.params.get('description')

_changed, output = run_nclu(module, command_list, command_string, commit, atomic, abort, description)
if module.check_mode:
commit = False

_changed, output, diff = run_nclu(module, command_list, command_string, commit, atomic, abort, description)
result = {
"changed": _changed,
"msg": output,
"diff": diff
}

if not testing:
module.exit_json(changed=_changed, msg=output)
module.exit_json(**result)
elif testing:
return {"changed": _changed, "msg": output}

Expand Down
2 changes: 2 additions & 0 deletions plugins/modules/network/edgeos/edgeos_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,8 @@ def main():
if module.params['save']:
diff = run_commands(module, commands=['configure', 'compare saved'])[1]
if diff != '[edit]':
if 'commands' in result:
result['commands'].append('save')
if not module.check_mode:
run_commands(module, commands=['save'])
result['changed'] = True
Expand Down
55 changes: 42 additions & 13 deletions tests/unit/plugins/modules/network/cumulus/test_nclu.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ def reset(self):
self.mocks = {}
self.pending = ""
self.last_commit = ""
self.check_mode = False


def skipUnlessNcluInstalled(original_function):
Expand Down Expand Up @@ -137,16 +138,16 @@ def test_command_helper_no_error_msg(self):

def test_empty_run(self):
module = FakeModule()
changed, output = nclu.run_nclu(module, None, None, False, False, False, "")
changed, output, diff = nclu.run_nclu(module, None, None, False, False, False, "")
self.assertEqual(module.command_history, ['/usr/bin/net pending',
'/usr/bin/net pending'])
self.assertEqual(module.fail_code, {})
self.assertEqual(changed, False)

def test_command_list(self):
module = FakeModule()
changed, output = nclu.run_nclu(module, ['add int swp1', 'add int swp2'],
None, False, False, False, "")
changed, output, diff = nclu.run_nclu(module, ['add int swp1', 'add int swp2'],
None, False, False, False, "")

self.assertEqual(module.command_history, ['/usr/bin/net pending',
'/usr/bin/net add int swp1',
Expand All @@ -158,9 +159,8 @@ def test_command_list(self):

def test_command_list_commit(self):
module = FakeModule()
changed, output = nclu.run_nclu(module,
['add int swp1', 'add int swp2'],
None, True, False, False, "committed")
changed, output, diff = nclu.run_nclu(module, ['add int swp1', 'add int swp2'],
None, True, False, False, "committed")

self.assertEqual(module.command_history, ['/usr/bin/net pending',
'/usr/bin/net add int swp1',
Expand All @@ -174,9 +174,8 @@ def test_command_list_commit(self):

def test_command_atomic(self):
module = FakeModule()
changed, output = nclu.run_nclu(module,
['add int swp1', 'add int swp2'],
None, False, True, False, "atomically")
changed, output, diff = nclu.run_nclu(module, ['add int swp1', 'add int swp2'],
None, False, True, False, "atomically")

self.assertEqual(module.command_history, ['/usr/bin/net abort',
'/usr/bin/net pending',
Expand All @@ -198,9 +197,9 @@ def test_command_abort_first(self):

def test_command_template_commit(self):
module = FakeModule()
changed, output = nclu.run_nclu(module, None,
" add int swp1\n add int swp2",
True, False, False, "committed")
changed, output, diff = nclu.run_nclu(module, None,
" add int swp1\n add int swp2",
True, False, False, "committed")

self.assertEqual(module.command_history, ['/usr/bin/net pending',
'/usr/bin/net add int swp1',
Expand All @@ -214,10 +213,40 @@ def test_command_template_commit(self):

def test_commit_ignored(self):
module = FakeModule()
changed, output = nclu.run_nclu(module, None, None, True, False, False, "ignore me")
changed, output, diff = nclu.run_nclu(module, None, None, True, False, False, "ignore me")

self.assertEqual(module.command_history, ['/usr/bin/net pending',
'/usr/bin/net pending'])
self.assertEqual(len(module.pending), 0)
self.assertEqual(module.fail_code, {})
self.assertEqual(changed, False)

def test_check_mode(self):
module = FakeModule()
module.check_mode = True
changed, output, diff = nclu.run_nclu(module, ['add int swp1', 'add int swp2'],
None, True, False, False, '')

self.assertEqual(module.command_history, ["/usr/bin/net pending",
"/usr/bin/net add int swp1",
"/usr/bin/net add int swp2",
"/usr/bin/net pending",
"/usr/bin/net abort"])
self.assertEqual(len(module.pending), 0)
self.assertEqual(module.fail_code, {})
self.assertEqual(changed, True)

def test_diff(self):
module = FakeModule()
changed, output, diff = nclu.run_nclu(module, ['add int swp1', 'add int swp2'],
None, False, False, False, '')

print(module.command_history)
self.assertEqual(diff, {'prepared': '/usr/bin/net add int swp1/usr/bin/net add int swp2'})
self.assertEqual(module.command_history, ["/usr/bin/net pending",
"/usr/bin/net add int swp1",
"/usr/bin/net add int swp2",
"/usr/bin/net pending"])
self.assertNotEqual(len(module.pending), 0)
self.assertEqual(module.fail_code, {})
self.assertEqual(changed, True)
6 changes: 0 additions & 6 deletions tests/unit/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@ unittest2 ; python_version <= '2.6'
# requirements for ftd module_utils
firepower-kickstart ; python_version >= '3.6' and python_version < '3.9' # Python 3.6+ only; dependency does not work with 3.9 yet

# requirements for FortiManager modules
pyFMG

# requirements for Nuage modules - CURRENTLY BROKEN!
# vspk
# bambou

# requirements for routeros_api module
librouteros ; python_version >= '3.6'
1 change: 0 additions & 1 deletion tests/utils/constraints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ openshift >= 0.6.2, < 0.9.0 # merge_type support
virtualenv < 16.0.0 ; python_version < '2.7' # virtualenv 16.0.0 and later require python 2.7 or later
pathspec < 0.6.0 ; python_version < '2.7' # pathspec 0.6.0 and later require python 2.7 or later
pyopenssl < 18.0.0 ; python_version < '2.7' # pyOpenSSL 18.0.0 and later require python 2.7 or later
pyfmg == 0.6.1 # newer versions do not pass current unit tests
pyyaml < 5.1 ; python_version < '2.7' # pyyaml 5.1 and later require python 2.7 or later
pycparser < 2.19 ; python_version < '2.7' # pycparser 2.19 and later require python 2.7 or later
mock >= 2.0.0 # needed for features backported from Python 3.6 unittest.mock (assert_called, assert_called_once...)
Expand Down
3 changes: 0 additions & 3 deletions tests/utils/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ f5-sdk ; python_version >= '2.7'
f5-icontrol-rest ; python_version >= '2.7'
deepdiff

# requirement for Fortinet specific modules
pyFMG

# requirement for aci_rest module
xmljson

Expand Down

0 comments on commit 018d8b8

Please sign in to comment.