首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Ansible - Print message - debug: msg="line1 \n {{ var2 }} \n line3 with var3 = {{ var3 }}“

Ansible - Print message - debug: msg="line1 \n {{ var2 }} \n line3 with var3 = {{ var3 }}“
EN

Stack Overflow用户
提问于 2015-12-10 04:15:21
回答 9查看 103.8K关注 0票数 71

在Ansible (1.9.4)或2.0.0中

我运行了以下操作:

代码语言:javascript
运行
复制
- debug: msg="line1 \n {{ var2 }} \n line3 with var3 = {{ var3 }}"

猫$ roles/setup_jenkins_slave/tasks/main.yml

代码语言:javascript
运行
复制
- debug: msg="Installing swarm slave = {{ slave_name }} at {{ slaves_dir }}/{{ slave_name }}"
  tags:
    - koba

- debug: msg="1 == Slave properties = fsroot[ {{ slave_fsroot }} ], master[ {{ slave_master }} ], connectingToMasterAs[ {{ slave_user }} ], description[ {{ slave_desc }} ], No.Of.Executors[ {{ slave_execs }} ], LABELs[ {{ slave_labels }} ], mode[ {{ slave_mode }} ]"
  tags:
    - koba


- debug: msg="print(2 == Slave properties = \n\nfsroot[ {{ slave_fsroot }} ],\n master[ {{ slave_master }} ],\n connectingToMasterAs[ {{ slave_user }} ],\n description[ {{ slave_desc }} ],\n No.Of.Executors[ {{ slave_execs }} ],\n LABELs[ {{ slave_labels }} ],\n mode[ {{ slave_mode }} ])"
  tags:
    - koba

但是这不是用新的行打印变量(对于第三个调试操作)?

EN

回答 9

Stack Overflow用户

回答已采纳

发布于 2015-12-10 08:37:15

作为一种变通方法,我使用了with_items,它对我来说还不错。

代码语言:javascript
运行
复制
- debug: msg="Installing swarm slave = {{ slave_name }} at {{ slaves_dir }}/{{ slave_name }}"

- debug: msg="Slave properties = {{ item.prop }} [ {{ item.value }} ]"
  with_items:
   - { prop: 'fsroot', value: "{{ slave_fsroot }}" }
   - { prop: 'master', value: "{{ slave_master }}" }
   - { prop: 'connectingToMasterAs', value: "{{ slave_user }}" }
   - { prop: 'description', value: "{{ slave_desc }}"  }
   - { prop: 'No.Of.Executors', value: "{{ slave_execs }}" }
   - { prop: 'LABELs', value: "{{ slave_labels }}" }
   - { prop: 'mode', value: "{{ slave_mode }}" }
  tags:
    - koba
票数 1
EN

Stack Overflow用户

发布于 2016-12-28 14:01:41

debug模块支持数组,所以你可以这样做:

代码语言:javascript
运行
复制
debug:
  msg:
    - "First line"
    - "Second line"

输出:

代码语言:javascript
运行
复制
ok: [node1] => {
    "msg": [
        "First line",
        "Second line"
    ]
}

或者,您可以使用以下答案中的方法:

In YAML, how do I break a string over multiple lines?

票数 89
EN

Stack Overflow用户

发布于 2016-10-30 11:43:25

我发现使用debug打印多行文本最方便的方法是:

代码语言:javascript
运行
复制
- name: Print several lines of text
  vars:
    msg: |
         This is the first line.
         This is the second line with a variable like {{ inventory_hostname }}.
         And here could be more...
  debug:
    msg: "{{ msg.split('\n') }}"

它将消息拆分为一个数组,debug将每一行打印为一个字符串。输出为:

代码语言:javascript
运行
复制
ok: [example.com] => {
    "msg": [
        "This is the first line.", 
        "This is the second line with a variable like example.com", 
        "And here could be more...", 
        ""
    ]
}

感谢jhutar

票数 64
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34188167

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档