Skip to content

Commit b298cf9

Browse files
committed
1 parent 52a655c commit b298cf9

File tree

1 file changed

+104
-105
lines changed

1 file changed

+104
-105
lines changed

library/cups_lpadmin.py

Lines changed: 104 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
11
#!/usr/bin/python
22
# -*- coding: utf-8 -*-
33

4-
"""
5-
(c) 2015, David Symons (Multimac) <[email protected]>
6-
(c) 2016, Konstantin Shalygin <[email protected]>
7-
(c) 2016, Hitesh Prabhakar <HP41@GitHub>
8-
9-
This file is part of Ansible
10-
11-
This module is free software: you can redistribute it and/or modify
12-
it under the terms of the GNU General Public License as published by
13-
the Free Software Foundation, either version 3 of the License, or
14-
(at your option) any later version.
15-
16-
This software is distributed in the hope that it will be useful,
17-
but WITHOUT ANY WARRANTY; without even the implied warranty of
18-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19-
GNU General Public License for more details.
20-
21-
You should have received a copy of the GNU General Public License
22-
along with this software. If not, see <http://www.gnu.org/licenses/>.
23-
"""
24-
25-
26-
# ===========================================
4+
# (c) 2015, David Symons (Multimac) <[email protected]>
5+
# (c) 2016, Hitesh Prabhakar <HP41@GitHubm>
6+
# (c) 2016-2017, Konstantin Shalygin <[email protected]>
7+
#
8+
# This file is part of Ansible
9+
#
10+
# Ansible is free software: you can redistribute it and/or modify
11+
# it under the terms of the GNU General Public License as published by
12+
# the Free Software Foundation, either version 3 of the License, or
13+
# (at your option) any later version.
14+
#
15+
# Ansible is distributed in the hope that it will be useful,
16+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
# GNU General Public License for more details.
19+
#
20+
# You should have received a copy of the GNU General Public License
21+
# along with Ansible. If not, see <http://www.gnu.org/licenses/>
22+
23+
ANSIBLE_METADATA = {'status': ['stableinterface'],
24+
'supported_by': 'community',
25+
'version': '1.0'}
2726

2827

2928
DOCUMENTATION = '''
@@ -44,178 +43,175 @@
4443
- CUPS 1.7+
4544
options:
4645
name:
47-
description:
48-
- Name of the printer in CUPS.
4946
required: false
5047
default: null
51-
purge:
5248
description:
53-
- Task to purge all printers in CUPS. Convenient before deploy.
49+
- Name of the printer in CUPS.
50+
purge:
5451
required: false
5552
default: false
5653
choices: ["true", "false"]
57-
state:
5854
description:
59-
- Whether the printer should or not be in CUPS.
55+
- Task to purge all printers in CUPS. Convenient before deploy.
56+
state:
6057
required: false
6158
default: present
6259
choices: ["present", "absent"]
63-
printer_or_class:
6460
description:
65-
- State whether the object/item we are working on is a printer or class.
61+
- Whether the printer should or not be in CUPS.
62+
printer_or_class:
6663
required: false
6764
default: printer
6865
choices: ["printer", "class"]
69-
driver:
7066
description:
71-
- System V interface or PPD file.
67+
- State whether the object/item we are working on is a printer or class.
68+
driver:
7269
required: false
7370
default: model
7471
choices: ["model", "ppd"]
75-
uri:
7672
description:
77-
- The URI to use when connecting to the printer. This is only required in the present state.
73+
- System V interface or PPD file.
74+
uri:
7875
required: false
7976
default: null
80-
enabled:
8177
description:
82-
- Whether or not the printer should be enabled and accepting jobs.
78+
- The URI to use when connecting to the printer. This is only required in the present state.
79+
enabled:
8380
required: false
8481
default: true
8582
choices: ["true", "false"]
86-
shared:
8783
description:
88-
- Whether or not the printer should be shared on the network.
84+
- Whether or not the printer should be enabled and accepting jobs.
85+
shared:
8986
required: false
9087
default: false
9188
choices: ["true", "false"]
92-
model:
9389
description:
94-
- The System V interface or PPD file to be used for the printer.
90+
- Whether or not the printer should be shared on the network.
91+
model:
9592
required: false
9693
default: null
97-
default:
9894
description:
99-
- Set default server printer. Only one printer can be default.
95+
- The System V interface or PPD file to be used for the printer.
96+
default:
10097
required: false
10198
default: false
10299
choices: ["true", "false"]
100+
description:
101+
- Set default server printer. Only one printer can be default.
103102
info:
103+
required: false
104+
default: null
104105
description:
105106
- The textual description of the printer.
107+
location:
106108
required: false
107109
default: null
108-
location:
109110
description:
110111
- The textual location of the printer.
112+
assign_cups_policy:
111113
required: false
112114
default: null
113-
assign_cups_policy:
114115
description:
115116
- Assign a policy defined in /etc/cups/cupsd.conf to this printer.
116-
required: false
117-
default: null
118117
class_members:
119-
description:
120-
- A list of printers to be added to this class.
121118
required: false
122119
default: []
123120
type: list
124-
report_ipp_supply_levels:
125121
description:
126-
- Whether or not the printer must report supply status via IPP.
122+
- A list of printers to be added to this class.
123+
report_ipp_supply_levels:
127124
required: false
128125
default: true
129126
choices: ["true", "false"]
130-
report_snmp_supply_levels:
131127
description:
132-
- Whether or not the printer must report supply status via SNMP (RFC 3805).
128+
- Whether or not the printer must report supply status via IPP.
129+
report_snmp_supply_levels:
133130
required: false
134131
default: true
135132
choices: ["true", "false"]
133+
description:
134+
- Whether or not the printer must report supply status via SNMP (RFC 3805).
136135
job_kb_limit:
136+
required: false
137+
default: null
137138
description:
138139
- Limit jobs to this printer (in KB)
140+
job_quota_limit:
139141
required: false
140142
default: null
141-
job_quota_limit:
142143
description:
143144
- Sets the accounting period for per-user quotas. The value is an integer number of seconds.
145+
job_page_limit:
144146
required: false
145147
default: null
146-
job_page_limit:
147148
description:
148149
- Sets the page limit for per-user quotas. The value is the integer number of pages that can be printed.
149150
- Double sided pages are counted as 2.
150-
required: false
151-
default: null
152151
options:
153-
description:
154-
- A dictionary of key-value pairs describing printer options and their required value.
155152
default: {}
156153
required: false
154+
description:
155+
- A dictionary of key-value pairs describing printer options and their required value.
157156
'''
158157

159-
# ===========================================
160-
161158

162-
EXAMPLES = '''
159+
EXAMPLES = r'''
163160
# Creates HP MFP via ethernet, set default A4 paper size and make this printer
164-
as server default.
161+
# as server default.
165162
- cups_lpadmin:
166-
name: 'HP_M1536'
167-
state: 'present'
168-
printer_or_class: 'printer'
169-
uri: 'hp:/net/HP_LaserJet_M1536dnf_MFP?ip=192.168.1.2'
170-
model: 'drv:///hp/hpcups.drv/hp-laserjet_m1539dnf_mfp-pcl3.ppd'
171-
default: 'true'
172-
location: 'Room 404'
173-
info: 'MFP, but duplex broken, as usual on this model'
174-
printer_assign_policy: 'students'
175-
report_ipp_supply_levels: 'true'
176-
report_snmp_supply_levels: 'false'
163+
name: "HP_M1536"
164+
state: "present"
165+
printer_or_class: "printer"
166+
uri: "hp:/net/HP_LaserJet_M1536dnf_MFP?ip=192.168.1.2"
167+
model: "drv:///hp/hpcups.drv/hp-laserjet_m1539dnf_mfp-pcl3.ppd"
168+
default: "true"
169+
location: "Room 404"
170+
info: "MFP, but duplex broken, as usual on this model"
171+
printer_assign_policy: "students"
172+
report_ipp_supply_levels: "true"
173+
report_snmp_supply_levels: "false"
177174
options:
178-
media: 'iso_a4_210x297mm'
175+
media: "iso_a4_210x297mm"
179176
180177
# Creates HP Printer via IPP (shared USB printer in another CUPS instance).
181-
Very important include 'snmp=false' to prevent adopt 'parent' driver,
182-
because if 'parent' receive not raw job this job have fail (filter failed).
178+
# Very important disable snmp to prevent adopt 'parent' driver, because if
179+
# 'parent' receive not raw job this job have fail (filter failed).
183180
- cups_lpadmin:
184-
name: 'HP_P2055'
185-
state: 'present'
186-
uri: 'ipp://192.168.2.127:631/printers/HP_P2055?snmp=false'
187-
model: 'raw'
181+
name: "HP_P2055"
182+
state: "present"
183+
uri: "ipp://192.168.2.127:631/printers/HP_P2055?snmp=false"
184+
model: "raw"
188185
options:
189-
media: 'iso_a4_210x297mm'
186+
media: "iso_a4_210x297mm"
190187
191188
# Create CUPS Class.
192189
- cups_lpadmin:
193-
name: 'StudentClass'
194-
state: 'present'
195-
printer_or_class: 'class'
190+
name: "StudentClass"
191+
state: "present"
192+
printer_or_class: "class"
196193
class_members:
197194
- CampusPrinter1
198195
- CampusPrinter2
199-
info: 'Printers for students'
200-
location: 'Room 404'
196+
info: "Printers for students"
197+
location: "Room 404"
201198
202199
# Deletes the printers/classes.
203200
- cups_lpadmin:
204-
name: 'HP_P2055'
205-
state: 'absent'
206-
printer_or_class: 'printer'
201+
name: "HP_P2055"
202+
state: "absent"
203+
printer_or_class: "printer"
207204
- cups_lpadmin:
208-
name: 'StudentClass'
209-
state: 'absent'
210-
printer_or_class: 'class'
205+
name: "StudentClass"
206+
state: "absent"
207+
printer_or_class: "class"
211208
212209
# Purge all printers/classes. Useful when does not matter what we have now,
213210
client always receive new configuration.
214-
- cups_lpadmin: purge='true'
215-
'''
216211
217-
# ===========================================
212+
- cups_lpadmin: purge='true'
218213
214+
'''
219215

220216
RETURN = '''
221217
purge:
@@ -267,13 +263,16 @@
267263
description: A concatenated string of all the commands run.
268264
returned: always
269265
type: string
270-
sample: "\nlpstat -p TEST \nlpinfo -l -m \nlpoptions -p TEST \nlpstat -p TEST \nlpstat -p TEST \nlpadmin -p TEST -o cupsIPPSupplies=true -o cupsSNMPSupplies=true \nlpoptions -p TEST -l "
266+
sample: "lpstat -p TEST
267+
lpinfo -l -m
268+
lpoptions -p TEST
269+
lpstat -p TEST
270+
lpstat -p TEST
271+
lpadmin -p TEST -o cupsIPPSupplies=true -o cupsSNMPSupplies=true
272+
lpoptions -p TEST -l"
271273
'''
272274

273275

274-
# ===========================================
275-
276-
277276
class CUPSCommand(object):
278277
"""
279278
This is the main class that directly deals with the lpadmin command.
@@ -379,7 +378,10 @@ def check_settings(self):
379378
"""
380379
msgs = []
381380

382-
if self.state == 'printer':
381+
if self.purge:
382+
return
383+
384+
if self.state == 'present':
383385
if not self.printer_or_class:
384386
msgs.append("When state=present printer or class must be defined.")
385387

@@ -680,7 +682,7 @@ def _printer_install_options(self):
680682
"""
681683
cmd = ['lpadmin', '-p', self.name]
682684

683-
for k, v in self.options.iteritems():
685+
for k, v in self.options.items():
684686
cmd.extend(['-o', '{0}={1}'.format(k, v)])
685687

686688
if self.default:
@@ -1133,9 +1135,6 @@ def start_process(self):
11331135
return result
11341136

11351137

1136-
# ===========================================
1137-
1138-
11391138
def main():
11401139
"""
11411140
main function that populates this Ansible module with variables and sets it in motion.

0 commit comments

Comments
 (0)