0

Ansible 2.2 版,Mac OS X El Capitan

我正在尝试生成端口映射列表。

当我运行这样的代码时

---
- hosts: [localhost]
  sudo: True 
  pre_tasks:
    - debug: 
        var: "{% for id in range(100,102) %}  
                '{{ id }}'
            {% endfor %}"

我希望得到这样的结果:

“‘100’‘101’”

但相反,我得到了结果:

“‘100’‘101’”:“100101”

为什么我得到这样的结果并不是很明显。看起来它从结果中创建了某种地图,但我不明白为什么。

4

1 回答 1

0

你得到了预期的结果,你只是看到不一致的输出,因为你使用了var参数而不是msgdebug模块中。

尝试:

- hosts: localhost
  tasks:
    - debug: 
        msg: "{% for id in range(100,102) %}  
                '{{ id }}'
              {% endfor %}"

在更复杂的情况下,您可能会考虑一种更可靠的调试方式,将内容保存到文件并检查内容。这将消除显示 Ansible 日志的回调插件的影响。

- hosts: localhost
  tasks:
    - copy: 
        dest: ./result.txt
        content: "{% for id in range(100,102) %}  
                    '{{ id }}'
                  {% endfor %}"

接着:

cat ./result.txt
于 2017-01-22T11:24:22.823 回答