当前位置:   article > 正文

RH358管理打印机和打印文件--自动化配置打印机_rh358 测试打印

rh358 测试打印

RH358管理打印机和打印文件–自动化配置打印机

本章节介绍如何使用Ansible部署和管理打印机。虽然有点积累,但可理解为对Ansible的加深使用。

RH358专栏地址:https://blog.csdn.net/qq_41765918/category_11532281.html

1. 使用Ansible部署CUPS

此前还没有特定于cups的Ansible模块。使用Ansible在服务器上部署CUPS遵循一个标准流程。

# 安装
#安装yum Ansible模块的cups和avahi包。
- name: Install packages for printer support
  yum:
  name:
    - avahi
    - cups
  state: present

# 启用并启动CUPS服务
#使用Ansible服务模块启用并启动cups服务。不需要启动avahi-daemon服务,因为它是套接字激活的,并自动启动。
- name: Enable and start services for printer support
  systemd:
    name: cups
    state: started
    enabled: yes

# 配置防火墙规则
#使用firewalld模块打开mdns服务的防火墙端口。
- name: Open the mDNS firewall port
  firewalld:
    service: mdns
    state: enabled
    permanent: yes
    immediate: yes
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

2. 添加和删除打印机

虽然目前Ansible还不包括管理CUPS的模块,但您可以使用命令Ansible模块调用的shell命令来创建和管理CUPS。

# 创建打印队列
#下面的示例剧本创建了两个打印机。为了保持剧本的简单,它不会自动发现可用的打印机。您必须定义一个包含打印机队列名称和相关uri列表的变量。
- name: Create or update print queues
  hosts: servers
  vars:
    printers:
      - name: "bwprinter"
        uri: "ipp://192.0.2.12:631/printers/bwprinter"
      - name: "color"
        uri: "ipp://192.0.2.77:631/ipp/port1"
  tasks:
    - name: Configure all printers
      command:
        cmd: lpadmin -p {{ item['name'] }} -v {{ item['uri'] }} -m everywhere -E
      loop: "{{ printers }}"
配置任务在每次运行时都会报告“更改”,即使打印机已经存在并且已正确配置。如果您希望配置任务真正是幂等的,它应该在运行配置所有打印机任务之前确定打印机队列是否存在并正确配置。

# 设置默认目标
#在下面的例子中,第一个任务集更改为false,因为它不更改托管主机;它运行一个命令来收集信息。第二个任务使用regex_replace筛选器从lpstat -d的输出中删除额外的文本,以便条件可以比较打印机名称
- name: Set the default printer
  hosts: servers
  vars:
    default_dest: "bwprinter"
  tasks:
    - name: Determine the current default destination
      command: lpstat -d
      register: curr_dest
      changed_when: false

    - name: Make sure default_dest is the default destination
      command: lpadmin -d {{ default_dest }}
      when: curr_dest['stdout'] | regex_replace('^(.*):.') != default_dest
#剧本确定默认打印机是什么,并使用该信息有条件地设置默认打印机。这将阻止它在默认打印机已经正确设置时进行更改。

# 删除打印队列
#下面的播放检查一个名为bwprinter的打印队列是否存在,如果存在,则删除它。第一个任务将ignore_errors设置为true,这样即使队列不存在,play也会继续执行。如果需要剧本失败,那就删除这一行。
- name: Delete obsolete print queues
  hosts: servers
  gather_facts: no
  vars:
    queue_name: "bwprinter"
  tasks:
    - name: Check if print queue exists
      command: lpstat -p "{{ queue_name }}"
      register: result
      ignore_errors: true
      changed_when: false
      
    - name: Delete the print queue
      command: lpadmin -x "{{ queue_name }}"
      when: result.rc == 0
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51

3. 使用Ansible管理打印队列

以下任务配置一个由queue_name变量(在本例中为bwprinter)指定的打印队列,以便在存在打印作业时接受它。如果队列不存在,任务和它的play将会存在失败。

- name: Make sure the print queue accepts jobs
  command: cupsaccept "{{ queue_name }}"
  • 1
  • 2

注意:此任务每次都会报告更改后的结果,即使队列已经在接受作业。您可以添加任务来避免这种结果。例如,您可以解析lpstat -p的输出,并有条件地运行cupsaccept任务。

可以为cupsreject、cupsenable或cupsdisable编写类似的任务。

4. 课本练习

[student@workstation ~]$ lab printing-automation start

该命令确保在serverc上配置了IPP Everywhere打印机。发送到此打印机的打印作业可以通过web浏览器查看:http://serverd /ippserver/ippp-everywhere-pdf。该命令还在/home/student/print-auto/中创建一个Ansible项目。

在本练习中,将在使用IPP Everywhere网络打印机的servera上创建一个CUPS打印队列。

1. 熟悉Ansible项目及其现状。
[student@workstation ~]$ cd ~/printing-auto
[student@workstation printing-auto]$ tree
.
├── ansible.cfg
├── inventory
├── printer-accept.yml
├── printer-create.yml
├── printer-destroy.yml
└── printer-reject.yml

0 directories, 6 files
[student@workstation printing-auto]$ cat inventory
serverb.lab.example.com
[clients]
servera.lab.example.com
[student@workstation printing-auto]$ cat ansible.cfg 
[defaults]
inventory=./inventory
remote_user=devops
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
2. 检查并完成printer-create.yml Ansible剧本。

