Ansible-palybook学习

目录

一.playbook介绍

playbook 是 ansible 用于配置,部署,和管理被控节点的剧本 。通过 playbook 的详细描述,执行其中的一系列 tasks ,可以让远端主机达到预期的状态。playbook 就像 Ansible 控制器给被控节点列出的的一系列 to-do-list ,而被控节点必须要完成。也可以这么理解,playbook 字面意思,即剧本,现实中由演员按照剧本表演,在 Ansible 中,这次由计算机进行表演,由计算机安装,部署应用,提供对外服务,以及组织计算机处理各种各样的事情

二.playbook格式

1.书写格式

  1. 文件的第一行应该以 "---" (三个连字符)开始,表明 YMAL 文件的开始。
  2. 在同一行中,# 之后的内容表示注释,类似于 shell,python 和 ruby。
  3. YMAL 中的列表元素以 "-" 开头然后紧跟着一个空格,后面为元素内容。
  4. 同一个列表中的元素应该保持相同的缩进。否则会被当做错误处理。
  5. play 中 hosts,variables,roles,tasks 等对象的表示方法都是键值中间以 ":"
    分隔表示,":" 后面还要增加一个空格

举例 安装挂载nfs服务

[root@tdm1 playbook]# cat nfs.yml 
---
- hosts: web
  remote_user: root
  tasks: 
    - name: install nfs
      yum:  name=rpcbind,nfs-utils state=present
    - name: nfs configure file
      copy: src=./export.j2 dest=/etc/exports backup=yes
    - name: mkdir share dir
      file: path=/data state=directory owner=nfsnobody group=nfsnobody
    - name: start rpcbind
      service: name=rpcbind state=started enabled=yes  
    - name: start nfs
      service: name=nfs  state=started enabled=yes  
    - name: mount local
      mount: src=47.93.98.117:/data path=/mnt fstype=nfs state=mounted

文件名称应该以.yml结尾

hosts :使用 hosts 指示使用哪个主机或主机组来运行下面的 tasks ,每个 playbook 都必须指定 hosts ,hosts 也可以使用通配符格式。主机或主机组在 inventory 清单中指定,可以使用系统默认的 /etc/ansible/hosts,也可以自己编辑,在运行的时候加上 -i 选项,指定清单的位置即可。在运行清单文件的时候,--list-hosts 选项会显示那些主机将会参与执行 task 的过程中。
remote_user:指定远端主机中的哪个用户来登录远端系统,在远端系统执行 task 的用户,可以任意指定,也可以使用 sudo,但是用户必须要有执行相应 task 的权限。
tasks:指定远端主机将要执行的一系列动作。tasks 的核心为 ansible 的模块,前面已经提到模块的用法。tasks 包含 name 和要执行的模块,name 是可选的,只是为了便于用户阅读,不过还是建议加上去,模块是必须的,同时也要给予模块相应的参数。

使用ansible-playbook运行playbook文件,得到以下输出信息,输出内容为json格式,由不同颜色组成
绿色代表执行成功,系统保持原样
黄色代表系统状态发生改变
红色代表失败,显示错误输出

执行有三个步骤:

1.收集facts

  1. 执行tasks

  2. 报告结果

2.notify介绍

Ansible提供了notify 指令和handlers 功能。如果在某个task中定义了notify指令,当Ansible在监控到该任务 changed=1时,会触发该notify指令所定义的handler,然后去执行handler。所谓handler ,其实就是task ,无论在写法上还是作用上它和task都没有区别,唯一的区别在于hander是被触发而被动执行的,不像普通task一样会按流程正常执行

测试1
当检测到nfs的配置发生变化是,会重启nfs服务和重新挂载
notify和handlers中定义的名称必须一致

[root@tdm1 playbook]# cat nfs.yml 
---
- hosts: web
  tasks: 
    - name: install nfs
      yum:  name=rpcbind,nfs-utils state=present
    - name: nfs configure file
      copy: src=./export.j2 dest=/etc/exports backup=yes
      notify: restart nfs   #当export.j2文件发生变化,就会由handlers来执行。
    - name: mkdir share dir
      file: path=/data state=directory owner=nfsnobody group=nfsnobody
    - name: start rpcbind
      service: name=rpcbind state=started enabled=yes  
    - name: start nfs
      service: name=nfs  state=started enabled=yes  
    - name: mount local
      mount: src=47.93.98.117:/data path=/mnt fstype=nfs state=mounted
      notify: client remount
  handlers: 
    - name: restart nfs     #名称和notify中的一致
      service: name=nfs  state=restarted 
    - name: client remount
      service: src=47.93.98.117:/data path=/mnt fstype=nfs state=remounted

#修改export.j2的内容
vim export.j2
/data 47.93.98.0/24(rw,all_squash)

执行剧本,观察

#查看文件是否更改
[root@tdm1 playbook]# ansible web -m shell -a 'cat /etc/exports'
47.93.98.117 | CHANGED | rc=0 >>
/data 47.93.98.0/24(rw,all_squash)

测试2
修改nginx的文件,检测到文件被修改,handlers下面任务会被执行

[root@tdm1 playbook]# cat nginx.yml 
---
- hosts: web
  tasks: 
    - name: install  nginx 
      yum:
        name: nginx
        state: installed
    - name: index file
      copy:
        content: "This is ansible test"
        dest: /usr/share/nginx/html/index.html
    - name: nginx configure file
      copy:
        src: ./nginx.conf.j2
        dest: /etc/nginx/conf.d/default.conf
        backup: yes
      notify: restart nginx    #文件被修改,重启nginx
    - name: start nginx
      service: 
        name: nginx
        state: started
  handlers:
    - name: restart nginx     #重启nginx
      service: 
        name: nginx
        state: restarted

#修改nginx.conf.j2的文件
vim nginx.conf.j2
server {
    listen       81;
    server_name  localhost;
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
}

参考:https://blog.csdn.net/u012562943/category_6298590.html

相关推荐
神秘的土鸡42 分钟前
Linux中使用Docker容器构建Tomcat容器完整教程
linux·运维·服务器·docker·容器·tomcat
TravisBytes1 小时前
linux 系统是如何收发数据包
linux·运维·服务器
德迅--文琪3 小时前
SCDN是服务器吗?SCDN防御服务器有什么特点?
运维·服务器
ice___Cpu3 小时前
Linux 基本使用和 web 程序部署 ( 8000 字 Linux 入门 )
linux·运维·前端
z202305083 小时前
linux 之0号进程、1号进程、2号进程
linux·运维·服务器
Hqst_Kevin4 小时前
Hqst 品牌 H81801D 千兆 DIP 网络变压器在光猫收发器机顶盒中的应用
运维·服务器·网络·5g·网络安全·信息与通信·信号处理
honey ball5 小时前
仪表放大器AD620
运维·单片机·嵌入式硬件·物联网·学习
秋已杰爱5 小时前
进程间关系与进程守护
运维·服务器
Flying_Fish_roe5 小时前
linux-软件包管理-包管理工具(Debian 系)
linux·运维·debian
BLEACH-heiqiyihu5 小时前
红帽9中nginx-源码编译php
运维·nginx·php