ansible playbook使用方法

内容纲要

什么是ansible playbook?

上一章节介绍了ad-hoc的使用方法,本章介绍一下ansible playbook的使用方法。当我们在执行多条ad-hoc命令时,需要一行一行的执行完才能达到想要的效果。这里我们可以把多条ad-hoc命令写成playbook,通过playbook运行多条命令。playbook里支持更高级的功能,变量、判断等等。

playbook语法

playbook是用yaml语法写的,yaml语法具有固定的格式,只要我们遵从yaml语法,写出来的playbook是没有任何问题的。
yaml维基百科:https://zh.wikipedia.org/wiki/YAML

playbook实例

下面我模拟一个需求,编写一个playbook。通过ansible来实现需求的自动化实施。

需求模拟
1.关闭selinux
2.关闭防火墙
3.安装一个apache服务
4.把本地的html页面上传到apache html目录
5.启动apache服务,并设置为开机自启

---
- hosts: test
  remote_user: root

  tasks:
  - name: Disable Selinux
    selinux:
     state: disabled

  - name: Disable Firewalld
    service: name=firewalld state=stopped enabled=no

  - name: Install Apache Service
    yum: name=httpd state=latest

  - name: Copy Local html to html dir
    copy: src=~/index.html dest=/var/www/html/index.html

  - name: Start Apache Service
    service: name=httpd state=started enabled=yes

ansible playbook

在ansible中playbook是必须掌握的,后期会在此之上添加一些变量,控制语句等使运维更加灵活。

相关链接:(ansible-playbook)
https://docs.ansible.com/ansible/latest/cli/ansible-playbook.html

上一篇:(ansible ad-hoc常用模块介绍)
https://www.wenjiangun.com/blog/1211/

spacer

Leave a reply

评论审核已启用。您的评论可能需要一段时间后才能被显示。

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据