ansible register when: result | succeeded when: item.rc != 0
ansible register 这个功能非常有用。当我们需要判断对执行了某个操作或者某个命令后,如何做相应的响应处理(执行其他 ansible 语句),则一般会用到register 。
举个例子:
我们需要判断sda6是否存在,如果存在了就执行一些相应的脚本,则可以为该判断注册一个register变量,并用它来判断是否存在,存在返回 succeeded, 失败就是 failed.
ansible执行完命令后的rc=0是什么意思?
命令返回值 returncode
tasks:
- shell: echo "I've got '{{ foo }}' and am not afraid to use it!" when: foo is defined - fail: msg="Bailing out. this play requires 'bar'" when: bar is not definedregister关键字可以将任务执行结果保存到一个变量中,该变量可以在模板或者playbooks文件中使用:
|
1
2
3
4
5
6
7
8
9
10
|
- name: test play hosts: all tasks: - shell: cat /etc/motd register: motd_contents - shell: echo "motd contains the word hi" when: motd_contents.stdout.find('hi') != -1 |
上边中的例子中,通过注册变量访问返回的内容,`stdout`里面保存了命令的标准输出内容。注册变量还可以使用在`with_items`中,如果其保存的内容可以转换为列表,或者内容本身就是个列表。如果命令的输出本身就是列表,可以通过`stdout_lines`访问:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
- name: registered variable usage as a with_items list hosts: all tasks: - name: retrieve the list of home directories command: ls /home register: home_dirs - name: add home dirs to the backup spooler file: path=/mnt/bkspool/{{ item }} src=/home/{{ item }} state=link with_items: home_dirs.stdout_lines # same as with_items: home_dirs.stdout.split() |
ansible register 这个功能非常有用。当我们需要判断对执行了某个操作或者某个命令后,如何做相应的响应处理(执行其他 ansible 语句),则一般会用到register 。
举个例子:
我们需要判断sda6是否存在,如果存在了就执行一些相应的脚本,则可以为该判断注册一个register变量,并用它来判断是否存在,存在返回 succeeded, 失败就是 failed.
- name: Create a register to represent the status if the /dev/sda6 exsited
shell: df -h | grep sda6
register: dev_sda6_result
ignore_errors: True
tags: docker
- name: Copy docker-thinpool.sh to all hosts
copy: src=docker-thinpool.sh dest=/usr/bin/docker-thinpool mode=0755
when: dev_sda6_result | succeeded
tags: docker
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
注意: 1、register变量的命名不能用 -中横线,比如dev-sda6_result,则会被解析成sda6_result,dev会被丢掉,所以不要用- 2、ignore_errors这个关键字很重要,一定要配合设置成True,否则如果命令执行不成功,即 echo $?不为0,则在其语句后面的ansible语句不会被执行,导致程序中止。
配合register循环列表
register注册一个变量后,可以配合with_items来遍历变量结果。示例如下:
- shell: echo "{{ item }}"
with_items:
- one
- two
register: echo
- name: Fail if return code is not 0
fail:
msg: "The command ({{ item.cmd }}) did not have a 0 return code"
when: item.rc != 0
with_items: echo.results
ansible register when: result | succeeded when: item.rc != 0的更多相关文章
- aspnetcore identity result.Succeeded SignInManager.IsSignedIn(User) false?
登陆返回的是 result.Succeeded 为什么跳转到其他页面SignInManager.IsSignedIn(User)为false呢? result.Succeeded _signInMan ...
- QueryError:Incorrect result size: expected 1, actual 0
1.错误描述 QueryError:Incorrect result size: expected 1, actual 0 2.错误原因 3.解决办法
- ORA-00001:unique constraint violated 以及 Incorrect result size: expected 1, actual 0
往数据库中插入数据时报错: www.2cto.com ORA-00001: unique constraint (IDX_CARTON_HEADER)violated. 即往CARTON_ ...
- ansible register基础使用讲解
当我们需要判断对执行了某个操作或者某个命令后,如何做相应的响应处理(执行其他 ansible 语句),则一般会用到register . 举个例子: 我们需要判断 zip 包是否存在,如果存在了就执行一 ...
- (后端)org.springframework.dao.EmptyResultDataAccessException: Incorrect result size: expected 1,actual 0
两种方案: 用queryForList方法替换queryForObject或者queryForMap,因为这两个方法必须要有值,不能为空. 把这个异常捕获,用try/catch. 这个查询的结果是nu ...
- Ansible 小手册系列 十四(条件判断和循环)
条件判断 When 语句 在when 后面使用Jinja2 表达式,结果为True则执行任务. tasks: - name: "shut down Debian flavored syste ...
- Ansible状态管理
转载自:http://xdays.me/ansible状态管理.html 简介 就像所有服务器批量管理工具(puppet有DSL,salt有state)一样,ansible也有自己的状态管理组件 ...
- Ansible的循环
Ansible的循环 1. 前言 有可能在一个任务中,可能要做很多事情,例如创建多个用户,安装很多个包等,那么就有可能用到循环. 2. 标准循环 重复的任务可以用下面的方式: ...
- 自动化运维—Ansible(上)
一:为什么选择Ansible 相对于puppet和saltstack,ansible无需客户端,更轻量级 ansible甚至都不用启动服务,仅仅只是一个工具,可以很轻松的实现分布式扩展 更强的远程命令 ...
随机推荐
- 创业就是和靠谱的人一起做热爱的事 印象笔记CEO谈创业
http://www.nowamagic.net/librarys/news/detail/1502在今年美国知名创业孵化器 Y Combinator 的创业学校大会上,印象笔记(Evernote)的 ...
- SVN解决创建文件时不带锁
解决创建文件时不带锁 C:\Documents and Settings\你的用户名\Application Data\Subversion 找到上面的用户路径 打开config添加 ### ...
- JSP开发中对jstl的引用方式(标签库引用)
创建标签库引用文件taglibs.inc 一 采用本地标签库的taglibs.inc文件 <%--struts库标签 --%> <%@ taglib uri="/WEB-I ...
- 经纬度 lbs 笔记
string Lat = objRequest.HeadLat.ToString(); 纬度 string Lng = objRequest.HeadLng.T ...
- FA_在建工程转固定资产(流程)
2014-06-08 Created By BaoXinjian
- PO_从PO追溯PR的方式和表(分析)
2014-06-03 Created By BaoXinjian
- EF相同Context升级为分布式事务的原因
http://www.digitallycreated.net/Blog/48/entity-framework-transactionscope-and-msdtc
- Python rindex() 方法
描述 Python rindex() 方法返回子字符串最后一次出现在字符串中的索引位置,该方法与rfind() 方法一样,只不过如果子字符串不在字符串中会报一个异常. 语法 rindex() 方法语法 ...
- JavaScript / Html 转 pdf、图片
Javascript 将 HTML 页面生成 PDF html2canvas: https://github.com/niklasvh/html2canvas jsPDF: https://githu ...
- python标准库介绍——11 atexit 模块详解
=== atexit 模块=== (用于2.0版本及以上) ``atexit`` 模块允许你注册一个或多个终止函数(暂且这么叫), 这些函数将在解释器终止前被自动调用. 调用 ``register`` ...