主要内容:
Ansible进阶(firewalld模块、template模块、语法:error、handlers、when条件判断、block任务块、loop循环)、Ansible Role基本概念、Roles标准规范、管理Anisble Role
一、ansible应用案例(进阶)
案例要求:
- 1)熟悉firewalld和template模块(识别变量)的使用
- 2)熟悉error处理机制(ignore_error)
- 3)熟悉handlers任务(notify通知)
- 4)熟悉when条件判断
- 5)熟悉block任务块(block、rescue、always)
- 6)熟悉loop循环的使用方法
1、firewalld模块
- 使用firewalld模块可以配置防火墙策略
帮助:ansible-doc firewalld
[root@control ansible]# vim ~/ansible/firewall.yml
---
- hosts: test
tasks:
- name: install firewalld. //任务1描述,安装firewalld
yum: //调用yum模块安装软件
name: firewalld //需安装的软件名称为firewalld
state: present //state: present代表安装软件
- name: run firewalld. //任务2描述,运行firewalld
service: //调用service模块启动服务
name: firewalld //启动的服务名称为firewalld
state: started //state: started代表启动服务
enabled: yes //enabled: yes是设置服务为开机自启
#### 以上任务已安装firewalld,已运行firewalld可忽略 ####
- name: set firewalld rule. //任务描述,设置防火墙规则
firewalld: //调用firewalld模块设置防火墙规则
port: 80/tcp //在防火墙规则中添加一个放行tcp,80端口的规则
permanent: yes //permanent 是设置永久规则
immediate: yes //immediate 是让规则立刻生效
state: enabled //state等于enabled是添加防火墙规则,相反disabled
[root@control ansible]# ansible-playbook firewall.yml
[root@node1 ~]# firewall-cmd --list-all //验证
public (active)
ports: 80/tcp
...
补充:临时规则,可不加 permanent
补充:不指定被控制端的防火墙区域,默认修改的为当前区域
2、template模块
1)copy模块
可以将一个文件拷贝给远程主机,但用户希望每个拷贝的文件内容都不一样(涉及变量),例如给所有web主机拷贝index.html内容是各自的IP地址,则需要用到template模块。
- copy模块与template模块语法与作用一样,区别是copy模块拷贝文件中的变量是不识别的,template模块拷贝的变量会识别;
- 补充:Ansible可以利用Jinja2模板引擎读取变量,之前在playbook中调用变量也是Jinja2的功能,Jinja2模块的表达式包含在分隔符"{ { }}"内。
2)template模块
参数:src指定需要拷贝的源文件,dest指定需要拷贝的目标位置
帮助:ansible-doc template
例如:给webserver主机拷贝index.html文件并放到/tmp/目录,要求每个文件内容不同(facts变量)
[root@control ansible]# vim ~/ansible/index.html //准备测试的源文件
Welcome to {
{ansible_hostname}} on {
{ ansible_eth0.ipv4.address }}.
[root@control ansible]# vim ~/ansible/template.yml
---
- hosts: webserver
tasks:
- name: use template copy index.html to webserver. //任务描述,需要调用template模块
template:
src: ~/ansible/index.html //相对或绝对路径都可以,保证能找到文件
dest: /tmp/index.html
[root@control ansible]# ansible-playbook template.yml
[root@node3 ~]# cat /tmp/index.html //验证
Welcome to node3 on 192.168.4.13.
[root@node4 ~]# cat /tmp/index.html //验证
Welcome to node4 on 192.168.4.14.
解释说明:
# { {ansible_hostname}}和{ { ansible_eth0.ipv4.address }}是ansible自动的facts变量
# src参数: 指定源,~/ansible/index.html是在控制端创建的测试源文件,文件包含变量
# dest参数: 指定目标,将源文件拷贝到被控制端目标主机放在/tmp目录下
3、Ansible高级语法应用
应用案例:error、handles、when、block、loop
3.1 error 错误处理
默认ansible在遇到error会立刻停止playbook;假设一个剧本里有20个任务,执行到第3个时失败,则不再往下执行任务;(使用ignore_errors可忽略错误任务,继续后续的任务);
参数:ignore_errors关键词,针对某个任务或全局任务忽略错误(取决于关键词位置)
示例1:
① 测试Playbook执行一个错误任务,查看执行的意外中断效果
[root@control ansible]# vim ~/ansible/error.yml
---
- hosts: test
tasks:
- name: start a service that does not exit.
service:
name: hehe //启动一个不存在的服务hehe,测试error报错
state: started
- name: touch a file.
file:
path: /tmp/service.txt
state: touch
[root@control ansible]# ansible-playbook error.yml
结果:执行成功任务1次,错误任务1次,执行到错误任务立刻停止playbooky,所以中断了后续的touch任务;
② 测试Playbook,执行时因为忽略了错误(针对某一个任务),不会被中断。
[root@control ansible]# vim ~/ansible/error.yml
---
- hosts: test
tasks:
&