代码之家  ›  专栏  ›  技术社区  ›  Ashar

在Ansible中打印时保留文件格式

  •  0
  • Ashar  · 技术社区  · 5 年前

    下面是我打印文件的手册。

    我使用了两种方法,但文件没有按原样打印,即ansible打印文件内容时新行格式消失。

      - name: List startup files
        shell: cat /tmp/test.txt
        register: readws
    
      - debug:
          msg: "/tmp/test.txt on {{ inventory_hostname }} is: {{ readws.stdout_lines }}"
    
      - debug:
          msg: "/tmp/test.txt on {{ inventory_hostname }} is: {{ lookup('file', '/tmp/test.txt') }}"
    
    cat /tmp/test.txt 
    i
    m
    good
    

    预期产量:

    TASK [debug] *****************************************************************************************
    ok: [localhost] => {
        "msg": "/tmp/test.txt on localhost is: 
    i
    m
    good
    "
    }
    

    稳定输出:

    TASK [List startup files] ******************************************************************
    changed: [localhost]
    
    TASK [debug] *****************************************************************************************
    ok: [localhost] => {
        "msg": "/tmp/test.txt on localhost is: [u'i', u'm ', u'good']"
    }
    
    TASK [debug] *****************************************************************************************
    ok: [localhost] => {
        "msg": "/tmp/test.txt on localhost is: i\nm \ngood"
    }
    

    你能建议一下吗?

    1 回复  |  直到 5 年前
        1
  •  1
  •   Zeitounator Vladimir Botka    5 年前

    你无法真正得到你所需要的(除非你改变了输出回调插件…)。

    最接近的方法是显示一系列行,如以下示例所示:

     - name: Show file content
       vars:
         my_file: /tmp/test.txt
         msg_content: |-
           {{ my-file }} on localhost is:
           {{ lookup('file', my_file) }}
       debug:
         msg: "{{ msg_content.split('\n') }}"