自动化运维-从零开始编写ansible剧本(二)

前言

前述文章自动化运维-ansible常用模块详解已经详细介绍过ansible 常用模块的Ad-hoc命令实现方式,适合执行 临时性、简单的任务;而使用较多的方式是通过ansible playbook实现多任务执行,无需重复多次输入命令。

本篇文章将通过剧本的方式详细介绍ansible常用模块shell、script、file、fetch、copy、template、yum、yum_repository、service、cron、user、group、unarchive的实现。

一、shell模块

编写剧本用ansible shell模块安装并启动mariadb

yaml 复制代码
---
- hosts: webserver
  tasks:
    - name: install mariadb
      shell: yum install -y mariadb
    - name: install mariadb-server
      shell: yum install -y mariadb-server
    - name: start mariadb service
      shell: systemctl start mariadb

从上述playbook可以看出,剧本中包含3个任务,分别是install mariadb、install mariadb-server、start mariadb service,shell模块执行的命令和直接服务器上执行的shell命令一样。

在以上执行结果中有一个"Gathering Facts"任务,这个任务会收集目标节点的信息,收集的信息具体信息可以通过命令ansible webserver -m setup查看。可以通过ansible变量获取这些信息,后续将会介绍,如果剧本中没有指明是否收集信息,这默认会自动收集节点信息。如果剧本中用不到与节点相关的信息,这可以通过指明gather_facts: no关掉信息收集,这样可以提高剧本的执行速度。

二、script模块

编写ansible playbook使用script模块获取被管理节点的磁盘使用情况。

yaml 复制代码
---
- hosts: webserver
  gather_facts: no
  tasks:
    - name: 获取磁盘使用信息
      script: /data/project1/disk_check.sh
      register: diskinfo
    - name: 显示磁盘使用信息
      debug:
        msg: "{{diskinfo}}"

这个剧本通过执行控制节点上的脚本/data/project1/disk_check.sh获取被管理节点的磁盘使用信息,由于脚本执行返回的结果在剧本执行结果中不能显示,需要将返回结果通过register注册为一个变量,然后通过debug模块读取磁盘的信息。

三、file模块

编写ansible playbook使用file模块创建文件、目录、链接文件。

yaml 复制代码
---
- hosts: webserver
  gather_facts: no
  tasks:
    - name: create a file
      file:
        path: /data1/file.txt
        state: touch
        mode: 644
    - name: create a directory
      file:
        path: /data2/
        state: directory
        mode: 755
    - name: create a link
      file:
        src: /data1/file.txt
        dest: /data1/linkfile.txt
        state: link

删除链接文件、文件、目录:

yaml 复制代码
---
- hosts: webserver
  gather_facts: no
  tasks:
    - name: delete directory
      file:
        path: /data2
        state: absent
    - name: delete file
      file:
        path: /data1/file.txt
        state: absent
    - name: delete link file
      file:
        path: /data1/linkfile.txt
        state: absent

四、copy模块

编写ansible playbook使用copy模块将控制节点的文件拷贝到被管理节点。

yaml 复制代码
---
- hosts: webserver
  gather_facts: no
  tasks:
    - name: copy file to remote target
      copy:
        src: /etc/hosts
        dest: /tmp/
        backup: yes
        mode: 0644

五、template模块

编写ansible playbook使用template模块将控制节点的模板文件拷贝到被管理节点。

yaml 复制代码
---
- hosts: webserver
  tasks:
    - template: 
        src: template.j2
        dest: /tmp/

以上模板文件中的变量可以通过ansible webserver -m setup获取。

六、yum模块

编写ansible playbook使用yum模块安装tftp和php。

yaml 复制代码
---
- hosts: webserver
  gather_facts: no
  tasks:
    - name: yum install tftp and php
      yum:
        name: tftp,php
        state: present

七、yum_repository模块

编写ansible playbook使用yum_repository模块创建一个yum仓库。

yaml 复制代码
---
- hosts: webserver
  tasks:
    - name: create a yum repo
      yum_repository:
        name: centos_repo
        description: 'create a centos yum repo'
        baseurl: 'http://mirrors.aliyun.com/centos/$releasever/os/$basearch/'
        enabled: yes
        gpgcheck: no

八、service模块

编写ansible playbook使用service模块对nginx服务进行管理。

yaml 复制代码
---
- hosts: webserver
  tasks:
    - name: start nginx     #启动nginx
      service:
        name: nginx
        state: started
    - name: restart nginx   #重启nginx
      service:
        name: nginx
        state: restarted
    - name: stop nginx      #停止nginx
      service:
        name: nginx
        state: stopped
    - name: reload nginx    #重载nginx
      service:
        name: nginx
        state: reloaded
    - name: enable nginx    #配置nginx服务重启自启动
      service:
        name: nginx
        enabled: yes

九、cron模块

编写ansible playbook使用cron模块添加计划任务。

yaml 复制代码
---
- hosts: webserver
  tasks:
    - name: add cron to host
      cron:
        name: clean file
        job: 'find /tmp -type f -mtime +7 -delete'
        minute: 0
        hour: 2

任务计划已经被添加到被管理节点。

十、user模块

编写ansible playbook使用user模块在被管理节点上添加用户。

yaml 复制代码
---
- hosts: webserver
  tasks:
    - name: add user
      user:
        name: user10       #添加用户user10
        shell: /bin/bash   
        create_home: yes   #创建家目录

十一、unarchive模块

编写ansible playbook使用unarchive模块将控制节点上的压缩包拷贝到被管理节点并解压。

yaml 复制代码
---
- hosts: webserver
  tasks:
    - name: unarchive package
      unarchive:
        src: nginx-1.30.4.tar.gz 
        dest: /tmp/

  以上介绍了11个模块的playbook,相信对playbook已经有一个较深入的理解,后续将继续介绍在playbook中使用变量、条件、循环等,使剧本具有更多特性,能够更加灵活的管理服务器。

  如果您对本节介绍内容有疑问,欢迎留言交流!

相关推荐
Wang's Blog2 分钟前
Java 项目实战: 外卖平台-删除分类的关联校验与自定义业务异常
java·服务器·项目开发
INGNIGHT5 分钟前
270 · 电话号码的字母组合II(Trie)
linux·算法
zly35006 分钟前
VMware Converter Standalone 物理机转化为虚拟机后源1硬盘变成了2个硬盘(2个硬盘文件)虚拟机无法启动。
linux·运维·服务器
智能运维指南20 分钟前
2026 企业智能运维平台选型:私有化、信创、全链路打通该怎么权衡?
运维·嘉为蓝鲸·aiops平台·一体化运维平台
学习中的码虫29 分钟前
网络编程5
服务器·网络
春天花会开13143 分钟前
Supabase 自部署 Edge Functions 超时从 60s 延长到 360s 完整复盘
运维·云原生
当下新鲜事1 小时前
脱硫脱硝塔动力系统科普:四方DL500变频器工作原理解析
大数据·运维·物联网·业界资讯
GPU实战笔记1 小时前
本地跑不动 llama.cpp,临时租云 GPU 怎么搭?从启动服务到环境复用
java·服务器·网络·人工智能·深度学习·llama
乐橙开放平台1 小时前
从「多套客户端」到一套开放能力:乐橙视频监控能力复盘
笔记·物联网·自动化·音视频·智能家居
Lsetea2 小时前
Nginx报No required SSL certificate was sent:mTLS客户端证书排查
运维·nginx·https·ssl·openssl