Ansible - [09] 高级语法
error 处理机制
默认 ansible 在遇到 error 会立刻停止 playbook
[root@control ansible]# cat ~/ansible/error.yml
---
- hosts: test
tasks:
- name: start a service that does not exist.
service:
name: hehe
state: started
- name: touch a file
file:
path: tmp/service.txt
state: touch
如果第一个任务不能成功执行,那么剧本就会中止,不会执行后续任务。
如果想要在第一个任务执行失败之后,继续执行后续任务,那么……
可以在剧本中添加:ignore_errors: true,可以忽略错误,继续后续的任务
[root@control ansible]# cat ~/ansible/error.yml
---
- hosts: test
tasks:
- name: start a service that does not exist.
service:
name: hehe
state: started
ignore_errors: true
- name: touch a file.
file:
path: /tmp/service.txt
state: touch
以上配置针对是某个任务,如果针对剧本全局忽略错误,可以进行如下配置.
[root@control ansible]# cat ~/ansible/error.yml
---
- hosts: test
ignore_errors: true
tasks:
- name: start a service that does not exist.
service:
name: hehe
state: started
- name: touch a file.
file:
path: /tmp/service.txt
state: touch
handlers
当某个任务需要依赖其他任务怎么办?
- 可以通过handlers定义一组任务
- 仅当某个任务触发(notify)handlers时才执行相应的任务
- 如果有多个notify触发执行handlers任务,也仅执行一次
- 仅当任务的执行状态为changed时,handlers任务才执行
- handlers任务在所有其他任务都执行后才执行
[root@control ansible]# cat ~/ansible/handlers.yml
---
- hosts: test
tasks:
- name: create directory.
file:
path: /tmp/parents/subdir/
state: directory
notify: touch file
handlers:
- name: touch file
file:
path: /tmp/parents/subdir/new.txt
state: touch
多次执行playbook该任务状态不再是changed
notify后面名称必须和handlers中的任务名称一致
when 条件判断
- when可以定义判断条件,条件为真时才执行某个任务
- 常见条件操作符如:
==、!=、>、>=、<、<= - 多个条件可以使用
and或or分割 - when表达式中调用变量不要使用{{ }}
1、远程主机剩余内存不足700M则关闭NetworkManager
[root@control ansible]# cat ~/ansible/when_1.yml
---
- hosts: test
tasks:
- name: check memory size.
service:
name: NetworkManager
state: stopped
when: ansible_memfree_mb < 700
2、判断操作系统是RedHat8则创建测试文件(支持多行输入,不保留换行符)
[root@control ansible]# cat ~/ansible/when_2.yml
---
- hosts: test
tasks:
- name: touch a file
file:
path: /tmp/when.txt
state: touch
when: >
ansible_distribution == "RedHat"
and
ansible_distribution_major_version == "8"
block 任务块
使用block可以将多个任务合并为一个组
[root@control ansible]# cat ~/ansible/block_1.yml
---
- hosts: test
tasks:
- name: define a group of tasks.
block:
- name: install httpd
yum:
name: httpd
state: present
- name: start httpd
service:
name: httpd
state: started
when: ansible_distribution == "RedHat"
rescue 定义block任务执行失败时要执行的其他任务
always 定义无论block任务是否成功,都要执行的任务
[root@control ansible]# cat ~/ansible/block_2.yml
---
- hosts: test
tasks:
- block:
- name: touch a file test1.txt
file:
path: /tmp/test1.txt
state: touch
rescue:
- name: touch a file test2.txt
file:
path: /tmp/test2.txt
state: touch
always:
- name: touch a file test3.txt
file:
path: /tmp/test3.txt
state: touch
loop 循环
1、循环创建多个目录
[root@control ansible]# cat ~/ansible/simple_loop.yml
---
- hosts: test
tasks:
- name: mkdir multi directory.
file:
path: /tmp/{{ item }}
state: directory
loop:
- School
- Legend
- Life
item是关键字
2、循环创建多个用户
[root@control ansible]# cat ~/ansible/complex_loop.yml
---
- hosts: test
tasks:
- name: create multi user.
user:
name: "{{ item.iname }}"
password: "{{ item.ipass | password_hash('sha512') }}"
loop:
- { iname: 'term', ipass: '123456' }
- { iname: 'amy', ipass: '654321' }
Ansible - [09] 高级语法的更多相关文章
- tn文本分析语言(三):高级语法
标签(空格分隔): 未分类 高级操作 1.脚本表达式 用双引号包含的脚本被称为脚本表达式,目前支持嵌入Python. 脚本表达式只能在顺序表达式中使用.代码可以在三个位置存在: |位置|功能|例子| ...
- Swift高级语法学习总结(转)
Swift高级语法学习总结 1.函数 1.1 func funcNmae()->(){} 这样就定义了一个函数,它的参数为空,返回值为空,如果有参数和返回值直接写在两个括号里就可以了 1.2 参 ...
- CSS 高级语法 ---- 继承和选择器的分组
1. 选择器的分组 ————————————————————————— 可以对选择器进行分组,被分组的选择器享用共同的声明. h1,h2,h3,h4,h5,h6 { color: green; ...
- Swift高级语法学习总结
Swift基础语法学习总结Swift高级语法学习总结Swift语法总结补充(一) 1.函数 1.1 func funcNmae()->(){} 这样就定义了一个函数,它的参数为空,返回值为空,如 ...
- C++ 高级语法学习与总结(代码实例)
C++11增加了许多的特性,auto就是一个很明显的例子. 还有就是typedid()获取数据变量的类型 看下面简短的代码: atuo: 很像java中的加强for循环..... //获取一个数据 ...
- Python自动化 【第七篇】:Python基础-面向对象高级语法、异常处理、Scoket开发基础
本节内容: 1. 面向对象高级语法部分 1.1 静态方法.类方法.属性方法 1.2 类的特殊方法 1.3 反射 2. 异常处理 3. Socket开发基础 1. ...
- iOS开发——语法篇OC篇&高级语法精讲二
Objective高级语法精讲二 Objective-C是基于C语言加入了面向对象特性和消息转发机制的动态语言,这意味着它不仅需要一个编译器,还需要Runtime系统来动态创建类和对象,进行消息发送和 ...
- iOS开发——语法篇OC篇&高级语法精讲
高级语法精讲 一.NSSet.NSMutableSet集合的介绍 1)NSSet.NSMutableSet集合,元素是无序的,不能有重复的值. 2)用实例方法创建一个不可变集合对象 例如: //宏定义 ...
- iOS开发——OC篇&OC高级语法
iOS开发高级语法之分类,拓展,协议,代码块详解 一:分类 什么是分类Category? 分类就是类的补充和扩展部分 补充和扩展的每个部分就是分类 分类本质上是类的一部分 分类的定义 分类也是以代码的 ...
- Scala进阶之路-Scala高级语法之隐式(implicit)详解
Scala进阶之路-Scala高级语法之隐式(implicit)详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 我们调用别人的框架,发现少了一些方法,需要添加,但是让别人为你一 ...
随机推荐
- SQLServer单表无缝转换到MySQL
场景:SQLServer 单表结构,无缝转换到MySQL 方法: 1. Navicat-右键需要导出的数据表-逆向表到模型 2. 弹出来的模型窗口里,选择 转换模型为 默认MySQL8.0 确认 3 ...
- 【Amadeus原创】域用户完美执行应用程序
企业环境中,为了安全起见一般都没有赋予域用户或者企业的PC客户端用户管理员权限. 但偶尔会有个别的程序一定需要管理员身份才能执行,如财务某些程序或专业的应用程序.那么如何不赋予用户管理员权限及密码但又 ...
- DBeaver 不错大家都来用 DBeaver 吧
支持 windows linux 支持 pg 等 n 多数据库
- 【Web前端】【开源分享】H5登陆界面 - 2021年12月24日
点我下载
- 龙哥量化:通达信DRAWICON的图标副图显示效果(鸡肋,可以不看)
新建一个副图指标,复制粘贴源码,先看一下效果图 DRAWTEXT_FIX(1,0.01,0.01,0,'通达信中DRAWICON的图标,方便大家折腾指标'),COLORMAGENTA; A:=CURR ...
- Qt开发经验小技巧191-195
关于QList队列的处理中,我们最常用的就是调用append函数添加item,往前插入item很多人第一印象就是调用insert(0,xxx)来插入,其实QList完全提供了往前追加item的函数pr ...
- Qt控件SDK使用示例大全
文章 链接 01表盘控件-01汽车仪表盘-gaugecar https://qtchina.blog.csdn.net/article/details/120240257 01表盘控件-02圆弧仪表盘 ...
- UML之类型
类型是对一个元素能够拥有的值的描述.类型可能是一个无限的集合,例如Integers类型(整数),理论上它的值有无限个:也可能是一个有限的集合,例如Boolean类型(布尔),它只有True和False ...
- 在用Android StudioBuild项目时,提示:Could not resolve all files for configuration ':classpath'.Could not find com.android.tools.
在用Android StudioBuild项目时,提示:Could not resolve all files for configuration ':classpath'.Could not fin ...
- Intellij IDEA部署Web项目到tomcat时提示:Error:Cannot build Artifact ':war exploded' because it is included into a circul
在Idea中使用Maven创建父子工程,第一个Model的那个项目可以很好的运行,在创建一个Model运行时报这个错.原因是tomcat部署了多个Web项目,可能最开始是两个项目的配置文件混用用,最后 ...