From b97e31dd552a3c8957e046590b5172ebb93bbde9 Mon Sep 17 00:00:00 2001 From: Alexei Znamensky <103110+russoz@users.noreply.github.com> Date: Mon, 5 Apr 2021 19:22:06 +1200 Subject: [PATCH] rewritten as list literals (#2160) * rewritten as list literals * added changelog fragment --- changelogs/fragments/2160-list-literals.yml | 11 +++++ plugins/lookup/hiera.py | 4 +- plugins/module_utils/known_hosts.py | 11 ++--- plugins/modules/cloud/smartos/nictagadm.py | 16 ++------ .../cloud/smartos/smartos_image_info.py | 5 +-- plugins/modules/files/xattr.py | 9 ++-- plugins/modules/net_tools/ipwcli_dns.py | 24 +++++++---- plugins/modules/packaging/os/svr4pkg.py | 3 +- plugins/modules/storage/zfs/zfs_facts.py | 15 ++----- plugins/modules/storage/zfs/zpool_facts.py | 10 +---- plugins/modules/system/beadm.py | 41 ++++--------------- 11 files changed, 55 insertions(+), 94 deletions(-) create mode 100644 changelogs/fragments/2160-list-literals.yml diff --git a/changelogs/fragments/2160-list-literals.yml b/changelogs/fragments/2160-list-literals.yml new file mode 100644 index 0000000000..661b1e322e --- /dev/null +++ b/changelogs/fragments/2160-list-literals.yml @@ -0,0 +1,11 @@ +minor_changes: + - hiera lookup - minor refactor converting multiple statements to a single list literal (https://github.com/ansible-collections/community.general/pull/2160). + - known_hosts module utils - minor refactor converting multiple statements to a single list literal (https://github.com/ansible-collections/community.general/pull/2160). + - nictagadm - minor refactor converting multiple statements to a single list literal (https://github.com/ansible-collections/community.general/pull/2160). + - smartos_image_info - minor refactor converting multiple statements to a single list literal (https://github.com/ansible-collections/community.general/pull/2160). + - xattr - minor refactor converting multiple statements to a single list literal (https://github.com/ansible-collections/community.general/pull/2160). + - ipwcli_dns - minor refactor converting multiple statements to a single list literal (https://github.com/ansible-collections/community.general/pull/2160). + - svr4pkg - minor refactor converting multiple statements to a single list literal (https://github.com/ansible-collections/community.general/pull/2160). + - zfs_facts - minor refactor converting multiple statements to a single list literal (https://github.com/ansible-collections/community.general/pull/2160). + - zpool_facts - minor refactor converting multiple statements to a single list literal (https://github.com/ansible-collections/community.general/pull/2160). + - beadm - minor refactor converting multiple statements to a single list literal (https://github.com/ansible-collections/community.general/pull/2160). diff --git a/plugins/lookup/hiera.py b/plugins/lookup/hiera.py index e757f41215..1ce82d7bd6 100644 --- a/plugins/lookup/hiera.py +++ b/plugins/lookup/hiera.py @@ -84,7 +84,5 @@ class Hiera(object): class LookupModule(LookupBase): def run(self, terms, variables=''): hiera = Hiera() - ret = [] - - ret.append(hiera.get(terms)) + ret = [hiera.get(terms)] return ret diff --git a/plugins/module_utils/known_hosts.py b/plugins/module_utils/known_hosts.py index 96f91ba80e..efd311eb51 100644 --- a/plugins/module_utils/known_hosts.py +++ b/plugins/module_utils/known_hosts.py @@ -87,11 +87,12 @@ def not_in_host_file(self, host): user_host_file = "~/.ssh/known_hosts" user_host_file = os.path.expanduser(user_host_file) - host_file_list = [] - host_file_list.append(user_host_file) - host_file_list.append("/etc/ssh/ssh_known_hosts") - host_file_list.append("/etc/ssh/ssh_known_hosts2") - host_file_list.append("/etc/openssh/ssh_known_hosts") + host_file_list = [ + user_host_file, + "/etc/ssh/ssh_known_hosts", + "/etc/ssh/ssh_known_hosts2", + "/etc/openssh/ssh_known_hosts", + ] hfiles_not_found = 0 for hf in host_file_list: diff --git a/plugins/modules/cloud/smartos/nictagadm.py b/plugins/modules/cloud/smartos/nictagadm.py index 7db7c5ea45..05aba6f188 100644 --- a/plugins/modules/cloud/smartos/nictagadm.py +++ b/plugins/modules/cloud/smartos/nictagadm.py @@ -119,20 +119,13 @@ class NicTag(object): return is_mac(self.mac.lower()) def nictag_exists(self): - cmd = [self.nictagadm_bin] - - cmd.append('exists') - cmd.append(self.name) - + cmd = [self.nictagadm_bin, 'exists', self.name] (rc, dummy, dummy) = self.module.run_command(cmd) return rc == 0 def add_nictag(self): - cmd = [self.nictagadm_bin] - - cmd.append('-v') - cmd.append('add') + cmd = [self.nictagadm_bin, '-v', 'add'] if self.etherstub: cmd.append('-l') @@ -150,10 +143,7 @@ class NicTag(object): return self.module.run_command(cmd) def delete_nictag(self): - cmd = [self.nictagadm_bin] - - cmd.append('-v') - cmd.append('delete') + cmd = [self.nictagadm_bin, '-v', 'delete'] if self.force: cmd.append('-f') diff --git a/plugins/modules/cloud/smartos/smartos_image_info.py b/plugins/modules/cloud/smartos/smartos_image_info.py index 473d345ad8..45d8e34085 100644 --- a/plugins/modules/cloud/smartos/smartos_image_info.py +++ b/plugins/modules/cloud/smartos/smartos_image_info.py @@ -72,10 +72,7 @@ class ImageFacts(object): self.filters = module.params['filters'] def return_all_installed_images(self): - cmd = [self.module.get_bin_path('imgadm')] - - cmd.append('list') - cmd.append('-j') + cmd = [self.module.get_bin_path('imgadm'), 'list', '-j'] if self.filters: cmd.append(self.filters) diff --git a/plugins/modules/files/xattr.py b/plugins/modules/files/xattr.py index 0d5f9f46f3..7691f30905 100644 --- a/plugins/modules/files/xattr.py +++ b/plugins/modules/files/xattr.py @@ -98,9 +98,8 @@ from ansible.module_utils._text import to_native def get_xattr_keys(module, path, follow): - cmd = [module.get_bin_path('getfattr', True)] - # prevents warning and not sure why it's not default - cmd.append('--absolute-names') + cmd = [module.get_bin_path('getfattr', True), '--absolute-names'] + if not follow: cmd.append('-h') cmd.append(path) @@ -109,10 +108,8 @@ def get_xattr_keys(module, path, follow): def get_xattr(module, path, key, follow): + cmd = [module.get_bin_path('getfattr', True), '--absolute-names'] - cmd = [module.get_bin_path('getfattr', True)] - # prevents warning and not sure why it's not default - cmd.append('--absolute-names') if not follow: cmd.append('-h') if key is None: diff --git a/plugins/modules/net_tools/ipwcli_dns.py b/plugins/modules/net_tools/ipwcli_dns.py index 355c70346f..284f3ad810 100644 --- a/plugins/modules/net_tools/ipwcli_dns.py +++ b/plugins/modules/net_tools/ipwcli_dns.py @@ -205,9 +205,11 @@ class ResourceRecord(object): def list_record(self, record): # check if the record exists via list on ipwcli search = 'list %s' % (record.replace(';', '&&').replace('set', 'where')) - cmd = [self.module.get_bin_path('ipwcli', True)] - cmd.append('-user=%s' % (self.user)) - cmd.append('-password=%s' % (self.password)) + cmd = [ + self.module.get_bin_path('ipwcli', True), + '-user=%s' % self.user, + '-password=%s' % self.password, + ] rc, out, err = self.module.run_command(cmd, data=search) if 'Invalid username or password' in out: @@ -222,9 +224,11 @@ class ResourceRecord(object): def deploy_record(self, record): # check what happens if create fails on ipworks stdin = 'create %s' % (record) - cmd = [self.module.get_bin_path('ipwcli', True)] - cmd.append('-user=%s' % (self.user)) - cmd.append('-password=%s' % (self.password)) + cmd = [ + self.module.get_bin_path('ipwcli', True), + '-user=%s' % self.user, + '-password=%s' % self.password, + ] rc, out, err = self.module.run_command(cmd, data=stdin) if 'Invalid username or password' in out: @@ -238,9 +242,11 @@ class ResourceRecord(object): def delete_record(self, record): # check what happens if create fails on ipworks stdin = 'delete %s' % (record.replace(';', '&&').replace('set', 'where')) - cmd = [self.module.get_bin_path('ipwcli', True)] - cmd.append('-user=%s' % (self.user)) - cmd.append('-password=%s' % (self.password)) + cmd = [ + self.module.get_bin_path('ipwcli', True), + '-user=%s' % self.user, + '-password=%s' % self.password, + ] rc, out, err = self.module.run_command(cmd, data=stdin) if 'Invalid username or password' in out: diff --git a/plugins/modules/packaging/os/svr4pkg.py b/plugins/modules/packaging/os/svr4pkg.py index 21d17f4de2..ea3cd7d468 100644 --- a/plugins/modules/packaging/os/svr4pkg.py +++ b/plugins/modules/packaging/os/svr4pkg.py @@ -108,8 +108,7 @@ from ansible.module_utils.basic import AnsibleModule def package_installed(module, name, category): - cmd = [module.get_bin_path('pkginfo', True)] - cmd.append('-q') + cmd = [module.get_bin_path('pkginfo', True), '-q'] if category: cmd.append('-c') cmd.append(name) diff --git a/plugins/modules/storage/zfs/zfs_facts.py b/plugins/modules/storage/zfs/zfs_facts.py index 930214743a..cb106de111 100644 --- a/plugins/modules/storage/zfs/zfs_facts.py +++ b/plugins/modules/storage/zfs/zfs_facts.py @@ -175,10 +175,7 @@ class ZFSFacts(object): self.facts = [] def dataset_exists(self): - cmd = [self.module.get_bin_path('zfs')] - - cmd.append('list') - cmd.append(self.name) + cmd = [self.module.get_bin_path('zfs'), 'list', self.name] (rc, out, err) = self.module.run_command(cmd) @@ -188,10 +185,7 @@ class ZFSFacts(object): return False def get_facts(self): - cmd = [self.module.get_bin_path('zfs')] - - cmd.append('get') - cmd.append('-H') + cmd = [self.module.get_bin_path('zfs'), 'get', '-H'] if self.parsable: cmd.append('-p') if self.recurse: @@ -202,10 +196,7 @@ class ZFSFacts(object): if self.type: cmd.append('-t') cmd.append(self.type) - cmd.append('-o') - cmd.append('name,property,value') - cmd.append(self.properties) - cmd.append(self.name) + cmd.extend(['-o', 'name,property,value', self.properties, self.name]) (rc, out, err) = self.module.run_command(cmd) diff --git a/plugins/modules/storage/zfs/zpool_facts.py b/plugins/modules/storage/zfs/zpool_facts.py index eced85000e..ed3d6cf965 100644 --- a/plugins/modules/storage/zfs/zpool_facts.py +++ b/plugins/modules/storage/zfs/zpool_facts.py @@ -134,10 +134,7 @@ class ZPoolFacts(object): self.facts = [] def pool_exists(self): - cmd = [self.module.get_bin_path('zpool')] - - cmd.append('list') - cmd.append(self.name) + cmd = [self.module.get_bin_path('zpool'), 'list', self.name] (rc, out, err) = self.module.run_command(cmd) @@ -147,10 +144,7 @@ class ZPoolFacts(object): return False def get_facts(self): - cmd = [self.module.get_bin_path('zpool')] - - cmd.append('get') - cmd.append('-H') + cmd = [self.module.get_bin_path('zpool'), 'get', '-H'] if self.parsable: cmd.append('-p') cmd.append('-o') diff --git a/plugins/modules/system/beadm.py b/plugins/modules/system/beadm.py index d34c5e7d96..d89ca79af1 100644 --- a/plugins/modules/system/beadm.py +++ b/plugins/modules/system/beadm.py @@ -154,9 +154,7 @@ class BE(object): self.is_freebsd = os.uname()[0] == 'FreeBSD' def _beadm_list(self): - cmd = [self.module.get_bin_path('beadm')] - cmd.append('list') - cmd.append('-H') + cmd = [self.module.get_bin_path('beadm'), 'list', '-H'] if '@' in self.name: cmd.append('-s') return self.module.run_command(cmd) @@ -218,42 +216,26 @@ class BE(object): return False def activate_be(self): - cmd = [self.module.get_bin_path('beadm')] - - cmd.append('activate') - cmd.append(self.name) - + cmd = [self.module.get_bin_path('beadm'), 'activate', self.name] return self.module.run_command(cmd) def create_be(self): - cmd = [self.module.get_bin_path('beadm')] - - cmd.append('create') + cmd = [self.module.get_bin_path('beadm'), 'create'] if self.snapshot: - cmd.append('-e') - cmd.append(self.snapshot) - + cmd.extend(['-e', self.snapshot]) if not self.is_freebsd: if self.description: - cmd.append('-d') - cmd.append(self.description) - + cmd.extend(['-d', self.description]) if self.options: - cmd.append('-o') - cmd.append(self.options) + cmd.extend(['-o', self.options]) cmd.append(self.name) return self.module.run_command(cmd) def destroy_be(self): - cmd = [self.module.get_bin_path('beadm')] - - cmd.append('destroy') - cmd.append('-F') - cmd.append(self.name) - + cmd = [self.module.get_bin_path('beadm'), 'destroy', '-F', self.name] return self.module.run_command(cmd) def is_mounted(self): @@ -276,10 +258,7 @@ class BE(object): return False def mount_be(self): - cmd = [self.module.get_bin_path('beadm')] - - cmd.append('mount') - cmd.append(self.name) + cmd = [self.module.get_bin_path('beadm'), 'mount', self.name] if self.mountpoint: cmd.append(self.mountpoint) @@ -287,9 +266,7 @@ class BE(object): return self.module.run_command(cmd) def unmount_be(self): - cmd = [self.module.get_bin_path('beadm')] - - cmd.append('unmount') + cmd = [self.module.get_bin_path('beadm'), 'unmount'] if self.force: cmd.append('-f') cmd.append(self.name)