【问题标题】:Printing output on new lines in ansible jinja2 template在ansible jinja2模板中的新行上打印输出
【发布时间】:2017-03-23 07:55:51
【问题描述】:

我正在努力使用这个脚本在新行上打印它的输出。我在网上尝试了一些建议的解决方案,但似乎都没有奏效。下面是我的剧本的 sn-p。

tasks:
 - debug: msg={% for oct in range(10,12) %}172.16.0.{{ oct }}{% endfor %}

我得到的输出是这样的

  TASK [debug] *******************************************************************
     ok: [localhost] => {
    "msg": "172.16.0.10172.16.0.11"

我需要这样的输出

TASK [debug] *******************************************************************
     ok: [localhost] => {
    "msg": "172.16.0.10"
           "172.16.0.11"

我尝试像{% for oct in range(10,12) %}172.16.0.{{ oct }}'\n'{% endfor %} 那样插入\n,但这只会将\n 作为字符串打印到我的输出中。

【问题讨论】:

    标签: python ansible jinja2


    【解决方案1】:

    您无法使用标准输出插件完全实现您想要的。

    最接近的是当您打印 (debug) 列表时 – Ansible 将在新行上打印每个项目:

    ---
    - hosts: localhost
      connection: local
      gather_facts: no
      tasks:
        - debug: msg="{{ lookup('sequence','start=10 end=11 format=172.16.0.%d',wantlist=true) }}"
    

    结果:

    ok: [localhost] => {
        "msg": [
            "172.16.0.10",
            "172.16.0.11"
        ]
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多