Centos7 使用 Ansible 批量安装中文字体

需求背景

Centos7 下 Java 生成图片水印时中文乱码,原因是没有安装中文字体。

安装中文字体

以下是基于 Centos7 手动安装中文字体的详细步骤。当测试或者生产环境服务器比较多的时候,建议使用自动化运维工具。

# 安装字体库
$ yum -y install fontconfig

# 查看是否有中文字体
$ fc-list :lang=zh

# 创建中文字体目录
$ mkdir /usr/share/fonts/chinese

# 在 windows 的 C:\Windows\Fonts 目录下找到相应的字体 copy 到 chinese 目录下,这里以 宋体 为例
$ scp simsun.ttc simsunb.ttf root@xxxxx:/usr/share/fonts/chinese

# 查看是否有中文字体
$ fc-list :lang=zh
/usr/share/fonts/chinese/simsun.ttc: SimSun,宋体:style=Regular,常规
/usr/share/fonts/chinese/simsun.ttc: NSimSun,新宋体:style=Regular,常规

Ansible 批量安装

通常测试或者生产环境服务器比较多,下面记录如何使用 Ansbile 来批量安装中文字体。

# ansbile playbook 执行
$ ansible-playbook fonts.yml

# 验证所有服务器是否生效
$ ansible all -m shell -a "fc-list :lang=zh"
sever01 | SUCCESS | rc=0 >>
/usr/share/fonts/chinese/simsun.ttc: SimSun,宋体:style=Regular,常规
/usr/share/fonts/chinese/simsun.ttc: NSimSun,新宋体:style=Regular,常规
sever02 | SUCCESS | rc=0 >>
/usr/share/fonts/chinese/simsun.ttc: SimSun,宋体:style=Regular,常规
/usr/share/fonts/chinese/simsun.ttc: NSimSun,新宋体:style=Regular,常规
......

fonts.yml 内容:

---
- name: Install Chinese Fonts.
  hosts: all 
  remote_user: root
  become: yes
  become_method: sudo
  become_user: root
  roles:
    - fonts

ansible playbook 目录结构(删除了无用目录):

$ tree roles/fonts
roles/fonts
├── files
│   ├── simsun.ttc
│   └── simsunb.ttf
└── tasks
    └── main.yml

2 directories, 3 files

task/main.yml 内容:

---
# tasks file for fonts

- name: install fontconfig.
  yum:
    name: "{{ item }}"
    state: installed
  with_items:
    - fontconfig
  ignore_errors: true

- name: mkdir /usr/share/fonts/chinese.
  file:
    path: /usr/share/fonts/chinese
    state: directory
    mode: 0755

- name: Copy fonts to agent.
  copy:
    src: "{{ item }}"
    dest: /usr/share/fonts/chinese
  with_items:
    - simsun.ttc
    - simsunb.ttf

微信公众号:daodaotest

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叨叨软件测试

不敢奢望赞赏,只求点赞关注~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值