Ansible Playbook中的Debug语句的行为与Ansible Role中的不同 [英] Debug statement in Ansible Playbook behaves differently than in Ansible Role
问题描述
在角色中运行调试语句时,我似乎无法在循环中获得Ansible调试语句来显示各个项目的值.为了进行比较,给定此剧本名为./test.yaml:
I cannot seem to get an Ansible debug statement in a loop to display the individual item values when running the debug statement in a role. For comparison, given this playbook named ./test.yaml:
- hosts: localhost
tasks:
- name: test
debug:
var: item
loop:
- 1
- 2
此命令:
ansible-playbook test.yaml
产生此结果:
PLAY [localhost] *****...
TASK [test] ****...
ok: [localhost] => (item=1) => {
"item": 1
}
ok: [localhost] => (item=2) => {
"item": 2
}
但是给定此文件:./roles/TestRole/tasks/main.yaml:
But given this file: ./roles/TestRole/tasks/main.yaml:
- name: test
debug:
var: item
loop:
- 1
- 2
此命令:
ansible localhost -m include_role -a name=TestRole
产生此结果:
localhost | SUCCESS => {
"changed": false,
"include_variables": {
"name": "FooRole"
}
}
localhost | SUCCESS => {
"msg" "All items completed"
}
因此-角色中的debug语句仅显示所有项目已完成",而不是显示项目值.看起来角色中的循环调试语句的行为与剧本中的循环调试语句不同.难道我做错了什么?在python 2.7.5上运行Ansible 2.7.9.
So - rather than displaying the item values, the debug statement in the role just says "All items completed". It looks like looped debug statements in roles behave differently than looped debug statements in playbooks. Am I doing something wrong? Running Ansible 2.7.9 on python 2.7.5.
推荐答案
这实际上是您从adhoc命令获得的(我绝对不知道为什么).同时,这是使用它的一个相当极端的情况.您宁愿在剧本中加入角色.下面的两个剧本示例都将为您带来预期的结果:
This is effectively what you get from the adhoc command (and I have absolutely no clue why). Meanwhile this is a rather edge case of using it. You would rather include a role in a playbook. Both playbook examples below will give you the result you are expecting:
---
- name: test1 for role
hosts: localhost
gather_facts: false
roles:
- role: TestRole
包含角色
---
- name: test2 for roles
hosts: localhost
gather_facts: false
tasks:
- name: include role
include_role:
name: TestRole
这篇关于Ansible Playbook中的Debug语句的行为与Ansible Role中的不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!