【发布时间】:2020-11-17 23:26:50
【问题描述】:
如果我将字符串存储到 ansible 寄存器中,然后让其他任务仅在该寄存器包含某个值时运行,它们总是运行,而不管寄存器中的值如何。
为什么当条件失败时 ansible 运行任务?
这是一个简单的例子。它应该只调试一次(如test_foo.stdout == "Foo"),但它会打印两个调试语句:
- name: foo
command: "echo \"Foo\""
register: test_foo
tags: [ 'foo-test' ]
- debug:
var: test_foo
when: test_foo.stdout == "Foo",
tags: [ 'foo-test' ]
- debug:
var: test_foo
when: test_foo.stdout == "Bar",
tags: [ 'foo-test' ]
请注意,当使用相同条件触发另一个命令调用时也会发生这种情况,而不仅仅是调试调用。它将始终执行这两个任务。
这是输出:
TASK [test : foo] ***********************************************************************************************************************************************************************************
changed: [localServer -> localhost] => {"changed": true, "cmd": ["echo", "Foo"], "delta": "0:00:01.002745", "end": "2020-11-17 11:22:25.733089", "rc": 0, "start": "2020-11-17 11:22:24.730344", "stderr": "", "stderr_lines": [], "stdout": "Foo", "stdout_lines": ["Foo"]}
TASK [test : debug] *********************************************************************************************************************************************************************************
ok: [localServer] => {
"test_foo": {
"changed": true,
"cmd": [
"echo",
"Foo"
],
"delta": "0:00:01.002745",
"end": "2020-11-17 11:22:25.733089",
"failed": false,
"rc": 0,
"start": "2020-11-17 11:22:24.730344",
"stderr": "",
"stderr_lines": [],
"stdout": "Foo",
"stdout_lines": [
"Foo"
]
}
}
TASK [test : debug] *********************************************************************************************************************************************************************************
ok: [localServer] => {
"test_foo": {
"changed": true,
"cmd": [
"echo",
"Foo"
],
"delta": "0:00:01.002745",
"end": "2020-11-17 11:22:25.733089",
"failed": false,
"rc": 0,
"start": "2020-11-17 11:22:24.730344",
"stderr": "",
"stderr_lines": [],
"stdout": "Foo",
"stdout_lines": [
"Foo"
]
}
}
以防万一这是特定于版本的奇怪东西,我使用的是 Ubuntu 20.04,这是我的版本:
$ ansible --version
ansible 2.9.6
config file = /path/to/project/orchestration/ansible.cfg
configured module search path = ['/home/myUser/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3/dist-packages/ansible
executable location = /usr/bin/ansible
python version = 3.8.5 (default, Jul 28 2020, 12:59:40) [GCC 9.3.0]
【问题讨论】:
标签: ansible