ansible register 无视when 条件执行

栏目: 服务器 · 发布时间: 7年前

内容简介:先来看一段ansible代码:正常来说,我们认为会输出结果会是’real_name’,因为第一个任务因为when的条件判断并没有执行但是,结果是什么呢?

先来看一段ansible代码:

- name: test
  hosts: localhost
  gather_facts: no
  vars:
    test_name: 'real_name'
    flag: false
  tasks:
    - name: retrieve node's hostname
      shell: "hostname"
      register: test_name
      when: flag | bool
 
    - debug:
        var: test_name

正常来说,我们认为会输出结果会是’real_name’,因为第一个任务因为when的条件判断并没有执行

但是,结果是什么呢?

ansible-playbook 1.yaml
 [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
 
 
PLAY [test] *************************************************************************************************************************************************************************
 
TASK [retrieve node's hostname] *****************************************************************************************************************************************************
skipping: [localhost]
 
TASK [debug] ************************************************************************************************************************************************************************
ok: [localhost] => {
    "test_name": {
        "changed": false,
        "skip_reason": "Conditional result was False",
        "skipped": true
    }
}
 
PLAY RECAP **************************************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0

并没有,难道是skip了?其实并没有skip, 而是第一个task的register讲test_name的值赋值成了如下三行:

“changed”: false,

“skip_reason”: “Conditional result was False”,

“skipped”: true

也就是说,尽快when的条件没有判断成功,但是register还会工作

具体的变通办法参见

https://stackoverflow.com/questions/34621799/ansible-how-do-i-avoid-registering-a-variable-when-a-when-condition-is-not


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Little MLer

The Little MLer

Matthias Felleisen、Daniel P. Friedman、Duane Bibby、Robin Milner / The MIT Press / 1998-2-19 / USD 34.00

The book, written in the style of The Little Schemer, introduces instructors, students, and practicioners to type-directed functional programming. It covers basic types, quickly moves into datatypes, ......一起来看看 《The Little MLer》 这本书的介绍吧!

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具