Amazon web services 有两个角色的剧本:运行角色B抱怨角色A';s成功运行的代码
我遇到了一个奇怪的行为:当我运行角色B时,它抱怨我可以成功运行的角色a的代码!我将此复制到这个最小的示例:Amazon web services 有两个角色的剧本:运行角色B抱怨角色A';s成功运行的代码,amazon-web-services,ansible,ansible-2.x,ansible-facts,Amazon Web Services,Ansible,Ansible 2.x,Ansible Facts,我遇到了一个奇怪的行为:当我运行角色B时,它抱怨我可以成功运行的角色a的代码!我将此复制到这个最小的示例: $ cat playbooka.yml - hosts: - host_a roles: - role: rolea tags: - taga - role: roleb tags: - tagb $ cat roles/rolea/tasks/main.yml - name: Get servi
$ cat playbooka.yml
- hosts:
- host_a
roles:
- role: rolea
tags:
- taga
- role: roleb
tags:
- tagb
$ 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"
我标记了两个角色,因为我希望有选择地运行角色A或角色B,它们由简单的任务组成,如下所示:
$ cat playbooka.yml
- hosts:
- host_a
roles:
- role: rolea
tags:
- taga
- role: roleb
tags:
- tagb
$ 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"'
但我仍然感到惊讶的是,角色B将解释角色A的代码,并且错误地解释了它。这是我应该报告的错误吗?来自:
Ansible 2.11之前不支持跳过带标记的元任务
由于您运行ansible 2.10,因此无论您使用什么标记,都会对rolea
中的meta
任务的when
条件进行评估。使用-t tagb
时,ansible\u facts.services[“amazon ssm代理”]
不存在,因为您跳过了service\u facts
,然后您会收到报告的错误
您可以:
- 升级到ansible 2.11(可能在我写下这个答案后不久,因为它还没有通过pip提供…)
- 重写您的条件,使
任务在var不存在时跳过,例如。meta
当: -定义了ansible_facts.services[“amazon ssm代理”][“状态”] -ansible_facts.services[“亚马逊ssm代理”][“州”]!=“跑步”
在您的具体案例中,另一种可能性是将
服务事实
任务移动到游戏顺序中更高的其他角色,或在剧本的预任务
部分中,并标记它。在这种情况下,无论您使用什么标记,任务都将始终执行,事实也将始终存在。您是对的,问题在于meta
。当我将meta
替换为debug作为测试时,不存在这样的问题。这是一个难题,谢谢你解决了!我发现我可以使用fail,而不是end_play,它没有问题。