自动化运维工具Ansible之playbook剧本
一 playbook
1、playbook简述
playbook是ansible用于配置,部署,和管理被控节点的剧本。通过playbook的详细描述,执行其中的tasks,可以让远端主机达到预期的状态。playbook是由一个或多个”play”组成的列表。 当对一台机器做环境初始化的时候往往需要不止做一件事情,这时使用playbook会更加适合。通过playbook你可以一次在多台机器执行多个指令。通过这种预先设计的配置保持了机器的配置统一,并很简单的执行日常任务。
ansible通过不同的模块实现相应的管理,管理的方式通过定义的清单文件(hosts)所管理的主机包括认证的方式连接的端口等。所有的功能都是通过调用不同的模块(modules)来完成不同的功能的。不管是执行单条命令还是play-book都是基于清单文件。
2、playbook剧本格式
playbook由YMAL语言编写,YMAL格式是类似于JSON的文件格式,一个文件中有多个play,一个play中只能有一个tasks,一个tasks中可以有多个name任务。
注意事项:
①大小写明个
②通过缩进表示层级关系
③不支持制表符tab键缩进,只能使用空格缩进
④缩进的空格数目不重要,只要相同层级左右对齐,通常开头缩进2个空格
⑤#号注释
⑥符号字符后缩进1个空格,如冒号:逗号,横杠-后面空一个格
⑦如果包含特殊字符用单引号和双引号引起来会作为字符串处理,单引号不识别变量,双引号识别变量。
二 playbook启动及检测
ansible-playbook 文件名.yaml
ansible-playbook 文件.yaml --start-at-task=‘任务名/标签’ #从某个task开始执行或只执行某个标签的name
#启动此文件
补充参数:-k(-ask-pass):用来交互式输入ssh密码
-K(-ask-become-pass):用来交互式输入sudo密码
-U:指定用户
ansible-playbook 文件.yaml --list-task #检查yml文件
ansible-playbook 文件.yaml --list-hosts #检测主机
ansible-playbook 文件.yaml --syntax-check #检测语法
三 ansible-playbook模块实战
3.1 playbook模块实战实例1
vim test1.yaml
#新建编辑yaml文件,内容如下
---
#ymal文件开头,可不写
- name: test
#定义play名称,可不写
gather_facts: false
#设置不进行facts信息收集,这可以加快执行速度,可省略默认开启
hosts: webservers
#要执行的组或者主机,webservers为组名需要在/etc/ansible/hosts中配置
remote_user: root
#执行时所使用的用户
tasks:
#定义任务列表,列表中任务按顺序执行
- name: test ping
#自定义name的任务名称
ping:
#第一个任务执行内容为使用ping模块ping,webservers组的主机
- name: test selinux
command: /sbin/setenforce 0
#第二个任务执行内容为使用command模块关闭selinux
ignore_errors: True
#若出现错误,忽略继续运行下面的任务,默认为true出错立即停止playbook
- name: disable firewalld
service: name=firewalld state=stopped
#第三个任务执行内容为使用service模块关闭firewalld防火墙
- name: test yum
yum: name=httpd state=latest
#第四个任务执行内容为使用yum模块安装httpd服务状态为latest
- name: test copy
copy: src=/opt/httpd.conf dest=/etc/httpd/conf/httpd.conf
#第五个任务为使用copy模块将ansible上的/etc/httpd.conf文件拷贝到指定组的主配置文件下替换,注意此处需要在ansible的/opt目录上有一个httpd.conf的模版否则会报错
notify: "restart httpd"
#如果上面的copy任务成功,则调用handlers模块的restart httpd,注意要与下面handlers的name名称相同
- name: test start httpd
service: enabled=yes name=httpd state=started
#第六个任务执行内容为使用service模块启动httpd服务并设置开机自启
handlers:
- name: restart httpd
service: name=httpd state=restarted
#handlers模块,若notify成立则执行此模块内容调用service模块重启httpd服务。注意使用name名称调用
3.2 vars模块实战实例2
---
- name:
hosts: webservers
remote_user: root
ignore_errors: false
vars:
#使用变量模块
- groupname: testgroup
#定义变量groupname的值为testgroup
- username: testuser
#定义变量username的值为testuser
tasks:
- name: create group
group: name={
{
groupname}} gid=111
#第一个任务使用group模块调用定义的groupname变量创建组
- name: create user
user: name={
{
username}} uid=10086 group={
{
groupname}}
#第二个任务使用user模块调用定义的username变量创建用户指定uid和组
3.3 when模块实战实例3
---
- hosts: webservers
remote_user: root
tasks:
- name: test when
service: name=httpd state=stopped
when: ansible_default_ipv4.address == "192.168.10.20"
#当内置的变量ipv4.address等于192.168.10.20时调用service模块关闭httpd服务
3.4 with_items循环模块实战实例4
---
- name: test1
hosts: webservers
gather_facts: false
tasks:
- name: create directories
file:
path: "{
{item}}"
state: directory
#路径调用变量item,变量值为下面的with_items的内容,一次执行一个有几个值执行几次,state为创建类型为目录
with_items:
- /tmp/test1
- /tmp/test2
- name: create file
file:
path: "{
{item}}"
state: touch
#路径调用变量item,变量值为下面的with_items的内容,一次执行一个有几个值执行几次,state为创建类型为文件
with_items:
- /tmp/test1/a.txt
- /tmp/test2/b.txt
3.5 tags模块实战实例5
---
- hosts: webservers![请添加图片描述](https://img-blog.csdnimg.cn/2e546c3c2be5458c9dff757d5e200d69.png)
remote_user: root
gather_facts: false
tasks:
- name: copy hosts
copy: src=/etc/hosts dest=/opt/hosts
tags:
- tags1
#自定义标签名,可以在执行yaml文件时使用 --tag="tags1"只执行此任务,always标签任务除外
- name: touch file
file: path=/opt/testhost state=touch
tags:
- always
#always标签,无论执行那个标签都会将此标签的任务内容执行