#- lineinfile # 确保某一行文本在指定文本中 删除或替换 sed
#- replace # 通过正则表达式替换文件中的字符串
#- yum_repository # 查看本地yum仓库
#- mount # 挂载模块
#- unarchive # 压缩解压
#- template # 模版模块
---
- hosts: test_playbook
remote_user: root
vars:
# 生成一个静态的时间戳变量,并格式化
timestamp: "{{ ansible_date_time.iso8601 | regex_replace(':', '-') | regex_replace('T', '_') | regex_replace('Z', '') }}"
tasks:
# 1. yum_repository - 配置本地yum仓库
- name: Configure Aliyun YUM repository
yum_repository:
name: aliyun
description: Aliyun YUM Repository
baseurl: http://mirrors.aliyun.com/centos/7/os/x86_64/ # 根据你的 CentOS 版本和仓库选择合适的 URL
gpgcheck: 1
gpgkey: http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
enabled: yes
# 2. yum makecache
- name: Update YUM cache
command: yum makecache
# 3. yum - 利用yum安装软件包mariadb
- name: install mariadb-server package
yum:
name: mariadb-server
state: present
#4.创建数据库错误日志存放目录
- name: create mariadb error log directory
file:
path: /var/log/mariadb
state: directory
# 5. service - 利用service启动服务mariadb
- name: starting mariadb service
service:
name: mariadb
state: started
enabled: true
# 6. lineinfile - 确保某一行文本在指定文本中 删除或替换 sed 这里替换错误日志存放位置log_error = /var/log/mariadb/error.log
- name: Ensure log_error path is correctly set in /etc/my.cnf
lineinfile:
path: /etc/my.cnf
regexp: '^log_error=' # 匹配以 'log_error=' 开头的行
line: 'log_error=/var/log/mariadb/error.log'
state: present # 确保这行存在
# 7. replace - 使用正则表达式替换文件中的字符串 这里将所有注释符号'#'替换为'##'
- name: Replace a setting in /etc/my.cnf
replace:
path: /etc/my.cnf
regexp: '^#'
replace: '##'
# 8. service - 利用service重启服务mariadb
- name: starting mariadb service
service:
name: mariadb
state: restarted
enabled: true
# 9. command - 创建挂载目录
- name: creat mount directory
file:
path: /mnt/disk
state: directory
# 10. mount - 挂载文件系统
- name: Mount /dev/sdb1 to /mnt/disk
mount:
path: /mnt/disk
src: /dev/sdb1
fstype: xfs
state: mounted
# 11.创建压缩测试文件
- name: create a tar.gz file
command: tar -czvf /var/log/mariadb/error_{{ timestamp }}.log.gz -C /var/log/mariadb error.log
# 12. unarchive - 解压缩文件
- name: Extract a tar.gz file
unarchive:
src: /var/log/mariadb/error_{{ timestamp }}.log.gz
dest: /var/log/mariadb/
remote_src: yes
playbook练手小项目
最新推荐文章于 2024-09-10 19:16:08 发布