diff --git a/plugins/modules/link_agent_linux.py b/plugins/modules/link_agent_linux.py index 881f058..b51cb27 100644 --- a/plugins/modules/link_agent_linux.py +++ b/plugins/modules/link_agent_linux.py @@ -99,9 +99,8 @@ """ -import subprocess - from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils.common.text.converters import to_native def link_nessus_agent(module): @@ -114,21 +113,13 @@ def link_nessus_agent(module): become = module.params["become"] sudo_prefix = "sudo " if become else "" - command = ( - f"{sudo_prefix}/opt/nessus_agent/sbin/nessuscli agent link " - f'--name="{name}" --key={linking_key} --groups="{groups}" ' - f'--network="{network}" --host="{host}" --port={port}' + command = "{} /opt/nessus_agent/sbin/nessuscli agent link --name='{}' --key={} --groups='{}' --network='{}' --host='{}' --port={}".format( + sudo_prefix, name, linking_key, groups, network, host, port ) - - try: - result = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT, timeout=10) - output = result.decode("utf-8") - return True, output, 0, True - except subprocess.TimeoutExpired: - module.fail_json(msg="Error: Command timed out. Tenable might not be installed.", rc=124) - except subprocess.CalledProcessError as e: - error_message = e.output.decode() - module.fail_json(msg=f"Failed to link Nessus Agent: {error_message}", rc=e.returncode) + rc, stdout, stderr = module.run_command(command, use_unsafe_shell=True) + if rc != 0: + module.fail_json(changed=False, stdout=to_native(stdout), stderr=to_native(stderr), rc=rc) + return True, stdout, stderr, rc def main(): @@ -145,9 +136,9 @@ def main(): supports_check_mode=False, ) - is_linked, output, rc, changed = link_nessus_agent(module) + is_linked, stdout, stderr, rc = link_nessus_agent(module) if is_linked: - module.exit_json(changed=changed, msg=output, rc=rc) + module.exit_json(changed=True, stdout=stdout, stderr=stderr, rc=rc) if __name__ == "__main__": diff --git a/plugins/modules/unlink_agent_linux.py b/plugins/modules/unlink_agent_linux.py index 9707ca9..705f501 100644 --- a/plugins/modules/unlink_agent_linux.py +++ b/plugins/modules/unlink_agent_linux.py @@ -35,23 +35,18 @@ """ -import subprocess - from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils.common.text.converters import to_native def unlink_nessus_agent(module): command = "/opt/nessus_agent/sbin/nessuscli agent unlink" - - try: - subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT) - return True, "Nessus Agent unlinked successfully" - except subprocess.CalledProcessError as e: - output = e.output.decode() - if "No host information found" in output: - return True, "No Agent is running on the server, no need to unlink" - else: - module.fail_json(msg=f"Failed to unlink Nessus Agent: {output}") + rc, stdout, stderr = module.run_command(command, use_unsafe_shell=True) + stdout = to_native(stdout) + stderr = to_native(stderr) + if rc != 0: + module.fail_json(changed=False, stdout=stdout, stderr=stderr, rc=rc) + return True, stdout, stderr, rc def main(): @@ -60,9 +55,9 @@ def main(): supports_check_mode=False, ) - is_unlinked, message = unlink_nessus_agent(module) + is_unlinked, stdout, stderr, rc = unlink_nessus_agent(module) if is_unlinked: - module.exit_json(changed=True, msg=message) + module.exit_json(changed=True, stdout=stdout, stderr=stderr, rc=rc) if __name__ == "__main__": diff --git a/pyproject.toml b/pyproject.toml index 221ab71..0126c85 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -30,11 +30,6 @@ src_paths = [ sections = ["FUTURE", "STDLIB", "THIRDPARTY", "FIRSTPARTY", "ANSIBLE_CORE", "LOCALFOLDER"] known_third_party = ["botocore", "boto3"] known_ansible_core = ["ansible"] -known_ansible_amazon_aws = ["ansible_collections.amazon.aws"] -known_ansible_community_aws = ["ansible_collections.community.aws"] [tool.flynt] transform-joins = true -exclude = [ - "ec2_metadata_facts", -]