Skip to content

Commit

Permalink
ansible: switch printserver to foo2zjs driver
Browse files Browse the repository at this point in the history
  • Loading branch information
dezeroku committed Feb 5, 2024
1 parent 6580f37 commit c9308aa
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 124 deletions.
5 changes: 2 additions & 3 deletions ansible/printserver.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
tags:
- printserver
vars:
# The identifier below specifies HP LasterJet P1102
hplip_printer_vendor_product_id: "03f0:002a"
cups_printer_model: "P1102"
cups_printer_name: "HP_LaserJet_P1102"
roles:
- role: cups
- role: hplip
3 changes: 3 additions & 0 deletions ansible/roles/cups/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
# This is used in few greps
cups_printer_model: ""
94 changes: 90 additions & 4 deletions ansible/roles/cups/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
---
- name: Install cups
# There are some strong assumptions about foo2zjs and grep patterns in this file
# It's still better than hardcoding thing, but it's likely that specific models
# may require changes in this code
# TODO: this whole thing can be rewritten using ansible.builtin.command and python instead of grepping
- name: Install cups and foo2zjs
become: true
become_user: root
ansible.builtin.apt:
name:
- cups
- printer-driver-foo2zjs
state: present

# TODO: proper checks for the admin endpoints
- name: Allow connections from LAN
become: true
become_user: root
notify:
- Restart cups
block:
# TODO: proper permissions for the endpoints
- name: Allow / from LAN
become: true
become_user: root
ansible.builtin.lineinfile:
dest: /etc/cups/cupsd.conf
insertafter: "<Location />"
line: " Allow 192.168.1.*"
backup: true

- name: Listen on the LAN host
become: true
Expand All @@ -30,4 +34,86 @@
dest: /etc/cups/cupsd.conf
insertafter: "Listen localhost:631"
line: "Listen {{ ansible_hostname }}:631"
backup: true

- name: Check the cups_printer_model
ansible.builtin.fail:
msg: cups_printer_model variable can not be empty
when: cups_printer_model | length == 0

- name: Check if the printer is already configured
become: true
become_user: root
ansible.builtin.shell:
cmd: |
set -o pipefail
lpstat -a | grep "^{{ cups_printer_name | default(cups_printer_model) }}"
executable: /bin/bash
failed_when: false
changed_when: false
register: lpstat

- name: Set the printer_already_configured variable
changed_when: false
ansible.builtin.set_fact:
printer_already_configured: "{{ True if lpstat.rc == 0 else False }}"

- name: Set up the printer
when: not printer_already_configured
# Restart cups just in case
notify:
- Restart cups
block:
- name: Find the cups-device-uri of the printer
block:
- name: Check for the device
become: true
become_user: root
ansible.builtin.shell:
cmd: |
set -o pipefail
lpinfo -v | grep "^direct usb" | grep "{{ cups_printer_model }}" | cut -d " " -f2
executable: /bin/bash
register: cups_device_uri_cmd
changed_when: false
failed_when: false

- name: Print error message
ansible.builtin.fail:
msg: "{{ cups_printer_model }} device is not present in the system"
when: cups_device_uri_cmd.rc != 0

- name: Set the cups_device_uri variable
changed_when: false
ansible.builtin.set_fact:
cups_device_uri: "{{ cups_device_uri_cmd.stdout }}"

- name: Find the foo2zjs driver for the printer
block:
- name: Check for the driver
become: true
become_user: root
ansible.builtin.shell:
cmd: |
set -o pipefail
lpinfo -m | grep "foo2zjs" | grep "{{ cups_printer_model }}.ppd" | cut -d " " -f1
executable: /bin/bash
register: cups_device_driver_cmd
changed_when: false
failed_when: false

- name: Print error message
ansible.builtin.fail:
msg: "{{ cups_printer_model }} foo2zjs driver is not present in the system"
when: cups_device_driver_cmd.rc != 0

- name: Set the cups_device_driver variable
changed_when: false
ansible.builtin.set_fact:
cups_device_driver: "{{ cups_device_driver_cmd.stdout }}"

- name: Configure the printer
changed_when: true
become: true
become_user: root
ansible.builtin.command:
cmd: lpadmin -p "{{ cups_printer_name | default(cups_printer_model) }}" -E -v "{{ cups_device_uri }}" -m "{{ cups_device_driver }}"
4 changes: 0 additions & 4 deletions ansible/roles/hplip/defaults/main.yml

This file was deleted.

113 changes: 0 additions & 113 deletions ansible/roles/hplip/tasks/main.yml

This file was deleted.

0 comments on commit c9308aa

Please sign in to comment.