我遇到了一个奇怪的行为:当我运行角色 B 时,它抱怨角色 A 的代码我可以成功运行!我已将此复制到这个最小示例中:
$ cat playbooka.yml
- hosts:
- host_a
roles:
- role: rolea
tags:
- taga
- role: roleb
tags:
- tagb
我标记了两个角色,因为我想有选择地运行角色 A 或角色 B,它们由简单的任务组成,如下面的最小示例所示:
$ cat roles/rolea/tasks/main.yml
- name: Get service_facts
service_facts:
- debug:
msg: '{{ ansible_facts.services["amazon-ssm-agent"]["state"] }}'
- when: ansible_facts.services["amazon-ssm-agent"]["state"] != "running"
meta: end_play
$ cat roles/roleb/tasks/main.yml
- debug:
msg: "I am roleb"
预览确认我可以运行标签指定的单个角色:
$ ansible-playbook playbooka.yml -t taga -D -C --list-hosts --list-tasks
playbook: playbooka.yml
play #1 (host_a): host_a TAGS: []
pattern: ['host_a']
hosts (1):
3.11.111.4
tasks:
rolea : Get service_facts TAGS: [taga]
debug TAGS: [taga]
$ ansible-playbook playbooka.yml -t tagb -D -C --list-hosts --list-tasks
playbook: playbooka.yml
play #1 (host_a): host_a TAGS: []
pattern: ['host_a']
hosts (1):
3.11.111.4
tasks:
debug TAGS: [tagb]
我可以运行角色 A 好的:
$ ansible-playbook playbooka.yml -t taga -D -C
PLAY [host_a] *************************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************************************************************************************************************************************
ok: [3.11.111.4]
TASK [rolea : Get service_facts] ******************************************************************************************************************************************************************************************************************
ok: [3.11.111.4]
TASK [rolea : debug] ******************************************************************************************************************************************************************************************************************************
ok: [3.11.111.4] => {
"msg": "running"
}
PLAY RECAP ****************************************************************************************************************************************************************************************************************************************
3.11.111.4 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
但是当我运行角色 B 时,它抱怨我刚刚成功运行的角色 A 中的代码!
$ ansible-playbook playbooka.yml -t tagb -D -C
PLAY [host_a] *************************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************************************************************************************************************************************
ok: [3.11.111.4]
ERROR! The conditional check 'ansible_facts.services["amazon-ssm-agent"]["state"] != "running"' failed. The error was: error while evaluating conditional (ansible_facts.services["amazon-ssm-agent"]["state"] != "running"): 'dict object' has no attribute 'services'
The error appears to be in '<path>/roles/rolea/tasks/main.yml': line 9, column 3, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- when: ansible_facts.services["amazon-ssm-agent"]["state"] != "running"
^ here
We could be wrong, but this one looks like it might be an issue with
unbalanced quotes. If starting a value with a quote, make sure the
line ends with the same set of quotes. For instance this arbitrary
example:
foo: "bad" "wolf"
Could be written as:
foo: '"bad" "wolf"'
我有两个问题:
ansible_facts
也有 services
,并且服务正在“运行”,如上图所示,运行角色 A。PS:我在 MacOS 本地使用最新的 Ansible 2.10.2 和最新的 python 3.9.1。远程 python 可以是 2.7.12 或 3.5.2 (Ubuntu 16_04)。我通过测试字典是否有 services
键解决了这个问题:
ansible_facts.services is not defined or ansible_facts.services["amazon-ssm-agent"]["state"] != "running"
但仍然让我感到惊讶的是,角色 B 会解释角色 A 的代码并错误地解释它。这是我应该报告的错误吗?
答案 0 :(得分:3)
来自notes in meta
module documentation:
Ansible 2.11 之前不支持跳过带有标签的元任务。
自从您运行 ansible 2.10 以来,无论您使用什么标签,您在 when
中的 meta
任务的 rolea
条件都会被评估。当您使用 -t tagb
时,ansible_facts.services["amazon-ssm-agent"]
不存在,因为您跳过了 service_facts
,然后您会收到您报告的错误。
您可以:
meta
任务,例如
when:
- ansible_facts.services["amazon-ssm-agent"]["state"] is defined
- ansible_facts.services["amazon-ssm-agent"]["state"] != "running"
在任何情况下,第二个解决方案仍然是 IMO 的好做法(例如,与运行旧版本的人分享您的工作,在没有安装代理的情况下意外运行主机......)。
在您的特定情况下的另一种可能性是将 service_facts
任务移动到播放顺序更高的其他角色,或在您的剧本的 pre_tasks
部分,并将其标记为 always
.在这种情况下,无论您使用什么标签,任务都将始终播放并且事实将始终存在。