利用剧本可以实现自动化部署服务功能
备份服务部署过程:
服务端:下载软件程序--编写配置文件--创建虚拟用户--创建密码文件(600)--创建备份目录(rsync)--启动服务
客户端:下载软件程序--创建密码文件(600)
剧本编写规范:
两部分内容: 角色信息 角色任务
- host: all (指定执行剧本的主机)
task: (指定剧本都有哪些操作)
具体编写规范: pyyaml格式
三个要求:1)满足信息缩进规范:以两个空格作为一个缩进关系
2)满足信息字典规范:在使用冒号时,后面需要跟上空格 冒号结尾不需要有空格
3)满足信息列表规范:短横线加空格构成列表信息
- hosts: all 指定执行任务的主机
remote_user: root 指定执行任务的用户
tasks:
- yum: name=htop sate=installed
测试:ansible-playbook 剧本名
补充:ansible颜色信息说明
绿色信息:表示没有对远程主机做任何修改,只做了查看操作
黄色信息:表示对远程主机做了数据修改调整
红色信息:远程管理出现错误
紫色信息:警告提示信息
蓝色信息:代表命令或剧本执行过程信息 相当于sh -x +++
gather_facts: no 优化剧本执行速度
利用剧本实现安装备份服务:
ansible-playbook –syntax-check 对指定文件进行语法检查
ansible-playbook -C rsync_auth.yml 模拟执行剧本,类似彩排过程
1)修改hosts文件
2)添加剧本 vim rsync.yml
- hosts: rsync_server
gather_facts: no
remote_user: root
tasks:
- name: 下载安装软件
yum: name=rsync state=installed
- name: 编写配置文件
copy: src=/etc/ansible/ansible_playbook/rsyncd.conf dest=/etc/
- name: 创建用户
user: name=rsync create_home=no shell=/sbin/nologin
- name: 创建密码文件
copy: content='mwy:123456' dest=/etc/mwy mode=600
- name: 创建目录
file: path=/backup state=directory owner=rsync group=rsync
- name: 启动服务
service: name=rsyncd state=started enabled=yes
- hosts: rsync_client
gather_facts: no
tasks:
- name: 安装软件
yum: name=rsync state=installed
- name: 创建密码文件
copy: content=123456 dest=/etc/mwy mode=600
检查测试:ansible-playbook --syntax-check rsync_auth.yml
模拟执行过程:ansible-playbook -C rsync_auth.yml
利用剧本实现安装NFS服务
- hosts: nfs_server
gather_facts: no
tasks:
- name: 下载安装软件
yum: name=nfs-utils,rpcbind state=installed
- name: 修改配置文件
copy: content='/data 172.16.1.0/24(rw,sync)' dest=/etc/exports
- name: 创建共享分区并设置权限
file: state=directory path=/data owner=nfsnobody group=nfsnobody
- name: 启动服务
service: name=nfs state=started enabled=yes
- name: 启动服务
service: name=rpcbind state=started enabled=yes
- hosts: nfs_client
gather_facts: no
tasks:
- name: 下载安装软件
yum: name=nfs-utils
- name: 创建挂载点
file: state=directory path=/data
- name: 执行挂载
mount: src=172.16.1.31:/data path=/data fstype=nfs opts=defaults state=mounted
批量管理剧本特殊编写方法
掌握剧本变量设置方法
需求:进行网站服务数据迁移
1)在剧本中设置变量 优先级第二
- hosts: web
vars:
dir_info: /var/html
tasks:
- name: create dir
file: path={{dir_info}} state=directory
- name: move data
copy: src=data/www dest={{dir_info}} owner=wyd
2)在执行命令中设置变量 优先级最高
-e 用来指定剧本中变量信息
3)在主机清单中设置变量 优先级最低
vim /etc/ansible/hosts
① [web]
172.16.1.7 dir_info=/var/html
172.16.1.31 dir_info=/var/html
172.16.1.41 dir_info=/var/html
② [web:vars]
dir_info=/var/html
掌握剧本信息输出方法 (剧本注册功能)
需求:批量启动服务程序,并进行验证
- hosts: web
tasks:
- name: boot
service: name=rsyncd state=started
- name: check rsync status
shell: netstat -lntup | grep 873
register: port_info
- name: output check info
debug: msg={{ port_info.stdout_line }}
register:注册的意思
debug:调试
掌握剧本编写判断功能
需求:管理3台主机,安装软件程序 httpd
- hosts: web
tasks:
- name: mkdir dir
yum: name=httpd state=installed
when: (ansible_hostname == "mw3") or (ansible_default_ipv4.address == "10.0.0.7")
掌握剧本编写循环功能
需求: 启动多个服务
方法一: 简单循环方法
- hosts: web
tasks:
- name: service
service: name={{ item }} state=started
loop:
- rpcbind
- nfs-utils
需求:多个主机上创建目录信息:
-/test_A_dir mode=600 /test_B_dir mode=601 /test_C_dir mode
- hosts: web
tasks:
- name: touch
file: state=directory path={{ item.dir }}
mode={{item.mode}}
loop:
- {dir: /test_A_dir,mode: 600}
- {dir: /test_B_dir,mode: 601}
- {dir: /test_C_dir,mode: 602}
任务:创建不同需求用户
- hosts: web
tasks:
- name: useradd
user: name={{item.nm}} state=present uid={
{item.uid}} create_home={{item.hm}} shell={{item.she
ll}}
loop:
- {nm: wyd666,uid: 666,hm: 'no',shell: /bin/bash}
- {nm: wyd667,uid: 667,hm: 'yes',shell: /sbin/nologin}
- {nm: wyd668,uid: 668,hm: 'yes',shell: /bin/bash}
掌握剧本忽略错误功能
在使用模块shell万能模块时,是不具有一种特性的,这种特性称为幂等性
使用方法,在不确定是否有幂等性时,加入
ignore_errors: yes
掌握剧本排错标记功能
tags: t2
执行时,ansible-playbook -t t2 tags.xml 直接执行标签
想跳过标签,则添加 --skip-tags
ansible-playbook -skip-tags t2 tags.xml 跳过t2标签
掌握剧本加速执行方法
gather_facts: no
获取主机时间情况信息
掌握剧本触发执行功能
tasks:
- name: vim config
copy: src=./rsyncd.conf dest=/etc/
notify: restart rsyncd
handlers:
- name: restart rsyncd
service: name=rsyncd state=restarted
notify和handlers的name需一致
掌握剧本模块文件功能
方式一:
template: 可以识别变量信息 类似于"",和copy模块使用方法一样
方式二: 编写一个重复段落内容信息
①:vi test_template.yml
- hosts:
vars:
port:
- 80
- 81
- 82
tasks:
template: src=./test.j2 dest=/etc/service.conf
②:vi test.j2
{% for info in port %}
server {
listen {{ info }}
}
{% endfor %}
③:执行剧本 ansible-playbook test_temlate.yml
④:查看剧本
server {
listen 80
}
server {
listen 81
}
server {
listen 82
}
掌握剧本整合功能配置
方式一: 目前主流简单整合剧本方式
cat summary.yml
- import_playbook: test01.yml
- import_playbook: test02.yml
运行剧本: ansible-playbook summary.yml及同时执行test01,和test02
方式二:利用角色roles方式进行执行
标准汇总剧本实现自动化功能方式 etc/ansible/roles目录
1)创建不同角色目录
自动创建ansible-galexy into 加目录名,及连同子目录一同创建
mkdir roles/rsync
2)在角色目录中创建相应子目录
vars: 在此目录中可以设置变量目录
tasks: 在此目录中编写剧本任务信息
templates:在此目录中定义jinja2模板文件信息
handlers:在此目录中定义触发器功能信息
files: 在此目录中保存所有要分发的配置文件信息
mkdir roles/rsync/{vars,tasks,templates,handlers,files}