ansible register 无视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

原文出处:503error -> https://www.503error.com/2019/ansible-register-%E6%97%A0%E8%A7%86when-%E6%9D%A1%E4%BB%B6%E6%89%A7%E8%A1%8C/1580.html

本站所发布的一切资源仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如果侵犯你的利益,请发送邮箱到 [email protected],我们会很快的为您处理。