00.课程介绍部分
ansible剧本扩展功能
ansible剧本整合功能 编辑rsync一件化部署
ansible剧本角色配置 标准化配置
作业解答:
01. 修改目录权限属性信息: 问题* 如何递归修改目录权限???
ansible 172.16.1.31 -m file -a "path=/opt recurse=yes mode=755"
recurse=yes
02. 默认在删除用户是不会删除用户家目录. 问题* 如何将用户家目录删除 == userdel -r
ansible 172.16.1.31 -m user -a "name=oldboy state=absent remove=yes
remove=yes
03. 在进行判断匹配时,如何取出内置变量指定子集信息
方式一: 利用点进行分割取出子集信息
[root@m01 ansible_playbook]# cat test02.yml
- hosts: 172.16.1.41
tasks:
- name: show host_eth0 info
debug: msg={{ ansible_eth0.ipv4.address }}
方式二: 利用中括号分割取出子集信息
[root@m01 ansible_playbook]# cat test02.yml
- hosts: 172.16.1.41
tasks:
- name: show host_eth0 info
debug: msg={{ ansible_eth0["ipv4"]["address"] }}
[root@m01 ansible_playbook]# cat test02.yml
- hosts: all
tasks:
- name: copy file
copy: src=/etc/hosts dest=/tmp/
when: ansible_eth0.ipv4.address == "10.0.0.41"
04. 扩展研究 lineinfile
ansible 172.16.1.31 -m lineinfile -a "dest=/oldboy/oldboy.txt regexp='aaa(.*)' line='bbbb'" 替换某一行
ansible 172.16.1.31 -m lineinfile -a "dest=/oldboy/oldboy.txt insertbefore='aaa(.*)' 'line='bbbb'" 在一行前插入信息
insertafter='ccc(.*)' 在一行后插入信息
regexp='ggg(.*)' state=absent 删除某一行
dest=/oldboy/oldboy.txt line='bbbbbbbbbb'
ansible帮助信息查看方法:
01. 查看官方资料文档
02. 利用ansible帮助查看命令
ansible-doc -l --- 列出所有ansible模块信息
ansible-doc -s 模块名 --- 查看指定模块详细参数信息
ansible-doc 模块名 --- 显示的帮助信息更加详细
03.剧本编写扩展功能
1.剧本变量设置功能 3种方式
2.剧本变量注册功能 在剧本执行过程中显示指定的输出信息 register
- hosts: 172.16.1.41
tasks:
- name: boot crond server
service: name=crond state=started
- name: check server process
shell: ps -ef|grep cron
register: oldboy
- name: show server status info
debug: msg={{ oldboy.stdout_lines }}
3.剧本编写判断功能 根据条件信息执行任务 when条件变量信息(setup) ==/!= 指定信息
4.剧本编写循环功能
编写循环功能:
编写方式一: 列表方式设置循环
- hosts: 172.16.1.41
tasks:
- name: install software
yum: name={{ item }} state=installed
with_items:
- rsync
- nfs-utils
- telnet-server
编写方式二: 字典方式设置循环
- hosts: 172.16.1.41
tasks:
- name: create user
user: name={{ item.old01 }} uid={{ item.old02 }} shell={{ item.old03 }}
with_items:
- {old01: 'oldboy01', old02: '5001', old03: '/sbin/nologin'}
- {old01: 'oldboy02', old02: '5002', old03: '/sbin/nologin'}
5.剧本忽略错误功能 剧本中shell模块使用时错误问题
[root@m01 ansible_playbook]# cat test_忽略错误配置.yml
- hosts: 172.16.1.41
tasks:
- name: install software
shell: yum install -y htop
- name: create user
shell: useradd oldboy
ignore_errors: yes 开启忽略错误功能
- name: boot server
shell: systemctl start rsyncd
6) 剧本编写标签功能 调式剧本
- name: create user
user: name=rsync shell=/sbin/nologin create_home=no
tags: oldboy01
ansible-playbook test_标签功能配置.yml -t oldboy01 --- 只执行标记任务
ansible-playbook test_标签功能配置.yml --skip-tags oldboy01 --- 跳过标记任务
7) 剧本提高执行效率
取消剧本收集主机信息功能
- hosts: 172.16.1.41
gather_facts: no --- 提升剧本执行效率
tasks:
PS: 取消了收集信息功能, 判断功能也无法使用了
=================================================================================================================================
总结: 剧本执行慢的原因
01. SSH远程连接优化没有配置 关闭认证功能 关闭DNS反向解析功能
02. yum下面软件慢 使用本地yum仓库
03. 剧本执行收集信息慢
04. 剧本执行过程必须完整 yum -- 慢 -- ctrl+c -- yum lockfile is 调用
=================================================================================================================================
8) 剧本触发器功能配置
[root@m01 ansible_playbook]# cat test_触发功能配置.yml
- hosts: 172.16.1.41
tasks:
- name: push config file
copy: src=/tmp/rsyncd.conf dest=/etc/
notify: rsync_restart
- name: boot server
service: name=rsyncd state=started
handlers:
- name: rsync_restart
service: name=rsyncd state=restarted
PS: 触发器任务会在所有任务执行完毕之后才执行
剧本编写扩展功能: https://docs.ansible.com/ansible/latest/user_guide/playbooks.html
04. ansible剧本整合功能
方式一:include_tasks: f1.yml
- hosts: all
remote_user: root
tasks:
- include_tasks: f1.yml
- include_tasks: f2.yml
方式二:include: f1.yml
- include:f1.yml
- include:f2.yml
方式三:- import_playbook: 推荐
[root@m01 ansible-playbook]# cat main.yml
- import_playbook: base.yml
- import_playbook: rsync.yml
- import_playbook: nfs.yml
编写多个服务剧本:
rsync
nfs
第一个历程: 编写主机配置文件
vim /etc/ansible/hosts
[oldboy]
172.16.1.7
172.16.1.31
172.16.1.41
第二个历程: 创建存储分发文件目录
# ll file/
总用量 12
-rw-r--r-- 1 root root 29 8月 26 18:23 exports
-rw-r--r-- 1 root root 406 8月 26 17:59 rsyncd.conf
-rw-r--r-- 1 root root 23 8月 26 18:23 rsync.password
第三个历程: 编写一键化部署服务剧本:
rsync_auto.yml
省略
nfs_auto.yml
第四个历程: 将多个剧本进行整合
5.课程知识总结
1) ansible剧本编写功能
a 剧本变量设置功能 3种方法
b 剧本注册功能设置
c 剧本判断功能设置 setup 收集主机信息
d 剧本循环功能设置 2中方法 列表/字典
e 剧本忽略错误功能
f 剧本标签设置功能 -t --skip-tags
g 剧本禁止收集功能 加快剧本执行效率
h 剧本触发功能配置
2) ansible剧本整合方法
编写服务一键化剧本步骤: 配置主机清单--配置目录环境---编写剧本---测试---整合
安装nginx
web01 web02 web03 --- 安装部署好nginx
第一个历程: 编辑yum源文件
vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
第二个历程: 清楚yum缓存 下载nginx软件
yum clean all
yum install -y nginx