Ansible Playbook中的Debug语句与Ansible Role中的行为不同

问题描述 投票:1回答:1

我似乎无法在循环中获得Ansible调试语句,以便在角色中运行调试语句时显示单个项值。为了比较,给定这个名为./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:

- 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"
}

所以 - 而不是显示项目值,角色中的调试语句只是说“所有项目已完成”。看起来角色中的循环调试语句与playbooks中的循环调试语句的行为不同。难道我做错了什么?在python 2.7.5上运行Ansible 2.7.9。

ansible
1个回答
1
投票

这实际上是你从adhoc命令得到的(我完全不知道为什么)。与此同时,这是使用它的一个相当优势的情况。你宁愿在剧本中加入一个角色。下面的两个剧本示例都将为您提供您期望的结果:

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
© www.soinside.com 2019 - 2024. All rights reserved.