Skip to content

Instantly share code, notes, and snippets.

@dmsimard
Created January 12, 2021 19:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmsimard/36e09769dfe07c4d5066eea1c7c04847 to your computer and use it in GitHub Desktop.
Save dmsimard/36e09769dfe07c4d5066eea1c7c04847 to your computer and use it in GitHub Desktop.
Ansible browser module
#!/usr/bin/python3
# Copyright: (c) 2021, David Moreau-Simard <dmsimard@redhat.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import, division, print_function
__metaclass__ = type
DOCUMENTATION = """
---
module: browser
author:
- David Moreau-Simard (@dmsimard) <dmsimard@redhat.com>
short_description: Opens a web browser page on remote machines
description:
- Opens a web browser page on remote machines
version_added: "2.0"
options:
url:
description:
- URL to open
type: str
required: true
browser:
description:
- The browser to use if not using the default.
- See https://docs.python.org/3/library/webbrowser.html#webbrowser.register for supported browsers
type: str
target:
description:
- When using "existing", the url is opened as a tab in an existing browser window if possible.
- When using "window", the url is opened in a new browser window if possible.
- When using "tab", the url is opened as a new browser tab if possible.
type: str
default: "existing"
choices: [ "existing", "window", "tab" ]
focus:
description:
- Whether or not to raise focus on the browser window.
- Note that under many window managers this will occur regardless of the setting of this variable.
type: bool
default: true
display:
description:
- The DISPLAY environment variable to launch the browser in
type: str
default: ":0.0"
"""
EXAMPLES = """
- name: Open a web page with the default browser
community.general.browser:
url: "https://www.ansible.com"
- name: Open a web page with a specific browser
community.general.browser:
url: "https://www.ansible.com"
browser: chromium
"""
from ansible.module_utils.basic import AnsibleModule
import os
import webbrowser
def main():
module = AnsibleModule(
argument_spec=dict(
url=dict(type="str", required=True),
browser=dict(type="str"),
target=dict(type="str", choices=["existing", "window", "tab"], default="existing"),
focus=dict(type="bool", default=True),
display=dict(type="str", default=":0.0"),
)
)
url = module.params["url"]
browser = module.params["browser"]
# Mapping as per https://docs.python.org/3/library/webbrowser.html#webbrowser.open
target = {
"existing": 0,
"window": 1,
"tab": 2
}[module.params["target"]]
focus = module.params["focus"]
# Set DISPLAY in order to open the URL in the right window manager environment
os.environ["DISPLAY"] = module.params["display"]
webbrowser.open(url, new=target, autoraise=focus)
module.exit_json(changed=True, msg="Successfully opened url in browser: %s" % url)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment