问题
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
This command:
ansible-playbook test.yaml
Produces this result:
PLAY [localhost] *****...
TASK [test] ****...
ok: [localhost] => (item=1) => {
"item": 1
}
ok: [localhost] => (item=2) => {
"item": 2
}
But given this file: ./roles/TestRole/tasks/main.yaml:
- name: test
debug:
var: item
loop:
- 1
- 2
This command:
ansible localhost -m include_role -a name=TestRole
Produces this result:
localhost | SUCCESS => {
"changed": false,
"include_variables": {
"name": "FooRole"
}
}
localhost | SUCCESS => {
"msg" "All items completed"
}
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.
回答1:
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:
Classic role execution
---
- name: test1 for role
hosts: localhost
gather_facts: false
roles:
- role: TestRole
Include role
---
- name: test2 for roles
hosts: localhost
gather_facts: false
tasks:
- name: include role
include_role:
name: TestRole
来源:https://stackoverflow.com/questions/55832732/debug-statement-in-ansible-playbook-behaves-differently-than-in-ansible-role