Ansible剧本文件应用
1.)批量管理剧本概念介绍及应用
#作用说明:具有将多个模块功能作用整合功能,可以提高批量管理工作效率
1.2)批量管理剧本编写规范
#剧本组成部分:
• host定义需要管理主机信息参照/etc/ansible/hosts
• task定义具体要执行任务信息需要依靠各种模块功能完成任务
1.3)编写语法规范:3个要求:
1)满足缩进要求:以两个空格作为一个缩进级别
举例:
[root@m01 ansible_script]# cat ansible_脚本01.yaml
hosts:
stangj
tasks:
name: 一个例子
yum:
name: htop
state: present
2)满足字典要求(key: value):需要用冒号分割,同一行时冒号后有空格,不同行是冒号后面无需加入空格
host:(空格)oldboy
tasks:(无需空格)
name:(空格)xXX
3)满足列表要求:需要将同级别或同功能配置
[root@m01 ansible_script]# cat ansible_脚本01.yaml
- hosts: stangj
tasks:
- name: 一个例子
yum:
name: htop
state: present
#编写简单剧本:一键化部署rsync备份服务
准备环境:最干净的环境---剧本执行过程幂等性不强客户端主机:web01 web02
服务端主机:backup
`第一个历程`:定义主机清单信息
[root@m01 ansible]# tail -6 hosts
[rsync_server]
192.168.139.101
[rsync_client]
192.168.139.99
192.168.139.100
`第二个历程`:编写剧本文件
[root@m01 ansible_script]# cat ansible_rsync.yaml
- hosts: rsync_server
tasks:
- name: 下载rsync
yum:
name: rsync
state: present
- name: 发送配置文件
copy:
src: /etc/ansible/ansible_script/rsync/rsyncd.conf.tmplate
dest: /etc/rsyncd.conf
- name: 创建虚拟用户
user:
name: rsync
shell: /sbin/nologin
create_home: no
- name: 创建备份目录
file:
path: /backup
state: directory
owner: rsync
group: rsync
- name: 创建密码文件信息
copy:
content: rsync_backup:123456
dest: /etc/rsync_password
mode: 0600
- name: 启动程序
service:
name: rsyncd
state: started
enable: true
- hosts: rsync_client
tasks:
- name: 下载rsync
yum:
name: rsync
state: present
- name: 创建密码文件
copy:
content: 123456
dest: /etc/rsync_password
mode: 0600
`rsyncd.conf.tmplate`的配置文件
[root@m01 ansible_script]# cat rsyncd.conf.tmplate
uid = rsync
gid = rsync
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 300
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
ignore errors
read only = false
list = false
hosts allow = 192.168.139.0/24
hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync_password
[backup]
comment = "backup dir by oldboy"
path = /backup
`第三个历程`:检查并执行剧本文件
[root@m01 ansible_script]# ansible-playbook --syntax-check /etc/ansible/ansible_script/ansible_rsync.yaml
[root@m01 ansible_script]# ansible-playbook /etc/ansible/ansible_script/ansible_rsync.yaml
`第四个历程`:检查剧本文件是否到达预期效果
[root@web01 ~]# rsync -avz /etc/hosts rsync_backup@192.168.139.101::backup --password-file=/etc/rsync_password
[root@web01 ~]# rsync -avz /etc/sysconfig/network-scripts/ifcfg-ens33 rsync_backup@192.168.139.101::backup --password-file=/etc/rsync_password
[root@web01 backup]# cd /backup/;ls
1.5)编写一键化部署NFS备份服务
#编写简单剧本:一键化部署rsync备份服务
准备环境:最干净的环境---剧本执行过程幂等性不强客户端主机:web01 web02
服务端主机:nfs
`第一个历程`:定义主机清单信息
[root@m01 nfs]# tail -6 /etc/ansible/hosts
[nfs_server]
192.168.139.102
[nfs_client]
192.168.139.100
192.168.139.99
`第二个历程`:编写剧本文件
[root@m01 nfs]# cat ansible_nfs.yaml
- hosts: nfs_server
tasks:
- name: 下载nfs
yum:
name: nfs-utils
state: present
- name: 发送配置文件
copy:
src: /etc/ansible/ansible_script/nfs/exports.conf.tmplate
dest: /etc/exports
- name: 创建存储目录
file:
path: /data
state: directory
owner: nfsnobody
group: nfsnobody
- name: 启动程序
systemd:
name: nfs
state: started
enabled: true
- hosts: nfs_client
tasks:
- name: 下载nfs
yum:
name: nfs-utils
state: present
- name: 启动程序
systemd:
name: nfs
state: started
enabled: true
- name: 实现存储挂载过程
mount:
src: 192.168.139.102:/data
path: /mnt
fstype: nfs
opts: defaults
state: mounted
- name: 检查挂载情况
shell:
cmd: df -TH |grep mnt
register: mount_info
- name:
debug:
msg: "{{ mount_info.stdout_lines }}"
`exports.conf.tmplate`配置文件信息
[root@m01 nfs]# cat exports.conf.tmplate
/data 192.168.139.0/24(rw,sync)
`第三个历程`:检查并执行剧本文件
[root@m01 nfs]# ansible-playbook --syntax-check /etc/ansible/ansible_script/nfs/ansible_nfs.yaml
[root@m01 nfs]# ansible-playbook /etc/ansible/ansible_script/nfs/ansible_nfs.yaml
2.)批量管理剧本编写方法
2.1)在剧本中设置变量信息
作用说明:可以方便剧本中部分关键信息调整变化
需求:需要创建一个目录并在创建后目录中创建文件并设置文件权限
`第一种方式`:在剧本文件直接设置变量
[root@m01 test]# cat ansible_脚本02.yaml
- hosts: 192.168.139.100
vars:
name_info: data01
tasks:
- name: 01-创建目录
file:
path: /{{ name_info }}
state: directory
- name: 02-创建文件信息
file:
path: /{{ name_info }}/{{ name_info }}.txt
state: touch
- name: 03-设置权限信息
file:
path: /{{ name_info }}/{{ name_info }}.txt
mode: 0666
[root@m01 test]# ansible-playbook --syntax-check ansible_脚本02.yaml
`第二种方式`:在主机清单配置文件中设置变量
主机清单自定义变量设置方法:
1)单独设置主机变量
[root@m01 test]# tail -1 /etc/ansible/hosts
192.168.139.100 name_info="data"
2)对主机组设置变量
[root@m01 test]# tail -5 /etc/ansible/hosts
192.168.139.99
192.168.139.100
[rsync_client:vars]
name_info="data"
`第二种方式`:通过命令参数设置变量
[root@m01 test]# cat ansible_脚本02.yaml
- hosts: 192.168.139.100
tasks:
- name: 01-创建目录
file:
path: /{{ name_info }}
state: directory
- name: 02-创建文件信息
file:
path: /{{ name_info }}/{{ name_info }}.txt
state: touch
- name: 03-设置权限信息
file:
path: /{{ name_info }}/{{ name_info }}.txt
mode: 0666
[root@m01 test]# ansible-playbook -e name_info="oldboy" ansible_脚本02.yaml
2.2)在剧本中设置注册信息
#作用说明:可以对剧本操作任务有提示输出信息
[root@m01 test]# cat ansible_脚本03.yaml
- hosts: rsync_server
tasks:
- name: 启动rsync程序
service:
name: rsyncd
state: started
- name: 检查服务状态
shell:
cmd: ps -ef | grep rsync
register: rsync_ps_info
- name: 输出检查结果
debug: msg={{ rsync_ps_info.stdout_lines }}
2.3)在剧本中设置判断信息
#作用说明:可以根据识别不同主机硬件信息或不同主机系统情况,做不同的处理
[root@m01 test]# cat ansible_脚本04.yaml
- hosts: rsync_client
tasks:
- name: 不同主机创建不同目录web01
file:
path: /web01
state: directory
when: ansible_ens33.ipv4.address == "192.168.139.100"
- name: 不同主机创建不同目录web02
file:
path: /web02
state: directory
when: ansible_ens33.ipv4.address == "192.168.139.99"
#如何判断: setup模块收集到网络信息做判断
常用判断条件信息:
ansible_all_ipv4_addresses: 仅显示ipv4的信息。
ansible_devices: 仅显示磁盘设备信息。
ansible_distribution: 显示是什么系统,例: CentOS , suse等。
ansible_distribution_major_version: 显示是系统主版本。
ansible_distribution_version: 仅显示系统版本。
ansible_machine: 显示系统类型,例:32位,还是64位。
ansible_eth0: 仅显示eth0的信息。
ansible_hostname: 仅显示主机名。
ansible_kernel: 仅显示内核版本。
ansible_lvm: 显示lvm相关信息。
ansible_memtotal_mb: 显示系统总内存。
ansible_memfree_mb: 显示可用系统内存。
ansible_memory_mb: 详细显示内存情况。
ansible_swaptotal_mb: 显示总的swap内存。
ansible_swapfree_mb: 显示swap内存的可用内存。
ansible_mounts: 显示系统磁盘挂载情况。
ansible_processor: 显示cpu个数(具体显示每个cpu的型号)。
ansible_processor_vcpus: 显示cpu个数(只显示总的个数)。
1.单一条件做判断:
when: ansible_ens33.ipv4.address == "192.168.139.100"
2.多个条件做判断:与或关系
when: (ansible_ens33.ipv4.address == "192.168.139.100") and (ansible_distribution = CentOS)
when: (ansible_ens33.ipv4.address == "192.168.139.100") or (ansible_distribution = CentOS)
2.4)在剧本中设置循环信息
#作用说明:可以利用循环功能简化剧本编辑信息
#在被管理主机上创建3个用户stj01 stj02
1.单一变化信息进行循环:
[root@m01 test]# cat ansible_脚本05.yaml
- hosts: rsync_client
tasks:
- name: 创建用户
user:
name: "{{ item }}"
with_items:
- stj01
- stj02
2.多个变化信息进行循环
[root@m01 test]# cat ansible_脚本05.yaml
- hosts: 192.168.139.100
tasks:
- name: 创建用户
user:
name: "{{ item.name }}"
uid: "{{ item.id }}"
shell: "{{ item.login }}"
loop:
- { name: 'tang01', id: '7771', login: '/bin/bash' }
- { name: 'tang02', id: '7772', login: '/sbin/nologin' }
- { name: 'tang03', id: '7773', login: '/bin/sh' }
2.5)在剧本中设置错误忽略
#作用说明:提高剧本执行幂等性
[root@m01 test]# cat ansible_脚本06_错误忽略.yaml
- hosts: rsync_client
tasks:
- name: 不同主机创建不同目录web01
file:
path: /web01
stte: directory
when: ansible_ens33.ipv4.address == "192.168.139.100"
ignore_errors: yes
- name: 不同主机创建不同目录web02
file:
path: /info
state: directory
when: ansible_ens33.ipv4.address == "192.168.139.99"
2.6)在剧本中设置标签信息
#作用说明:可以对指定任务信息做标记
[root@m01 test]# cat ansible_脚本07_标记.yaml
- hosts: rsync_client
tasks:
- name: 不同主机创建不同目录web01
file:
path: /web01
stte: directory
when: ansible_ens33.ipv4.address == "192.168.139.100"
tags: create_directory
- name: 不同主机创建不同目录web02
file:
path: /info
state: directory
when: ansible_ens33.ipv4.address == "192.168.139.99"
`方式一`:选择标记任务进行执行
[root@m01 test]# ansible-playbook -t create_directory ansible_脚本07_标记.yaml
`方式二`:跳过标记功能
[root@m01 test]# ansible-playbook --skip-tags create_directoy ansible_脚本07_标记.yaml
2.7)在剧本中设置触发信息
#作用说明:当某个任务信总执行时,才会执行其他某个任务,当某个任务未操作时,其他任务无需执行而求:创建备份服务剧本:
如果配置文件改动,就需要重启服务程序
A任务执行 B任务才执行
[root@m01 test]# cat ansible_脚本08_触发器.yaml
- hosts: rsync_client
tasks:
- name: 不同主机创建不同目录web01
file:
path: /web01
state: directory
when: ansible_ens33.ipv4.address == "192.168.139.100"
notify: restarted_rsync
- name: 不同主机创建不同目录web02
file:
path: /info
state: directory
when: ansible_ens33.ipv4.address == "192.168.139.99"
handlers:
- name: restarted_rsync
service:
name: rsyncd
state: restarted
3)批量管理剧本整合方法
3.1)利用剧本指令进行整合简单方式
[root@m01 summary]# vim summary.yaml
- import_playbook: /etc/ansible/ansible_script/rsync/ansible_rsync.yaml
- import_playbook: /etc/ansible/ansible_script/nfs/ansible_nfs.yaml
[root@m01 summary]# cat /etc/ansible/ansible_script/rsync/ansible_rsync.yaml
- hosts: rsync_server
tasks:
- name: 下载rsync
yum:
name: rsync
state: present
- name: 发送配置文件
copy:
src: /etc/ansible/ansible_script/rsync/rsyncd.conf.tmplate
dest: /etc/rsyncd.conf
- name: 创建虚拟用户
user:
name: rsync
shell: /sbin/nologin
create_home: no
- name: 创建备份目录
file:
path: /backup
state: directory
owner: rsync
group: rsync
- name: 创建密码文件信息
copy:
content: rsync_backup:123456
dest: /etc/rsync_password
mode: 0600
- name: 启动程序
systemd:
name: rsyncd
state: started
enabled: true
- hosts: rsync_client
tasks:
- name: 下载rsync
yum:
name: rsync
state: present
- name: 创建密码文件
copy:
content: 123456
dest: /etc/rsync_password
mode: 0600
`rsyncd.conf.tmplate`的配置文件
[root@m01 ansible_script]# cat /etc/ansible/ansible_script/rsync/ansible_rsync.yaml
vi /etc/rsyncd.conf
uid = rsync
gid = rsync
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 300
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
ignore errors
read only = false
list = false
hosts allow = 192.168.139.0/24
hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync_password
[backup]
comment = "backup dir by oldboy"
path = /backup
[root@m01 summary]# cat /etc/ansible/ansible_script/nfs/ansible_nfs.yaml
- hosts: nfs_server
tasks:
- name: 下载nfs
yum:
name: nfs-utils
state: present
- name: 发送配置文件
copy:
src: /etc/ansible/ansible_script/nfs/exports.conf.tmplate
dest: /etc/exports
- name: 创建存储目录
file:
path: /data
state: directory
owner: nfsnobody
group: nfsnobody
- name: 启动程序
systemd:
name: nfs
state: started
enabled: true
- hosts: nfs_client
tasks:
- name: 下载nfs
yum:
name: nfs-utils
state: present
- name: 启动程序
systemd:
name: nfs
state: started
enabled: true
- name: 实现存储挂载过程
mount:
src: 192.168.139.102:/data
path: /mnt
fstype: nfs
opts: defaults
state: mounted
- name: 检查挂载情况
shell:
cmd: df -TH |grep mnt
register: mount_info
- name:
debug:
msg: "{{ mount_info.stdout_lines }}"
`exports.conf.tmplate`配置文件信息
[root@m01 summary]# cat /etc/ansible/ansible_script/nfs/exports.conf.tmplate
/data 192.168.139.0/24(rw,sync)
`第三个历程`:检查并执行剧本文件
[root@m01 summary]# ansible-playbook --syntax-check summary.yaml
[root@m01 summary]# ansible-playbook summary.yaml
3.2)利用角色功能进行整合专业方式
作用介绍:可以将多个不同业务剧本整合为不同角色
rsync业务部署 playbook_rsync剧本 --- rsync角色
nfs业务部署 playbook_nfs剧本 --- nfs角色
`需要用到的文件信息`
mkdir /etc/ansible/roles/roles_rsync/{tasks,files,templates,vars,handlers,meta}
tasks:在此目录中可以定义任务配置文件信息存放任务
files:在此目录中可以存放需要分发文件信息存放分发文件信息普通文件
templates:在此目录中可以存放需要分发文件信息――存放分发文件信息 jinja2模板文件(支持变量)
vars:在此目录中可以存放变量信息文件存放角色变量信息
handlers:在此目录中可以存放触发器配置信息存放handlers配置
meta:在此目录中可以存放角色执行依赖关系
3.3)剧本整合 vs 角色整合
1)角色整合更加专业(文件存放路径主机清单设置所有配置信息可以拆分管理)
2)角色任务执行过程中可以查看到主机信息
3)角色配置部署过程复杂