|
1 | 1 | #!/usr/bin/python
|
2 | 2 | # -*- coding: utf-8 -*-
|
3 | 3 |
|
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'} |
27 | 26 |
|
28 | 27 |
|
29 | 28 | DOCUMENTATION = '''
|
|
44 | 43 | - CUPS 1.7+
|
45 | 44 | options:
|
46 | 45 | name:
|
47 |
| - description: |
48 |
| - - Name of the printer in CUPS. |
49 | 46 | required: false
|
50 | 47 | default: null
|
51 |
| - purge: |
52 | 48 | description:
|
53 |
| - - Task to purge all printers in CUPS. Convenient before deploy. |
| 49 | + - Name of the printer in CUPS. |
| 50 | + purge: |
54 | 51 | required: false
|
55 | 52 | default: false
|
56 | 53 | choices: ["true", "false"]
|
57 |
| - state: |
58 | 54 | description:
|
59 |
| - - Whether the printer should or not be in CUPS. |
| 55 | + - Task to purge all printers in CUPS. Convenient before deploy. |
| 56 | + state: |
60 | 57 | required: false
|
61 | 58 | default: present
|
62 | 59 | choices: ["present", "absent"]
|
63 |
| - printer_or_class: |
64 | 60 | 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: |
66 | 63 | required: false
|
67 | 64 | default: printer
|
68 | 65 | choices: ["printer", "class"]
|
69 |
| - driver: |
70 | 66 | 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: |
72 | 69 | required: false
|
73 | 70 | default: model
|
74 | 71 | choices: ["model", "ppd"]
|
75 |
| - uri: |
76 | 72 | 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: |
78 | 75 | required: false
|
79 | 76 | default: null
|
80 |
| - enabled: |
81 | 77 | 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: |
83 | 80 | required: false
|
84 | 81 | default: true
|
85 | 82 | choices: ["true", "false"]
|
86 |
| - shared: |
87 | 83 | 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: |
89 | 86 | required: false
|
90 | 87 | default: false
|
91 | 88 | choices: ["true", "false"]
|
92 |
| - model: |
93 | 89 | 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: |
95 | 92 | required: false
|
96 | 93 | default: null
|
97 |
| - default: |
98 | 94 | 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: |
100 | 97 | required: false
|
101 | 98 | default: false
|
102 | 99 | choices: ["true", "false"]
|
| 100 | + description: |
| 101 | + - Set default server printer. Only one printer can be default. |
103 | 102 | info:
|
| 103 | + required: false |
| 104 | + default: null |
104 | 105 | description:
|
105 | 106 | - The textual description of the printer.
|
| 107 | + location: |
106 | 108 | required: false
|
107 | 109 | default: null
|
108 |
| - location: |
109 | 110 | description:
|
110 | 111 | - The textual location of the printer.
|
| 112 | + assign_cups_policy: |
111 | 113 | required: false
|
112 | 114 | default: null
|
113 |
| - assign_cups_policy: |
114 | 115 | description:
|
115 | 116 | - Assign a policy defined in /etc/cups/cupsd.conf to this printer.
|
116 |
| - required: false |
117 |
| - default: null |
118 | 117 | class_members:
|
119 |
| - description: |
120 |
| - - A list of printers to be added to this class. |
121 | 118 | required: false
|
122 | 119 | default: []
|
123 | 120 | type: list
|
124 |
| - report_ipp_supply_levels: |
125 | 121 | 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: |
127 | 124 | required: false
|
128 | 125 | default: true
|
129 | 126 | choices: ["true", "false"]
|
130 |
| - report_snmp_supply_levels: |
131 | 127 | 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: |
133 | 130 | required: false
|
134 | 131 | default: true
|
135 | 132 | choices: ["true", "false"]
|
| 133 | + description: |
| 134 | + - Whether or not the printer must report supply status via SNMP (RFC 3805). |
136 | 135 | job_kb_limit:
|
| 136 | + required: false |
| 137 | + default: null |
137 | 138 | description:
|
138 | 139 | - Limit jobs to this printer (in KB)
|
| 140 | + job_quota_limit: |
139 | 141 | required: false
|
140 | 142 | default: null
|
141 |
| - job_quota_limit: |
142 | 143 | description:
|
143 | 144 | - Sets the accounting period for per-user quotas. The value is an integer number of seconds.
|
| 145 | + job_page_limit: |
144 | 146 | required: false
|
145 | 147 | default: null
|
146 |
| - job_page_limit: |
147 | 148 | description:
|
148 | 149 | - Sets the page limit for per-user quotas. The value is the integer number of pages that can be printed.
|
149 | 150 | - Double sided pages are counted as 2.
|
150 |
| - required: false |
151 |
| - default: null |
152 | 151 | options:
|
153 |
| - description: |
154 |
| - - A dictionary of key-value pairs describing printer options and their required value. |
155 | 152 | default: {}
|
156 | 153 | required: false
|
| 154 | + description: |
| 155 | + - A dictionary of key-value pairs describing printer options and their required value. |
157 | 156 | '''
|
158 | 157 |
|
159 |
| -# =========================================== |
160 |
| - |
161 | 158 |
|
162 |
| -EXAMPLES = ''' |
| 159 | +EXAMPLES = r''' |
163 | 160 | # Creates HP MFP via ethernet, set default A4 paper size and make this printer
|
164 |
| - as server default. |
| 161 | +# as server default. |
165 | 162 | - 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" |
177 | 174 | options:
|
178 |
| - media: 'iso_a4_210x297mm' |
| 175 | + media: "iso_a4_210x297mm" |
179 | 176 |
|
180 | 177 | # 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). |
183 | 180 | - 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" |
188 | 185 | options:
|
189 |
| - media: 'iso_a4_210x297mm' |
| 186 | + media: "iso_a4_210x297mm" |
190 | 187 |
|
191 | 188 | # Create CUPS Class.
|
192 | 189 | - cups_lpadmin:
|
193 |
| - name: 'StudentClass' |
194 |
| - state: 'present' |
195 |
| - printer_or_class: 'class' |
| 190 | + name: "StudentClass" |
| 191 | + state: "present" |
| 192 | + printer_or_class: "class" |
196 | 193 | class_members:
|
197 | 194 | - CampusPrinter1
|
198 | 195 | - CampusPrinter2
|
199 |
| - info: 'Printers for students' |
200 |
| - location: 'Room 404' |
| 196 | + info: "Printers for students" |
| 197 | + location: "Room 404" |
201 | 198 |
|
202 | 199 | # Deletes the printers/classes.
|
203 | 200 | - 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" |
207 | 204 | - cups_lpadmin:
|
208 |
| - name: 'StudentClass' |
209 |
| - state: 'absent' |
210 |
| - printer_or_class: 'class' |
| 205 | + name: "StudentClass" |
| 206 | + state: "absent" |
| 207 | + printer_or_class: "class" |
211 | 208 |
|
212 | 209 | # Purge all printers/classes. Useful when does not matter what we have now,
|
213 | 210 | client always receive new configuration.
|
214 |
| -- cups_lpadmin: purge='true' |
215 |
| -''' |
216 | 211 |
|
217 |
| -# =========================================== |
| 212 | +- cups_lpadmin: purge='true' |
218 | 213 |
|
| 214 | +''' |
219 | 215 |
|
220 | 216 | RETURN = '''
|
221 | 217 | purge:
|
|
267 | 263 | description: A concatenated string of all the commands run.
|
268 | 264 | returned: always
|
269 | 265 | 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" |
271 | 273 | '''
|
272 | 274 |
|
273 | 275 |
|
274 |
| -# =========================================== |
275 |
| - |
276 |
| - |
277 | 276 | class CUPSCommand(object):
|
278 | 277 | """
|
279 | 278 | This is the main class that directly deals with the lpadmin command.
|
@@ -379,7 +378,10 @@ def check_settings(self):
|
379 | 378 | """
|
380 | 379 | msgs = []
|
381 | 380 |
|
382 |
| - if self.state == 'printer': |
| 381 | + if self.purge: |
| 382 | + return |
| 383 | + |
| 384 | + if self.state == 'present': |
383 | 385 | if not self.printer_or_class:
|
384 | 386 | msgs.append("When state=present printer or class must be defined.")
|
385 | 387 |
|
@@ -680,7 +682,7 @@ def _printer_install_options(self):
|
680 | 682 | """
|
681 | 683 | cmd = ['lpadmin', '-p', self.name]
|
682 | 684 |
|
683 |
| - for k, v in self.options.iteritems(): |
| 685 | + for k, v in self.options.items(): |
684 | 686 | cmd.extend(['-o', '{0}={1}'.format(k, v)])
|
685 | 687 |
|
686 | 688 | if self.default:
|
@@ -1133,9 +1135,6 @@ def start_process(self):
|
1133 | 1135 | return result
|
1134 | 1136 |
|
1135 | 1137 |
|
1136 |
| -# =========================================== |
1137 |
| - |
1138 |
| - |
1139 | 1138 | def main():
|
1140 | 1139 | """
|
1141 | 1140 | main function that populates this Ansible module with variables and sets it in motion.
|
|
0 commit comments