本文介绍了Ansible Playbook 中的 Debug 语句的行为与 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"
}

所以 - 而不是显示项目值,角色中的调试语句只是说所有项目已完成".看起来角色中的循环调试语句的行为与剧本中的循环调试语句的行为不同.难道我做错了什么?在 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 中的不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 17:57