核心
ansible 2.3.0.0 (stable-2.3 923c9ef17c) last updated 2017/03/17 09:12:40 (GMT +1000)
config file = /apps/dev/playbook/jordan/mr-ansible/ansible.cfg
configured module search path = Default w/o overrides
python version = 2.7.12 (default, Sep 23 2016, 14:23:49) [GCC 4.4.7 20120313 (Red Hat 4.4.7-17)]
标准
RHEL 6.7
如果我正在检查是否定义了由模块注册的变量(带有when子句),然后获得子字典值,则这可能不是错误,而只是我在较旧版本中使用Ansible 2.3 RC1时遇到的问题在2.3中失败,而在2.2及以下版本中则正常。 当由于某种原因跳过注册变量的模块,然后执行后续任务检查是否定义了变量并获取子键时,就会发生这种情况。
剧本范例
---
- name: test
hosts: localhost
connection: local
tasks:
- name: module that won't run
stat:
path: /apps/tmp
register: test
when: False
- name: debug of var
debug:
var: test
- name: test with conditional
debug:
msg: A
when: test is defined and test.stat.exists
使用Ansible 2.2.1.0时
ansible 2.2.1.0
config file = /cygdrive/d/dev/jordan/ansible.cfg
configured module search path = Default w/o overrides
PLAY [test] ********************************************************************
TASK [setup] *******************************************************************
ok: [localhost]
TASK [module that won't run] ***************************************************
task path: /cygdrive/d/dev/jordan/2.3tests.yml:7
skipping: [localhost] => {"changed": false, "skip_reason": "Conditional check failed", "skipped": true}
TASK [debug of var] ************************************************************
task path: /cygdrive/d/dev/jordan/2.3tests.yml:13
ok: [localhost] => {
"test": {
"changed": false,
"skip_reason": "Conditional check failed",
"skipped": true
}
}
TASK [test with conditional] ***************************************************
task path: /cygdrive/d/dev/jordan/2.3tests.yml:17
skipping: [localhost] => {"changed": false, "skip_reason": "Conditional check failed", "skipped": true}
PLAY RECAP *********************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0
PLAY [test] ************************************************************************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************************************************************
Friday 17 March 2017 15:20:42 +1000 (0:00:00.039) 0:00:00.039 **********
ok: [localhost]
META: ran handlers
TASK [module that won't run] *******************************************************************************************************************************************************
task path: /apps/dev/playbook/jordan/mr-ansible/test.yml:7
Friday 17 March 2017 15:20:44 +1000 (0:00:01.093) 0:00:01.133 **********
skipping: [localhost] => {"changed": false, "skip_reason": "Conditional result was False", "skipped": true}
TASK [debug of var] ****************************************************************************************************************************************************************
task path: /apps/dev/playbook/jordan/mr-ansible/test.yml:13
Friday 17 March 2017 15:20:44 +1000 (0:00:00.012) 0:00:01.145 **********
ok: [localhost] => {
"changed": false,
"test": {
"changed": false,
"skip_reason": "Conditional result was False",
"skipped": true
}
}
TASK [test with conditional] *******************************************************************************************************************************************************
task path: /apps/dev/playbook/jordan/mr-ansible/test.yml:17
Friday 17 March 2017 15:20:44 +1000 (0:00:00.034) 0:00:01.180 **********
fatal: [localhost]: FAILED! => {"failed": true, "msg": "The conditional check 'test is defined and test.stat.exists' failed. The error was: error while evaluating conditional (test is defined and test.stat.exists): 'dict object' has no attribute 'stat'\n\nThe error appears to have been in '/apps/dev/playbook/jordan/mr-ansible/test.yml': line 17, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: test with conditional\n ^ here\n"}
PLAY RECAP *************************************************************************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=1
嗨!
非常感谢您提交给Ansible。 这对我们意义重大。
我们认为您所提交的票证被误解了,因为一件事的工作方式与所述的有所不同。
将来,这可能是一个更适合用户列表的主题,如果您需要上述帮助,也可以在此处发布。
由于您跳过了'stat'任务,因此您仍然会获得test
作为已定义的字典,但是它没有stat
键,这就是为什么您会收到错误消息。 为避免出现此类问题,您需要:
when: test is defined and 'stat' in test and test.stat.exists
再次感谢您对此事以及您对Ansible的关注!
最有用的评论
可能的误解
嗨!
非常感谢您提交给Ansible。 这对我们意义重大。
我们认为您所提交的票证被误解了,因为一件事的工作方式与所述的有所不同。
将来,这可能是一个更适合用户列表的主题,如果您需要上述帮助,也可以在此处发布。
由于您跳过了'stat'任务,因此您仍然会获得
test
作为已定义的字典,但是它没有stat
键,这就是为什么您会收到错误消息。 为避免出现此类问题,您需要:再次感谢您对此事以及您对Ansible的关注!