Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save simonLeary42/6d6786394f7af3fbdcedb4ae06c333dd to your computer and use it in GitHub Desktop.
Save simonLeary42/6d6786394f7af3fbdcedb4ae06c333dd to your computer and use it in GitHub Desktop.
import sys
import json
import yaml
import shutil
import subprocess
from ansible.plugins.action import ActionBase
"""
fails if bitwarden is not unlocked
"""
class ActionModule(ActionBase):
def run(self, tmp=None, task_vars=None):
result = super(ActionModule, self).run(tmp, task_vars)
def failed(msg):
return result.update({"failed": True, "msg": msg})
if not shutil.which("bw"):
return failed("error: `bw status` command not found!")
try:
bw_status_out = subprocess.check_output("bw status", text=True, stderr=sys.stderr)
except subprocess.CalledProcessError as e:
return failed("error: `bw status` failed!")
try:
status = json.loads(bw_status_out)
except json.JSONDecodeError as e:
return failed(f"error: `bw status` did not print valid json!\n{e}")
try:
status_status = status["status"]
except KeyError:
return failed(
'error: `bw status` printed valid json but the "status" property was not found!'
)
if not isinstance(status_status, str):
return failed(
'error: `bw status` printed valid json but the "status" property is not a string!'
)
if status_status != "unlocked":
return failed("bitwarden is not unlocked!")
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment