我正在经历一种奇怪的行为:当我运行角色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和最新的python3.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的代码,并错误地解释它。这是我应该报告的错误吗?
发布于 2021-01-23 17:34:40
来自notes in meta module documentation
在Ansible 2.11之前,不支持使用标记跳过元任务的
。
因为您运行了Anble2.10,所以无论您使用什么标记,meta任务的rolea条件总是会被计算出来的。当您使用-t tagb时,ansible_facts.services["amazon-ssm-agent"]不存在,因为您跳过了service_facts,然后得到报告的错误。
你可以:
pip...)
meta任务就会跳过,例如:-ansible_facts.services“amazon”定义-ansible_facts.services“amazon”!=“运行”)
第二种解决方案仍然是IMO在任何情况下的良好实践(例如,与运行旧版本的人共享您的工作,在没有安装代理的情况下意外地与主机运行)。
在您的具体情况下,另一种可能是将service_facts任务按播放顺序或在游戏手册的pre_tasks部分移动到另一个更高的角色,并将其标记为always。在这种情况下,无论使用什么标记,任务都会发挥作用,事实也将始终存在。
https://stackoverflow.com/questions/65861456
复制相似问题