这个剧本在servera上创建一个名为my-printer的打印队列。它提供打印机支持所需的服务并打开防火墙端口。

[student@workstation printing-auto]$ cat printer-create.yml 
---
- name: Install CUPS and create a print queue
  hosts: clients
  gather_facts: no
  become: yes
  vars:
    print_packages:
    - cups
    - avahi
    print_services:
    - avahi-daemon
    - cups
    print_ports:
    - mdns
    queue_name: "my-printer"
    dev_uri: "ipp://serverc.lab.example.com:631/printers/rht-printer"
  tasks:
    - name: Install the CUPS and Avahi packages
      yum:
        name: "{{ print_packages }}"
        state: present

    - name: Enable and start the CUPS and Avahi services
      service:
        name: "{{ item }}"
        state: started
        enabled: yes
      loop: "{{ print_services }}"

    - name: Open the mDNS firewall port
      firewalld:
        service: "{{ item }}"
        state: enabled
        permanent: yes
        immediate: yes
      loop: "{{ print_ports }}"

    - name: Check if print queue already exists
      command: lpstat -p "{{ queue_name }}"
      register: cmdout
      ignore_errors: true
      changed_when: false

    - name: Create the print queue
      command: lpadmin -p "{{ queue_name }}" -v "{{ dev_uri }}" -m everywhere -E
      when: cmdout.rc != 0

    - name: Check default print queue
      command: lpstat -d
      register: curr_dest
      changed_when: false

    - name: Make the new print queue the default
      command: lpadmin -d "{{ queue_name }}"
      when: curr_dest['stdout'] | regex_replace('^(.*):.') != queue_name
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
3. 运行剧本创建打印队列。
[student@workstation printing-auto]$ ansible-playbook --syntax-check printer-create.yml

playbook: printer-create.yml
[student@workstation printing-auto]$ ansible-playbook printer-create.yml
  • 1
  • 2
  • 3
  • 4
4. 检查并完成printer-reject.yml Ansible剧本。

这个剧本调优服务器上的my-printer打印队列,以拒绝传入的打印作业。对文件进行编辑以填充缺失的信息。

[student@workstation printing-auto]$ cat printer-reject.yml 
---
- name: Configure a print queue to reject jobs
  hosts: clients
  gather_facts: no
  become: yes
  vars:
    queue_name: "my-printer"
  tasks:
    - name: Confirm the print queue exists
      command: lpstat -p "{{ queue_name }}"
      register: cmdout
      ignore_errors: true
      changed_when: false

    - name: Tune the print queue to reject jobs
      command: cupsreject "{{ queue_name }}"
      when: cmdout.rc == 0
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
5. 运行剧本配置打印队列以拒绝传入的打印作业。
[student@workstation printing-auto]$ ansible-playbook --syntax-check printer-reject.yml 

playbook: printer-reject.yml
[student@workstation printing-auto]$ ansible-playbook printer-reject.yml

[root@servera ~]# ls -al | lp
lp: Destination "my-printer" is not accepting jobs.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
6. 检查并完成printer-accept.yml Ansible剧本。

这个剧本调优服务器上的my-printer打印队列以接受传入的打印作业。

[student@workstation printing-auto]$ vim printer-accept.yml 
---
- name: Configure a print queue to accept jobs
  hosts: clients
  gather_facts: no
  become: yes
  vars:
    queue_name: "my-printer"
  tasks:
    - name: Confirm the print queue exists
      command: lpstat -p "{{ queue_name }}"
      register: cmdout
      ignore_errors: true
      changed_when: false

    - name: Tune the print queue to accept jobs
      command: cupsaccept "{{ queue_name }}"
      when: cmdout.rc == 0
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
7. 运行剧本配置托管主机上的打印队列以接受传入的打印作业。
[student@workstation printing-auto]$ ansible-playbook printer-accept.yml --syntax-check

playbook: printer-accept.yml
[student@workstation printing-auto]$ ansible-playbook printer-accept.yml

[root@servera ~]# ls -al | lp
request id is my-printer-1 (0 file(s))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
8. 检查并完成printer-destroy.yml Ansible剧本。

这个剧本删除了服务器上的my-printer打印队列。

[student@workstation printing-auto]$ vim printer-destroy.yml 
---
- name: Remove a print queue
  hosts: clients
  gather_facts: no
  become: yes
  vars:
    queue_name: "my-printer"
  tasks:
    - name: Check if print queue exists
      command: lpstat -p "{{ queue_name }}"
      register: cmdout
      ignore_errors: true
      changed_when: false

    - name: Delete the print queue
      command: lpadmin -x "{{ queue_name }}"
      when: cmdout.rc == 0
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
9. 运行剧本删除托管主机上的打印队列。
[student@workstation printing-auto]$ ansible-playbook printer-destroy.yml --syntax-check

playbook: printer-destroy.yml
[student@workstation printing-auto]$ ansible-playbook printer-destroy.yml
  • 1
  • 2
  • 3
  • 4
完成实验

[student@workstation ~]$ lab printing-automation finish

总结

  • 介绍如何使用Ansible部署CUPS。
  • 介绍如何添加和删除打印机。
  • 使用Ansible管理打印队列。
  • 若喜欢金鱼哥的文章,顺手点个赞。也可点个关注,因为后续会不断上干货。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/718951
推荐阅读
相关标签
  

闽ICP备14008679号