ansible-hacklab-desktop/roles/hacklab_printers/library/cups_printer
2018-06-02 23:03:27 +01:00

153 lines
6.2 KiB
Python

#!/usr/bin/python
# -*- coding: utf-8 -*-
import hashlib
import cups
from ansible.module_utils.basic import AnsibleModule
def get_printer_state(conn, name):
printers = conn.getPrinters()
if name in printers:
state = {
'info': printers[name]['printer-info'],
'location': printers[name]['printer-location'],
'device': printers[name]['device-uri'],
'shared': printers[name]['printer-is-shared'],
}
if printers[name]['printer-state'] == 5:
state['enabled'] = False
else:
state['enabled'] = True
attributes = conn.getPrinterAttributes(name,
requested_attributes=['printer-is-accepting-jobs'])
state['accept'] = attributes['printer-is-accepting-jobs']
return state
else:
return None
def main():
module = AnsibleModule(
argument_spec=dict(
name=dict(required=True, type='str'),
info=dict(required=False, type='str'),
location=dict(required=False, type='str'),
device=dict(required=False, type='str'),
ppd=dict(required=False, type='str'),
accept=dict(required=False, type='bool'),
enabled=dict(required=False, type='bool'),
shared=dict(required=False, type='bool'),
default=dict(default=False, type='bool'),
state=dict(default='present', choices=['absent', 'present']),
options=dict(required=False, type='dict'),
),
supports_check_mode=True
)
conn = cups.Connection()
current_state = get_printer_state(conn, module.params['name'])
# handle state=absent
if module.params['state'] == 'absent':
if current_state is None:
module.exit_json(changed=False)
else:
if not module.check_mode:
conn.deletePrinter(module.params['name'])
module.exit_json(changed=True, msg='removed')
messages = []
changed = False
# handle state=present, new printer
if current_state is None:
kwargs = {}
if module.params['info']:
kwargs['info'] = module.params['info']
if module.params['location']:
kwargs['location'] = module.params['location']
if module.params['device']:
kwargs['device'] = module.params['device']
if module.params['shared']:
kwargs['shared'] = module.params['shared']
if module.params['ppd']:
kwargs['filename'] = module.params['ppd']
if not module.check_mode:
conn.addPrinter(module.params['name'], **kwargs)
conn.enablePrinter(module.params['name'])
conn.acceptJobs(module.params['name'])
messages.append('added')
changed = True
current_state = get_printer_state(conn, module.params['name'])
# handle state=present, existing printer
if module.params['info'] is not None and module.params['info'] != current_state['info']:
if not module.check_mode:
conn.setPrinterInfo(module.params['name'], module.params['info'])
messages.append('set info')
changed = True
if module.params['location'] is not None and module.params['location'] != current_state['location']:
if not module.check_mode:
conn.setPrinterLocation(module.params['name'], module.params['location'])
messages.append('set location')
changed = True
if module.params['device'] is not None and module.params['device'] != current_state['device']:
if not module.check_mode:
conn.setPrinterDevice(module.params['name'], module.params['device'])
messages.append('set device')
changed = True
if module.params['shared'] is not None and module.params['shared'] != current_state['shared']:
if not module.check_mode:
conn.setPrinterShared(module.params['name'], module.params['shared'])
messages.append('set shared {}'.format(module.params['shared']))
changed = True
if module.params['accept'] is not None and module.params['accept'] != current_state['accept']:
if not module.check_mode:
if module.params['accept'] is True:
conn.acceptJobs(module.params['name'])
messages.append('accept jobs')
else:
conn.rejectJobs(module.params['name'])
messages.append('reject jobs')
changed = True
if module.params['enabled'] is not None and module.params['enabled'] != current_state['enabled']:
if not module.check_mode:
if module.params['enabled'] is True:
conn.enablePrinter(module.params['name'])
messages.append('enabled')
else:
conn.disablePrinter(module.params['name'])
messages.append('disabled')
changed = True
if module.params['ppd'] is not None:
filename = conn.getPPD(module.params['name'])
current_sig = hashlib.sha1(open(filename, 'r').read()).hexdigest()
new_sig = hashlib.sha1(open(module.params['ppd'], 'r').read()).hexdigest()
if new_sig != current_sig:
if not module.check_mode:
conn.addPrinter(module.params['name'], filename=module.params['ppd'])
messages.append('updated ppd')
changed = True
#os.unlink(filename)
if isinstance(module.params['options'], dict):
current_options = conn.getPrinterAttributes(module.params['name'])
for k, v in module.params['options'].items():
if '{}-default'.format(k) in current_options:
if current_options['{}-default'.format(k)] != v:
if not module.check_mode:
conn.addPrinterOptionDefault(module.params['name'], k, v)
messages.append('set option {}'.format(k))
changed = True
if module.params['default'] is True:
if conn.getDefault() != module.params['name']:
if not module.check_mode:
conn.setDefault(module.params['name'])
messages.append('set default')
changed = True
module.exit_json(changed=changed, msg=', '.join(messages))
if __name__ == '__main__':
main()